Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is a C program that is using UNIX system calls. #include #include #include #include #include #define BUFLEN 10 int main(void) { int i; char

Below is a ā€˜Cā€™ program that is using UNIX system calls.

#include

#include

#include

#include

#include

#define BUFLEN 10

int main(void)

{

int i;

char buffer[BUFLEN+1];

pid_t fork_return;

fork_return = fork( );

if (fork_return == 0)

{

strncpy(buffer, "CHILD", BUFLEN); /*in the child process*/

buffer[BUFLEN] = '0';

}

else if(fork_return > 0)

{

strncpy(buffer, "PARENT", BUFLEN); /*in the parent process*/

buffer[BUFLEN] = '0';

}

else if(fork_return == -1)

{

printf("ERROR:");


return 1;

}

for (i=0; i<5; ++i) /*both processes do this*/

{

sleep(1); /*5 times each*/

write(1, buffer, strlen(buffer));

}

return 0;

}

When you run this program, it will print

  • One process will always end before the other. If there is enough intervening time before the second process ends, the system call will redisplay the prompt, producing the last line of output where the output from the child process is appended to the end of the prompt (ie. %child)

You know that the wait() system call allows the parent process to suspend its activities until one of these actions has occurred.

Explain the functioning of this program

  1. insert a wait() system call to change the output to:
    Paste it in this document and explain what difference it has made to the program.
  1. There is another system call clone(). How is it different than fork()?
  1. Process Control Block is the information about each process? What is the data structure required to maintain this information?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Lets break down the functioning of the provided C program 1 The program defines a buffer size and initializes a buffer 2 It forks a child process usin... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

C++ Primer Plus

Authors: Stephen Prata

6th Edition

978-0321776402, 0321776402

More Books

Students also viewed these Operating System questions