You are on page 1of 3

mycode_1.

c
A dry run of the program has been mentioned.

__________________________________

Process 1 is executing
Process 1 is forked
Process 1: x = 1
Process 2: x = 0
Process 1 is put on hold. It will resume execution once process 2
terminates.

Process 2 is executing
Process 2 is forked since its copy of x = 0
Process 2: x = 2
Process 3: x = 0
Process 2 is put on hold. It will resume execution once process 3
terminates.

Process 3 is executing
Process 3 is forked since its copy of x = 0
Process 3: x = 3
Process 4: x = 0
Process 3 is put on hold. It will resume execution once process 4
terminates.

Process 4 is executing
Process 4 is forked since its copy of x = 0
Process 4: x = 3
Process 5: x = 0
Process 4 is put on hold. It will resume execution once process 5
terminates.

Process 5 is executing
It exits the if conditions and prints the required string
Process 5 terminates.

Since process 5 has terminated, Process 4 resumes execution


It exits the if conditions and prints the required string
Process 4 terminates.
Since process 4 has terminated, Process 3 resumes execution
It exits the if conditions and prints the required string
Process 3 terminates.

Since process 3 has terminated, Process 2 resumes execution


It exits the if conditions and prints the required string
Process 2 terminates.

Since process 2 has terminated, Process 1 resumes execution


It exits the if conditions and prints the required string
Process 1 terminates.

Program terminates.

__________________________________

Note: The only reason the wait(NULL) has been mentioned after each fork
is because the second question requires that the master process is the
last one to terminate. This has been explained in detail under mycode_2.c

Alternative method for mycode_1.c


I start by storing the PID of the main process in a variable called
“masterPID”.

The program is then forked three times. The reason I have chosen to fork
it three times is because we need a minimum of five processes to run.
Forking twice would give me four processes and thrice gives me eight.

Each process would be assigned its own unique PID. These processes
would also have a track of the PID of the master process.

“Documentation is important.” is only printed by processes with the


following PIDs:
1. masterPID
2. masterPID + 1
3. masterPID + 2
4. masterPID + 3
5. masterPID + 4

This is implemented by a simple if condition: getpid() - masterPID <= 4

Here, I have used the fact that child processes get PIDs that are one plus
the last PID that was used.
I however chose not to go with this method due to the following reasons:
5 processes are only what are required. The extra 3 processes are not
required and only take up processing power with no output.

mycode_2.c

The program begins by forking.

The child process prints its own PID. Following that, it execs the file
“mycode_1” which is the executable file for mycode_1.c. The if condition
that follows is just an error check in case “mycode_1” does not exist.

The parent process waits till the child process finishes executing.

Following this, the parent prints its own PID and the child’s PID(which is
parent’s PID + 1 since parent’s PID is the last PID that was used in the
program).

Note: The child process will terminate when the process(master process)
that it directly calls by execl would terminate. The termination of the child
process is independent of the termination of any processes created by the
master process.

A problem I faced while implementing this was that the master process of
mycode_1.c was terminating before its child processes, causing the child
process of mycode_2.c to terminate, after which the parent process of
mycode_2.c continued executing and got completed, after which some of
the child processes of mycode_1.c got completed. The output became
dependent on how the system scheduled the different processes created.

This was removed by adding a wait(NULL) after every fork in mycode_1.c.

You might also like