Fork a Process The following code can fork a child process. Both parent and chil
ID: 3853708 • Letter: F
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 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 <stdio.h>
#include <string.h>
#include <sys/types.h>
#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));
}
}
Explanation / Answer
NOTE: In my OS the output is coming in sequence like first parent pid then child pid which generally do not happen for every OS. Please check the code in your system and revert back in case of any issues. I will promise to respond back within 24 hours.
Code:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#define MAX_COUNT 50
#define BUF_SIZE 100
void main(void)
{
pid_t pid;
int i;
char buf[BUF_SIZE];
pid = fork();
for (i = 1; i <= MAX_COUNT; i++) {
if(pid==0){
if(i%2) printf("This line is from Child pid %d, value = %d ", pid, i);
}
else{
if(i%2 == 0) printf("This line is from Parent pid %d, value = %d ", pid, i);
}
}
}
Execution and output:
This line is from Parent pid 22771, value = 2
This line is from Parent pid 22771, value = 4
This line is from Parent pid 22771, value = 6
This line is from Parent pid 22771, value = 8
This line is from Parent pid 22771, value = 10
This line is from Parent pid 22771, value = 12
This line is from Parent pid 22771, value = 14
This line is from Parent pid 22771, value = 16
This line is from Parent pid 22771, value = 18
This line is from Parent pid 22771, value = 20
This line is from Parent pid 22771, value = 22
This line is from Parent pid 22771, value = 24
This line is from Parent pid 22771, value = 26
This line is from Parent pid 22771, value = 28
This line is from Parent pid 22771, value = 30
This line is from Parent pid 22771, value = 32
This line is from Parent pid 22771, value = 34
This line is from Parent pid 22771, value = 36
This line is from Parent pid 22771, value = 38
This line is from Parent pid 22771, value = 40
This line is from Parent pid 22771, value = 42
This line is from Parent pid 22771, value = 44
This line is from Parent pid 22771, value = 46
This line is from Parent pid 22771, value = 48
This line is from Parent pid 22771, value = 50
This line is from Child pid 0, value = 1
This line is from Child pid 0, value = 3
This line is from Child pid 0, value = 5
This line is from Child pid 0, value = 7
This line is from Child pid 0, value = 9
This line is from Child pid 0, value = 11
This line is from Child pid 0, value = 13
This line is from Child pid 0, value = 15
This line is from Child pid 0, value = 17
This line is from Child pid 0, value = 19
This line is from Child pid 0, value = 21
This line is from Child pid 0, value = 23
This line is from Child pid 0, value = 25
This line is from Child pid 0, value = 27
This line is from Child pid 0, value = 29
This line is from Child pid 0, value = 31
This line is from Child pid 0, value = 33
This line is from Child pid 0, value = 35
This line is from Child pid 0, value = 37
This line is from Child pid 0, value = 39
This line is from Child pid 0, value = 41
This line is from Child pid 0, value = 43
This line is from Child pid 0, value = 45
This line is from Child pid 0, value = 47
This line is from Child pid 0, value = 49