Process Scheduling: Source Code 1
Process Scheduling: Source Code 1
Source Code 3:
program My_Pgm3
i = 10
for n = 1 to 15
x=i+n
next
end
Source Code 4:
program My_Pgm3
i=5
for n = 1 to 15
x=i+n
next
end
Create 4 processes P1, P2, P3 and P4 from source codes 1, 2, 3 and 4 respectively with following
properties. Fill up the following table:
Scheduling Algorithm : FCFS
Process Arrival Time Waiting time
P1 0 0.19 sec
P2 0 47.19 sec
P3 0 85.09 sec
P4 0 150.49 sec
Average waiting time: 70.74 sec
Scheduling Algorithm : Round Robin with time quantum 5
Process Arrival Time Waiting time
P1 0 9.11 sec
P2 0 9.63 sec
P3 0 7.45 sec
P4 0 7.54 sec
Average waiting time: 104.5 sec
Scheduling Algorithm : Round Robin with time quantum 10
Process Arrival Time Waiting time
P1 0 15.7 sec
P2 0 16.66 sec
P3 0 13.12 sec
P4 0 13.52 sec
Average waiting time: 94.84 sec
Out of three cases, which one is better and why?
FCFS seems to be faster than round robin because of faster and efficient execution of
processes.
2) Consider the following source code
program ThreadTest
sub thread1 as thread
for i = 1 to 5
writeln(“In thread1: “, i)
next
end sub
sub thread2 as thread
call thread1
for i = 6 to 10
writeln(“In thread2 :”, i)
next
end sub
call thread2
writeln(“Inmain”)
wait
end
P1 0 2
P2 1 3
P3 2 5
P4 3 4
P5 4 6
4) Write a program to implement SJF scheduling algorithm and run the program using CPU -OS
Simulator for below processes.
Process id Arrival time CPU Burst time
P1 0 2
P2 1 3
P3 2 5
P4 3 4
P5 4 6
call thread2
call thread1
wait
writeln("Exiting main")
end
Using CPU-OS simulator Compile the above code, load it in the memory, create a process
and set RR as the scheduling algorithm.
Perform the following:
a) Set time quantum for RR scheduling algorithm to 10 ticks and start the program.
When the program stops, note down the value of g printed in sub thread1 and sub
thread2 routines.
Thread1: g = 6; Thread2: g = 15
b) Set time quantum for RR scheduling algorithm to 5 ticks and start the program.
When the program stops, note down the value of g printed in sub thread1 and sub
thread2 routines.
Thread1: g = 7; Thread2: g = 16
Is there any difference in values printed in a andb? If so, what is the reason behind it?
What is the value of g printed if thread1 and thread2 executes in sequential manner?
When used on FCFS algorithm the g value changed.
Thread1: g = 5; Thread2: g = 15
Program CriticalRegion
var g integer
sub thread1 as thread synchronise
writeln("In thread1")
g=0
for n = 1 to 10
g=g+1
next
writeln("thread1 g = ", g)
writeln("Exiting thread1")
end sub
sub thread2 as thread synchronise
writeln("In thread2")
g=0
for n = 1 to 5
g=g+1
next
writeln("thread2 g = ", g)
writeln("Exiting thread2")
end sub
writeln("In main")
call thread1
call thread2
wait
writeln("Exiting main")
end
Using CPU-OS simulator compiles the above code, load it in the memory, create a process
and set RR as the scheduling algorithm.
Perform the following:
a) Set time quantum for RR scheduling algorithm to 10 ticks and start the program.
When the program stops, note down the value of g printed in sub thread1 and sub
thread2 routines.
Semaphore Mutex
1 Semaphore is a signalling Mutex is a locking
mechanism. mechanism
2 Semaphore allows multiple Mutex allow multiple
program thread to access program thread to access
finite instance of resources. a single resource but not
simultaneously.
3 Semaphores value is Mutex object is locked or
modified using wait () and unlocked by the process
signal () operation. requesting or releasing the
resource.
4 If all resources are being If a Mutex object is
used, the process requesting already locked, the
for resource perform wait () process requesting for
operation and block itself till resources waits and
semaphore count becomes queued by the system till
greater than one. lock is released.
7) Consider the following source code
Program ThreadTest
sub thread1 as thread
for i = 1 to 10
writeln("In thread1: ", i)
next
end sub
sub thread2 as thread
for i = 1 to 10
writeln("In thread1: ", i)
next
end sub
call thread1
call thread2
writeln("In main")
do
loop
end
Using CPU-OS simulator ,compile the above source and load it in main memory. Create a
single process, choose RR scheduling algorithm with time quantum of 3 ticks. Run the
Process.
Answer the following questions:
e) How many processes and how many threads are created?
1 process and 2 threads
f) Identify the name of the processes and threads.
Process: THREADTEST; Threads: P1T0 and P1T1
g) What is the PID and PPID of the processes and threads created?
e) When the main process is in waiting state, first thread is running and second thread is in
ready state, click on SUSPEND button on OS Control panel. Observe the memory allotted
for main process, and two threads.
FCFS:
SJF (No-Pre-emptive):
SJF (Non-Pre-emptive):
SJF (Pre-emptive):
RR (5 ticks):
P1 0 2 5(lowest)
P2 1 3 1(highest)
P3 2 5 4
P4 3 4 2
P5 4 6 5
I am using SJF algorithm with Pre emptive setting for this process.
Answer the following questions:
1. What is average waiting time and response time?
Ans: Average waiting time = 2.83 sec; Average response time = 2.83 sec
2. What is Average turnaround time?
Ans: Average turnaround time = 2.32 secs
11) Enter the following source code in the CPU-OS simulator:
NOTE: enter and leave keyword pair protect the program code between them. This makes sure the
protected code executes exclusively without sharing the CPU with any other thread.
a) Compile the above program and load in memory.
Perform the following:
a) Set time quantum for RR scheduling algorithm to 10 ticks and start the program.
When the program stops, note down the value of g printed in sub thread1 and sub
thread2 routines.
Thread1: g=20; Thread2: g = 12
b) Set time quantum for RR scheduling algorithm to 5 ticks and start the program.
When the program stops, note down the value of g printed in sub thread1 and sub
thread2 routines.
Thread1: g=20; Thread2: g = 12
b) Write down the observation when the above keywords are added and explain their use.
Enter: This keyword allows only the given thread to start executing.
Leave: This keyword only allows the thread to leave the block after completing the
execution.
Wait: As the thread1 starts executing control goes over the whole command block and also
into the thread2. This is where wait comes into play. Wait puts other threads into waiting
state until the current thread has completed execution.
12) Enter the following source code in CPU-OS simulator.
programCriticalRegion
sub thread1 as thread
var g integer
writeln("In thread1")
g=0
for n = 1 to 20
g=g+1
next
writeln("thread1 g = ", g)
writeln("Exiting thread1")
end sub
sub thread2 as thread
var g integer
writeln("In thread2")
g=0
for n = 1 to 12
g=g+1
next
writeln("thread2 g = ", g)
writeln("Exiting thread2")
end sub
writeln("In main")
call thread1
call thread2
wait
writeln("Exiting main")
end
a) Compile the above program and load in memory as before. Next, run it. Make a note of the
two values of variable g.
b) Why have we chosen to display the same global variable g in both threads?
e) In the absence of any help from hardware and operating system, how would you protect a
critical region in your code? Suggest a way of doing it and state how it would differ from the
above methods
Ans: In a multi-process test system, sharing a resource among the processes requires a locking
mechanism to protect critical sections. A critical section is needed when one of the processes
needs exclusive access to a shared resource. To prevent another process from accessing the
instrument during the critical section, the first process locks the resource. The lock remains in
effect for the time necessary to complete its task. During this time, the second process cannot
execute any interaction with the resource, including an attempt to lock the resource for its own
use. This can be achieved by using semaphores and mutexes.
Operating Systems Assignment on CPU-OS Simulator
c) What is the PID and PPID of the processes and threads created?
P0 0 10
P1 1 6
P2 3 2
P3 4 4
P4 6 5
deadlockp2.txt :
program p2
resource(3, allocate)
wait(3)
resource(4, allocate)
for n = 1 to 20
next
end
deadlockp3.txt :
program p3
resource(4, allocate)
wait(3)
resource(1, allocate)
for n = 1 to 20
next
end
deadlockp4.txt :
program p4
resource(6, allocate)
wait(3)
resource(1, allocate)
for n = 1 to 20
next
end
deadlockp5.txt :
program p5
resource(2, allocate)
wait(3)
resource(5, allocate)
for n = 1 to 20
next
end
deadlockp6.txt :
program p6
resource(7, allocate)
wait(3)
resource(2, allocate)
for n = 1 to 20
next
end
a) Compile all the four source code and load in main memory at starting addresses 150, 250,
350, 450, 550 and 650 respectively. (Attach snapshots)
b) Create 6 processes P1, P2, P3, P4, P5 and P6. (Attach snapshots)
c) Use round robin scheduling algorithm to schedule the processes for execution at
different ticks like 5 ticks, 10 ticks and 15 ticks.
d) Show the status of the processes with the necessary snapshots.
Consider the following table of arrival time and burst time for three processes P0, P1
and P2.
Process Arrival time Burst Time
P0 0 ms 9 ms
P1 1 ms 4 ms
P2 2 ms 9 ms
The pre-emptive shortest job first scheduling algorithm is used. Scheduling is carried
out only at arrival or completion of processes. Draw Gantt Chart ,What is the
average waiting time and avg. turnaround time for the three processes.
17)Statement 2:-
Consider the following table with Process, BurstTime,Priority and consider all
processes are arrived in Ready Queue.
Process Burst time Priority
P1 8 4(Low)
P2 10 1(High)
P3 6 3
P4 4 2
Perform Process scheduling by following Methods
1.FCFS
2.SJF(Non Premptive)
3.Premptive SJF
4.Premptive Priority
Draw Gant Chart and calculate Avg Waiting Time and Avg Turnaround time by
above Methods.
18) Show that the two-phase locking protocol ensures conflict serializability.
19) Servers can be designed to limit the number of open connections. For example, a server
may wish to have only N socket connections at any point in time. As soon as N connections
are made, the server will not accept another incoming connection until an existing connection
is released. Explain how semaphores can be used by a server to limit the number of
concurrent connections.
Questions To Be Answered?
Questions to be answered?