Question: Fork a Process The following code can fork a child process. Both parent and child process can all print out 50 lines. Please modify the

Fork a Process

The following code can fork a child process. Both parent and child process can all print out 50 lines. Please modify the code to make parent process print out the even number of lines among the 50 lines, and the child process will only print out the odd number of lines. In your output message, please specify if the process is a parent process or a child process. And also output the process id. Please submit your C language source code

#include

#include

#include

#define MAX_COUNT 50

#define BUF_SIZE 100

void main(void)

{

pid_t pid;

int i;

char buf[BUF_SIZE];

fork();

pid = getpid();

for (i = 1; i <= MAX_COUNT; i++) {

sprintf(buf, "This line is from pid %d, value = %d ", pid, i);

write(1, buf, strlen(buf));

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!