Professional Documents
Culture Documents
A: 0
Explanation: In the child process the fork() returns 0, So the PID value at A will
be zero
else if (pid == 0)
{
pid1 = getpid();
printf(child: pid = %d, pid); /* A */
printf(child: pid1 = %d, pid1); /* B */
}
Another simple logical explanation is, in the above code the PID must be zero for
the condition to be satisfied for the execution to gets to line A.
B: 1203
Explanation: At B the current processs PID should be displayed. The current
process PID is obtained from getpid() into PID1. The current process is a child
process so the PID1 value is 1203 (According to the assumption in the question)
C: 1203
Explanation: In the parent process the fork() returns the child PID, So the PID
value at C will be 1203 (According to the assumption in the question)
D: 1200
Explanation: At D the current processs PID should be displayed. The current
process PID is obtained from getpid() into PID1. The current process is a parent
process so the PID1 value is 1200 (According to the assumption in the question)
Question2 answers:
a. There will be a total of 6 unique processes created by the three fork()
function calls in the program.
Explanation:
1. The first fork() creates two process, let us name them as PP and PC
where PP is parent process and PC is child process.
2. The second fork in the program is under the if statement if(pid == 0),
this means that only child process can execute the second i.e., only PC
can execute this fork()
3. So the PC will create two process again, let us name them as PCP and
PCC.
4. After coming to the final fork() i.e., the third one we have three unique
process ie., PP, PCP and PCC
5. After executing the third fork() these process will create parent and
child process,
a. PP PPP, PPC
b. PCP PCPP, PCPC
c. PCC PCCP, PCCC
6. So the final six unique process are PPP, PPC, PCPP, PCPC, PCCP, PCCC
7. The above can be graphically depicted as below
8.
MAIN PROCESS
1st Fork
PC
PP
3rd Fork
PPP
2nd Fork
PPC
PCC
PCP
3rd Fork Child
PCPP
PCPC
PCCP
PCCC
PCC
Thread
Thread