Os Past Papers' Solutions 2017

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

PAST PAPERS’ SOLUTIONS

YEAR 2017

Q # 3:
Schedule the following processes using RR. The processes P1, P2 and P3 have
arrived at time unit 0, 1 and 2 respectively. The number inside the parenthesis
indicates the time units for CPU and I/O bursts. Assume a time quantum of 4 time
units.

a. Create a Gantt chart


b. Calculate turnaround time

Process CPU Burst I/O Burst CPU Burst I/O Burst CPU Burst
P1x 3x 6x 2x 4x 5(1)x
P2x 5(1)x 4x 3x x x
P3 6(2)x 8x 5(1)x 7 2

a. Gantt Chart:

P1 P2 P3 P1 P2 P3 P1 P2 P1 P3
0 3 7 11 13 14 16 20 23 24

P3 No P P3
28 29 33 36 38

Process Arrival Time CPU Finish Waiting Turnaround


Burst Time Time Time
P1 0 10 24 14 24
P2 1 8 23 14 22
P3 2 13 38 24 36

Here, Waiting time= Finish Time – CPU Burst Time – Arrival Time
Average waiting time= 14 + 14 + 24 / 3
=17.33

Turn Around Time = Finish Time – Arrival Time


Average Turn Around Time = 24 + 22 + 36 / 3
=27.33

Q# 4 a: Zombie vs. Orphan processes:

Zombie Process:

A process which has finished the execution but still has entry in the process
table to report to its parent process is known as a zombie process. A child
process always first becomes a zombie before being removed from the
process table. The parent process reads the exit status of the child process
which reaps off the child process entry from the process table.
Orphan Process:

A process whose parent process no more exists i.e. either finished or


terminated without waiting for its child process to terminate is called an
orphan process. However, the orphan process is soon adopted by init
process, once its parent process dies.

b. Output of given code snippet:

First understand the following terms


Fork() & PID:

System call fork() is used to create processes. It takes no arguments and returns a
process ID. The purpose of fork() is to create a new process, which becomes
the child process of the caller. After a new child process is created, both processes
will execute the next instruction following the fork() system call. Therefore, we have
to distinguish the parent from the child. This can be done by testing the returned
value of fork():

• If fork() returns a negative value to the parent process as the PID, the
creation of a child process was unsuccessful.
• fork() returns a zero as PID if the new child process was create, and returns a
positive value, the process ID of the child process, to the parent.
• The returned process ID is of type pid_t defined in sys/types.h. Normally, the
process ID is an integer. Moreover, a process can use function getpid() to
retrieve the process ID assigned to this process.

Wait system call:


The wait() system call suspends execution of the current process until one of its
children terminates. wait(): on success, returns the process ID of the terminated
child; on error, -1 is returned.

The call wait(&status) is equivalent to:


waitpid(-1, &status, 0);
The waitpid() system call suspends execution of the current process until a child
specified by pid argument has changed state. By default, waitpid() waits only for
terminated children, but this behaviour is modifiable via the options argument, as
described below.
The value of pid can be:

meaning wait for any child process whose process group ID is equal to the
< -1
absolute value of pid.

-1 meaning wait for any child process.


meaning wait for any child process whose process group ID is equal to that of the
0
calling process.

>0 meaning wait for the child whose process ID is equal to the value of pid.

Solution of part b:
Here after creating variables int cpid and status,a fork () is executed and cpid will be
assigned with the code returned by fork (). Cpid will have one of three values; a
number greater than 0, a 0 or a negative number.
First, the statement “start execution” will be printed on screen.

• If (cpid > 0) is true, that means we are inside the parent process right now. The
wait (&status) will get the status of the newly created child process by fork() and
will wait until it terminates.

On the next line, again a fork () is executed to create one more child process.
Statement “between creation” will be printed and one more child process will be
created with fork(). The statement “Inside Parent Process” will be printed on screen.
On the next line, “Bye” is printed.

If, on the other hand, cpid was equal to 0, the Statement “Inside Child Process” will
be printed. Fork() will create a new child process here. On next line “Good Bye” will
be printed and exit (0) will be executed, which means that the code was executed
successfully and process will end.

In case of negative value of cpid, the process would have ended by giving an error
message as –ve number means child process creation wasn’t successful.

Q # 5:
Number of instances: A (9), B (3), C (6)
So the total available recourses: (9, 3, 6)

Process Max Allocation


A B C A B C
P1 3 2 2 1 0 0
P2 6 1 3 6 1 2
P3 3 1 4 2 1 1
P4 4 2 2 0 0 2

Current Available Resources: (9, 3, 6) – (9, 2, 5) = (0, 1, 1)


Need Matrix:
Process Need (Max-
Allocation)
A B C
P1 2 2 2
P2 0 0 1
P3 1 0 3
P4 4 2 0

For Safe Sequence, select a process such that Need of the process < Available
resources.
Current Available Resources= (0, 1, 1)

1st Iteration:
Need (Process P2) = (0,0,1) < (0,1,1) Available resources
Process P2 completes and returns allocated resources, so
Current Available Resources = (0, 1, 1)
+ Resources Allocated to P2 = (6, 1, 2)
=Current Available Resources= (6, 2, 3)

2nd Iteration:
Need (Process P1) = (2,2,2) < (6,2,3) Available resources
Process P1 completes and returns allocated resources, so
Current Available Resources = (6, 2, 3)
+ Resources Allocated to P1 = (1, 0, 0)
=Current Available Resources= (7, 2, 3)

3rd Iteration:
Need (Process P3) = (1,0,3) < (7,2,3) Available resources
Process P3 completes and returns allocated resources, so
Current Available Resources = (7, 2, 3)
+ Resources Allocated to P3 = (2,1, 1)
=Current Available Resources= (9, 3, 4)

Need (Process P4) = (4,2,0) < (9,3,4) Available resources


Process P4 completes and returns allocated resources, so
Current Available Resources = (9, 3, 4)
+ Resources Allocated to P1 = (0, 0, 2)
=Current Available Resources= (9, 3, 6)

so now
Current Available Resources (9, 3, 6) = Total available recourses: (9, 3, 6)

Safe sequence: P2->P1->P3->P4

You might also like