Questions 4. (15 points) Including the initial parent processes, how many proces
ID: 3735436 • Letter: Q
Question
Questions 4. (15 points) Including the initial parent processes, how many processes are created by the following program? #include #include int main() pid t pid pid fork(); if (pid 0) { forkf) return 0; lelse if (pid >o) wait(NULL) return 0Explanation / Answer
The purpose of fork() is to create a new process. It takes no arguments and returns a PID (process ID) of the new process. The process that called fork() is called the parent process and the new process created is called the child process. After a new child process is created, both parent and child processes will execute the next instruction following the fork(). fork() call returns a 0 to parent process and a non-negative number to the child process, which is PID of child process. So pid = fork(); #1 if (pid == 0) { #2 fork(); #3 return 0; } else if(pid > 0) { #4 wait(NULL); return 0; } a. if original process is P, Line #1 creates a new Process P1. Then Line #2 allows only the parent process, which creates a new process P2 at line 3. Process P1 starts at line 4 and enters the block. Procee P2 starts at line 3 and returns. So total of 3 processes have been created by the program. Note that, line #5 will be executed by all of the above processes.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.