Run Using Kali Linux....Create a source code file named "Lab1.c". The code is as follows. A child is created and prints a message five times. The original process (parent) prints a message only three times. How would this code look on the compiler if you could please post the code and the compiler results. Need Reference Thank You
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
pid_t pid;
char *message;
int n;
printf("fork program starting\n");
pid = fork();
switch(pid)
{
case -1:
perror("fork failed");
exit(1);
case 0:
message = "This is the child";
n = 5;
break;
default:
message = "This is the parent";
n = 3;
break;
}
for(; n > 0; n--) {
puts(message);
sleep(1);
}
exit(1);
}
Form creates 2^n processes suppose 1 fork() is used that means 1 child process and 1 parent process will be created and 0 means child process and 1 means parent process so n=5 that is coded inside case 0: so when child process is created it will enter into the case 0: and n=5 is assigned there so child process will have their loop 5 times and parent is 1 so it will go to non zero parent process where n=3 is there so loop will execute 3 time , both process have their own copy of for loop to execute.
if you have any doubt then please ask me without any hesitation in the comment section below , if you like my answer then please thumbs up for the answer , before giving thumbs down please discuss the question it may possible that we may understand the question different way and i can edit and change the answers if you argue, thanks :)
Get Answers For Free
Most questions answered within 1 hours.