Below is a C program that is using UNIX system calls. #include #include #include #include #include #define
Question:
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
- 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.
- There is another system call clone(). How is it different than fork()?
- Process Control Block is the information about each process? What is the data structure required to maintain this information?