Process Scheduling: Source Code 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 38

Process Scheduling

1) Consider the following 3 source codes:


Source Code 1:
program My_Pgm1
i=1
for n = 1 to 10
x=i+n
next
end
Source Code 2:
program My_Pgm2
i = 10
for n = 1 to 8
x=i+n
next
end

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

Using CPU -OS simulator perform below actions:


1. Compile the above source and load it in main memory.
2. Create a single process, choose RR scheduling algorithm with time quantum of 3 ticks.
3. Run the Process.
Answer the following questions:
a) How many processes and how many threads are created?
1 process and 2 threads
b) Identify the name of the processes and threads.
Process: THREADTEST; Threads: P1T0 and P1T1
c) What is the PID and PPID of the processes and threads created?

d) Represent the parent and child relationship using tree representation


3) Write a program to implement FCFS 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

Answer the following questions:


1. What is average waiting time and response time?
Average waiting time =2.76 Secs sec, average response time: 2.78 Sec
2. What is Average turnaround time?
Average Turnaround time = 2.2948 Secs

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

Answer the following questions:


1. What is average waiting time and response time?
Average waiting time =2.78 secs, average response time: 2.78 secs
2. What is Average turnaround time?
Average Turnaround time = 2.31 Secs
5) Consider the following source code
program CriticalRegion
var g integer
sub thread1 as thread
writeln("In thread1")
g=0
for n = 1 to 5
g=g+1
next
writeln("thread1 g = ", g)
writeln("Exiting thread1")
end sub
sub thread2 as thread
writeln("In thread2")
g=0
for n = 1 to 15
g=g+1
next
writeln("thread2 g = ", g)
writeln("Exiting thread2")
end sub
writeln("In main")

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

 Identify the critical section in the code.


For thread1:
g=0
for n = 1 to 5
g=g+1
next
writeln("thread1 g = ", g)
For thread2:
g=0
for n = 1 to 15
g=g+1
next
writeln("thread1 g = ", g)
 What is the overall conclusion?
6) Consider the following source code

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.

Thread1: g = 10; Thread2: g=5


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 = 10; Thread2: g=5


c) Is there any difference in values printed in a and b?
No there is no difference found between a and b.
d) What popular high‐level language uses the keyword synchronise(or similar) for the
same purpose as the code given above?
Synchronization in java is the capability to control the access of multiple threads to
any shared resource.
e) Critical regions are often implemented using semaphores and mutexes. Find out
what these are and how they differ. Describe on a separate sheet.

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?

h) Represent the parent and child relationship using tree representation


8) Consider the following source code:
program ThreadTest2
var s1 string(4)
var s2 string(4)
sub thread1 as thread
s1 = "WASE"
writeln("In thread1")
while true
wend
end sub
sub thread2 as thread
call thread1
s2 = "WIMS"
writeln("In thread2")
while true
wend
end sub
call thread2
writeln("In main")
wait
writeln(s1)
writeln(s2)
end
Perform the following :
a) Compile the above program. Load the program in the main memory starting at address 100.
Create a process. Note down the name of the process. Choose RR scheduling algorithm
with time quantum equals to 3 ticks.
Process name: THREADTEST2
b) Click on the SYMBOL TABLE… button in the compiler window. In the displayed
window, observe the information on variables s1 and s2. Where variables s1, s2 are stored?

c) Start the process. List the names of the threads created.


d) Press VIEW PROCESS STATES button in Views panel and observe how process and two
threads are executed by the processor.

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.

f) Now resume execution by pressing the RESUME button on OS Control panel.


Process was resumed and executed successfully.
9) In the compiler window, enter the following source code in the compiler’s source editor window
program Loop
i=0
for n = 1 to 40
i=i+1
next
end
a) Select different scheduling policies and run the processes in the OS simulator.

FCFS:

SJF (No-Pre-emptive):

SJF (Non-Pre-emptive):
SJF (Pre-emptive):

RR (5 ticks):

b) Explain the differences between pre‐emptive and non‐pre‐emptive scheduling.


 In pre-emptive scheduling the CPU is allocated to the processes for the limited time whereas in
Non-pre-emptive scheduling, the CPU is allocated to the process till it terminates or switches to
waiting state.
 The executing process in pre-emptive scheduling is interrupted in the middle of execution when
higher priority one comes whereas, the executing process in non-pre-emptive scheduling is not
interrupted in the middle of execution and wait till its execution.
 In Pre-emptive Scheduling, there is the overhead of switching the process from ready state to
running state, vice-verse, and maintaining the ready queue. Whereas in case of non-pre-emptive
scheduling has no overhead of switching the process from running state to ready state.
 In pre-emptive scheduling, if a high priority process frequently arrives in the ready queue then the
process with low priority has to wait for a long, and it may have to starve. On the other hands, in the
non-pre-emptive scheduling, if CPU is allocated to the process having larger burst time then the
processes with small burst time may have to starve.
 Pre-emptive scheduling attain flexible by allowing the critical processes to access CPU as they
arrive into the ready queue, no matter what process is executing currently. Non-pre-emptive
scheduling is called rigid as even if a critical process enters the ready queue the process running
CPU is not disturbed.
 The Pre-emptive Scheduling has to maintain the integrity of shared data that’s why it is cost
associative as it which is not the case with Non-pre-emptive Scheduling.
10) Write a program to implement Priority scheduling algorithm and run the program using CPU
-OS Simulator for below processes.
Process id Arrival time CPU Burst time Priority

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:

Program Critical Region


var g integer
sub thread1 as thread
writeln("In thread1")
enter
g=0
for n = 1 to 20
g=g+1
next
writeln("thread1 g = ", g)
leave
writeln("Exiting thread1")
end sub
sub thread2 as thread
writeln("In thread2")
enter
g=0
for n = 1 to 12
g=g+1
next
writeln("thread2 g = ", g)
leave
writeln("Exiting thread2")
end sub
writeln("In main")
call thread1
call thread2
wait
writeln("Exiting main")
end

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

c) Is there any difference in values printed in a and b?


There is no difference between a and b.

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?

To check if the value varies or not while executing in different threads.


c) Critical regions are often implemented using semaphore and mutex. Find out what these
are and how they differ. Describe on a separate sheet.
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 a
finite instance of resources. 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 already
used, the process requesting locked, the process
for resource perform wait () requesting for resources
operation and block itself till waits and queued by the
semaphore count becomes system till lock is released.
greater than one.

d) Some computer architectures have a “test‐and‐set” CPU instruction for implementing


critical regions. Find out how this works and briefly describe on a separate sheet.
Ans:’ Test‐and‐Set’ is an instruction that returns the old value of a memory location and
sets the memory location value to 1 as a single atomic operation. If one process is currently
executing a test-and-set, no other process is allowed to begin another test-and-set until the
first process test-and-set is finished. This also reduces the chances of having deadlocks. But
it suffers from spin locks.

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

13) For the source code given below


program threadtest
sub thread1 as thread
for i = 1 to 10
writeln("in thread1:", i)
next
end sub
sub thread2 as thread
call thread1
for i = 11 to 20
writeln("in thread2:", i)
next
end sub
sub thread3 as thread
call thread2
for i = 21 to 30
writeln("in thread3:", i)
next
end sub
sub thread4 as thread
call thread3
for i = 21 to 30
writeln("in thread4", i)
next
end sub
call thread4
writeln("inmain")
wait
end

Using CPU -OS simulator perform below actions:


1. Compile the above source and load it in main memory.
2. Create a single process, choose RR scheduling algorithm with time quantum of 3ticks.
3. Run the Process.
Answer the following questions by attaching the necessary snapshots of the CPU OS
simulator
a) How many processes and how many threads are created?
Ans: 1 process and 4 threads

b) Identify the name of the processes and threads.


Ans: Process: THREADTEST; Threads: P1T8, P1T8T9, P1T8T9, P1T8T10, P1T8T11

c) What is the PID and PPID of the processes and threads created?

d) Represent the parent and child relationship using treerepresentation.


14) Write a program to implement FCFS algorithm and run the program using CPU -OS
Simulator for below processes. (Attach the necessary CPU-OS simulator snap shots
afterrunning).

Process id Arrival time CPU Burst time

P0 0 10
P1 1 6
P2 3 2
P3 4 4
P4 6 5

consider the following source code for 6 programs


deadlockp1.txt:
program p1
resource(1, allocate)
wait(3)
resource(2, allocate)
for n = 1 to 20
next
end

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

Using CPU simulator perform below actions:

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.

Operating Systems Assignment to Be Answered Without Using CPU-OS Simulator

15) Consider the following snap-shot of asystem


Processes Allocated MAX Available
R1 R2 R3 R4 R1 R2 R3 R4 R1 R2 R3 R4
P1 0 0 1 2 0 0 1 2 2 1 0 0
P2 2 0 0 0 2 7 5 0
P3 0 0 3 4 6 6 5 6
P4 2 3 5 4 4 3 5 6
P5 0 3 3 2 0 6 5 2
i) Find need matrix
ii) Is the system in the safe state? Justify
iii) Can a request(0,1,0,0) from P3 be safely granted immediately? Justify the
answer. Show the system state after grant of request?
13) Consider a process requesting to read from the following tracks:

98, 183, 37, 122, 14, 124, 65, 67


a) Draw track chart for FCFS, SSTF, SCAN, SEEK, C-SCAN and C-SEEK
algorithms of diskscheduling.
b) Determine total head movement in tracks for eachcase
c) Justify which is the best algorithm.
16) Statement 1:-

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.

20) Write A Program To Implement PreemptiveSjf Algorithm For The


Given Inputs(Implement Using CpuOs Simulator)

Process Arrival Time Burst Time


P1 0 9
P2 1 4
P3 2 9

Questions To Be Answered?

1) What Is The Average Waiting Time For The Three Processes?


2) What Is Total Turn Around Time Of Process P3?
3) What is the Completion time for each process?

21) Observe memory allocation –worst fit algorithm using OS


simulator and comments on the results for the below process details.

Process Id Memory Size


P1 15
P2 25
P3 20

22) Observe disk scheduling–FCFS using OS simulator for the below


queue: 95, 180, 34, 119, 11, 123, 62, and 64 with the read –write head
initially at the track 50.
23) Considering a system with five processes P0 through P4 and three
resources of type A, B, C. Resource type A has 10 instances, B has 5
instances and type C has 7 instances. Suppose at time t0 following
snapshot of the system has been taken:

Questions to be answered?

1) What will be the content of need matrix?


2)  Is the system in a safe state? If Yes, then what is the safe sequence?
3) What will happen if process P1 requests one additional instance of resource
type A and two instances of resource type C?

You might also like