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

Answer the following questions based on the C code for Program1A.c and Program1B

ID: 3803237 • Letter: A

Question

Answer the following questions based on the C code for Program1A.c and Program1B.c:

1. Are any of the variables of a parent shared with any of its child processes? Do the children of the same parent share some variables between themselves?

2. Are file descriptors of a parent inherited by child processes? Specifically, if a file Project1.txt is opened by a parent and kept open across a call to fork, is Project1.txt still open in the child process? If the answer is yes, when a child process reads blocks from Project1.txt, will the parent remain at the same position in Project1.txt?

3. Remember that the wait and exit calls can be used for communication between a parent process and its child processes. Now modify Project1A.c so that the parent process terminates only after BOTH its child processes have terminated.

4. Is it allowed for a parent to terminate before one of its child processes has terminated? If so, does this affect the child processes in any way?

Is it possible for the parent process in Project1A.c to terminate one of its child processes, say A1 or A2? Now, is it possible for the parent process in Project1B.c to terminate one of its grandchild processes, say A1 or A2? If so, how? Hint: Using exit to pass data between the child and parent will not work in this case. For details, see the man page for wait.

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

1. fork()ing creates an exact copy of the parent process at the time of forking. However, after the fork() is completed, the child has a completely different existence, and will not report back to the parent. Hence, no sharing of any of the variables. Global variables are still global within its own process. Children of same parent also become a diffrent process altogether, hence they also don't share any data.

2.  There are two things: the file descriptor, which is a small integer that the process uses in its read and write calls to identify the file, and the file description, which is a structure in the kernel. The file offset is part of the file description. It lives in the kernel.
so whenever we create a new child using fork(), we get the same file descriptor copied to the child too, that is only one intgere, pointing to some file_description which is not part of our parent-child process. Hence, when child performs any file related operation using file descriptor, ultimately file description advances. Now, when parent tries to read or write now, it will use the same file description structure. So yes, file descriptors are shared but parent will not remain at same position if child reads.

3. You have already wait calls in place
int main()
{
   pid_t pid1, pid2;  
   pid1 = fork();
       if (pid1 == 0) { // child process
           printf("Child process A1 created ");
exit(0);
   }
       else {
           pid2 = fork();
           if (pid2 == 0) { // 2nd child process
               printf("Child process A2 created ");
   exit(0);
       } else{
           waitpid(pid2,0,0); // wait till child process ends
           waitpid(pid1,0,0); // wait till child process ends
       }
   }
   return 0;
}


4. Its absolutely possible for the parent to terminate before the child process completes, Because both are independent processes after the fork call and anyone can be run at anytime based on schedulign, and that is the reason why we place wait calls for the child processes in the parent process.

Its allowed for the parent to terminate before child.In this case, generally nothing happens (from the viewpoint of the child) when the parent dies. The orphaned child will get a new foster parent, this is called init in the Unix world, and child will keep exxecuting as normal.

A parent can kill a child process using a SIGKILL signal, by calling kill(process_id, SIGKILL) as long as parent has the child pid stored with it.

I can only answer top 4 questions here. Please post a seperate question in case of other questions.

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