You are on page 1of 3

ASSIGNMENT: Quiz1

Name: Krishna Sagar Biradawada


Id: 1005541
Question1 answers:
If a fork function is executed, it creates a copy of the existing process in a
separate address space. A Parent process and a child process will be running.
The following values are returned if a fork is executed properly else -1 will be
returned.
In child process, 0 is returned as PID
In parent process, new process PID is returned
getpid() is used to get the PID of the current process.
Assumptions in the problem:
1. 1200 is the PID of the parent process
2. 1203 is the PID of the child process.
The PIDs at the line A, B, C, D are as follows:

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

b. There will be a total of 2 unique threads created by the thread create ()


function call.
Explanation:
1. The statement thread create() is after the second fork() call which is
in the condition if(pid==0) so, the statement is only accessed by the
child process i.e., PCP and PCC
2. So the PCP creates one thread and PCC creates another thread. So
there will be two unique threads in total.
3. Graphical representation is as follows.
PCP

PCC

Thread

Thread