Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

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

Professional Microsoft SQL Server 2014 Administration

Authors: Adam Jorgensen, Bradley Ball

1st Edition

111885926X, 9781118859261

More Books

Students also viewed these Databases questions