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

1. Explain step by step what is happening in the following two C programs and ho

ID: 3803046 • Letter: 1

Question

1. Explain step by step what is happening in the following two C programs and how they connect:

Project1A.c

#include
#include

int main()
{
   pid_t pid1, pid2;  
   pid1 = fork();
       if (pid1 == 0) { // child process
           printf("Child process A1 created ");
   }
       else {
           pid2 = fork();
           if (pid2 == 0) { // 2nd child process
               printf("Child process A2 created ");
       } else{
           waitpid(pid2,0,0); // wait till child process ends
           waitpid(pid1,0,0); // wait till child process ends
       }
   }
   return 0;
}

Project1B.c

#include
#include


int main()
{
   pid_t pid1;  
   pid1 = fork();
       if (pid1 == 0) { // child process
       execv("Project1A",NULL); // run another process named Project1A
           exit(127);
   } else {
       waitpid(pid1,0,0); // wait till child process ends
   }

   return 0;
}

Explanation / Answer

Project1A.c

#include<stdio.h>
#include<conio.h>

int main()
{
   pid_t pid1, pid2;  
   pid1 = fork();
       if (pid1 == 0) { // child process
           printf("Child process A1 created ");
   }
       else {
           pid2 = fork();
           if (pid2 == 0) { // 2nd child process
               printf("Child process A2 created ");
       } else{
           waitpid(pid2,0,0); // wait till child process ends
           waitpid(pid1,0,0); // wait till child process ends
       }
   }
   return 0;
}

Project1B.c

#include<stdio.h>
#include<conio.h>


int main()
{
   pid_t pid1;  
   pid1 = fork();
       if (pid1 == 0) { // child process
       execv("Project1A",NULL); // run another process named Project1A
           exit(127);
   } else {
       waitpid(pid1,0,0); // wait till child process ends
   }

   return 0;
}