Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you explain the below written C code? #include<stdio.h> #include<unistd.h> #

ID: 3804051 • Letter: C

Question

Can you explain the below written C code?

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>

int main()
{
int status;
int pid;
  
/// first fork
pid=fork();

if(pid<0)
{
printf("Error ");
exit(-1);
}
else if(pid==0)
{
  
/// second fork
int pid2=fork();
if(pid2<0)
{
printf("Error ");
exit(-1);
}
else if(pid2==0)
{
printf("First One Exiting(Process:%d).. ",getpid());
exit(0);
  
}
else
{
   wait(0);
printf("Second One Exiting(Process:%d).. ",getpid());
exit(0);
  
}
  
}
else
{
wait(0);
int pid2=fork();
if(pid2==0)
{
printf("Third One Exiting(Process:%d).. ",getpid());
exit(0);
  
}
else
{
   wait(0);
printf("Fourth One Exiting(Process:%d).. ",getpid());
exit(0);
  
}
  
}

  
   return 0;

}

  

fork() fork() fork() exit (0) exit (0) exit (0) exit(0)

Explanation / Answer

Here is the code expanied:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/wait.h>
int main()
{
int status;
int pid;
  
/// first fork
pid=fork();   //Here starts the new process, and the code from here will be executed by 2 processes assume, the first process P1, and the child process P1.1.
if(pid<0)   //This block is executed when the child process is not created successfully.
{
printf("Error ");
exit(-1);
}
else if(pid==0)   //This block will be executed by the child process i.e., by P1.1
{
  
/// second fork
int pid2=fork();   //Here another process will be created and the code from here will also be executed also by another process assume P1.1.1.
if(pid2<0)       //This happens if the just before created process fails.
{
printf("Error ");
exit(-1);
}
else if(pid2==0)   //This block will be executed by the process P1.1.1
{
printf("First One Exiting(Process:%d).. ",getpid());   //This will be printed.
exit(0);       //Process P1.1.1 will come to an end here.
  
}
else   //This block will be executed by process first processes child, i.e., by P1.1
{
wait(0);   //Will push the process P1.1 to wait state, if there are any child processes for P1.1. As no child processes are there, it has nothing to wait for.
printf("Second One Exiting(Process:%d).. ",getpid());   //This will be printed.
exit(0);   //P1.1 comes to an end here.
  
}
  
}
else   //This block will be executed by the parent process P1.
{
wait(0);   //Will push the process P1 to wait state, if there are any child processes for P1. As no child processes are there, it has nothing to wait for.
int pid2=fork();   //Here starts another mew process assume P1.2
if(pid2==0)   //This block is executed by the child processi.e., by P1.2
{
printf("Third One Exiting(Process:%d).. ",getpid());   //P1.2 will come to an end.
exit(0);       //And P1.2 will come to an end.
  
}
else   //This block is executed by the parent process P1.
{
wait(0);   //Will push the process P1 to wait state, if there are any child processes for P1. As no child processes are there, it has nothing to wait for.
printf("Fourth One Exiting(Process:%d).. ",getpid());   //This will be printed.
exit(0);   //And pP1 will also come to an end.
  
}
  
}
  
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote