sub code:cs2257 sub name: operating systems dept: cse sem...

28
A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 1 Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem/Year:IV/II UNIT II: PROCESS SCHEDULING AND SYNCHRONIZATION PART A (2 Marks) 1. What is deadlock? (AUC NOV2010) A deadlock is a situation in which two or more competing actions are each waiting for the other to finish, and thus neither ever does. 2. Distinguish pre-emption and No-pre-emption (AUC NOV2008,AUC MAY 2012) Preemption means the operating system moves a process from running to ready without the process requesting it. Without preemption, the system implements ―run to completion (or yield or block)‖. The ―preempt‖ arc in the diagram. Preemption needs a clock interrupt (or equivalent). Preemption is needed to guarantee fairness. Preemption is found in all modern general purpose operating systems. Even non preemptive systems can be multiprogrammed (e.g., when processes block for I/O). 3. Is it possible to have a deadlock involving only one process? Explain your answer. (AUC NOV2008) No. This follows directly from the hold-and-wait condition . 4. What are conditions under which a deadlock situation may arise? Deadlock can arise if four conditions hold simultaneously. Mutual exclusion: only one process at a time can use a resource. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes. No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task. Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1 5. What is critical section problem?

Upload: others

Post on 20-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 1

Sub Code:CS2257 Sub Name: Operating Systems

Dept: CSE Sem/Year:IV/II

UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

PART – A (2 Marks)

1. What is deadlock? (AUC NOV2010)

A deadlock is a situation in which two or more competing actions are each waiting for

the other to finish, and thus neither ever does.

2. Distinguish pre-emption and No-pre-emption (AUC NOV2008,AUC MAY

2012)

Preemption means the operating system moves a process from running to ready without

the process requesting it.

Without preemption, the system implements ―run to completion (or yield or block)‖.

The ―preempt‖ arc in the diagram.

Preemption needs a clock interrupt (or equivalent).

Preemption is needed to guarantee fairness.

Preemption is found in all modern general purpose operating systems.

Even non preemptive systems can be multiprogrammed (e.g., when processes block for

I/O).

3. Is it possible to have a deadlock involving only one process? Explain your

answer. (AUC NOV2008)

No. This follows directly from the hold-and-wait condition .

4. What are conditions under which a deadlock situation may arise?

Deadlock can arise if four conditions hold simultaneously.

Mutual exclusion: only one process at a time can use a resource.

Hold and wait: a process holding at least one resource is waiting to acquire additional

resources held by other processes.

No preemption: a resource can be released only voluntarily by the process holding it, after

that process has completed its task.

Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting

for a resource that is held by P1

5. What is critical section problem?

Page 2: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 2

A critical section is a piece of code that accesses a shared resource that must not be

concurrently accessed by more than one thread of execution. A critical section will usually

terminate in fixed time, and a thread.

6. Define busy waiting and spin lock

When a process is in its critical section, any other process that tries to enter its critical section

must loop continuously in the entry code. This is called as busy waiting and this type of

semaphore is also called a spinlock, because the process while waiting for the lock.

7. What are the four necessary conditions that are needed for deadlock can occur?

(AUC MAY 2012)

Mutual Exclusion - At least one resource must be held in a non-sharable mode; If any other process requests this resource, then that process must wait for the resource to be released. Hold and Wait - A process must be simultaneously holding at least one resource and waiting for at least one resource that is currently being held by some other process. No preemption - Once a process is holding a resource ( i.e. once its request has been granted ), then that resource cannot be taken away from that process until the process voluntarily releases it. Circular Wait - A set of processes { P0, P1, P2, . . ., PN } must exist such that every P[ i ]

is waiting for P[ ( i + 1 ) % ( N + 1 ) ]

8. What is a semaphore? State the two parameters. (AUC APR/MAY 2010,AUC

NOV/DEC 2011)

A semaphore S is integer variable that can be only be accessed via two indivisible (atomic)

operations wait and signal.

9. What is deadlock? What are the schemes used in operating system to handle

deadlocks? (AUC APR/MAY 2010)

Ensure that the system will never enter a deadlock state.

Allow the system to enter a deadlock state and then recover.

Ignore the problem and pretend that deadlocks never occur in the system; used by most

operating systems, including UNIX.

10. What is dispatcher?

Dispatcher module gives control of the CPU to the process selected by the short-term

scheduler; this involves:

o switching context

o switching to user mode

o jumping to the proper location in the user program to restart that program

11. What is dispatch latency?

Page 3: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 3

The dispatch latency is referred as time it takes for the dispatcher to stop one process and start

another running.

12. Define Mutual Exclusion. (AUC NOV/DEC

2011)

If process pi is executing in its critical section,then no other processes an be executing in their

critical section.

13. What is turnaround time?

Turnaround time is the difference of time between the time of arrival of process and time of

dispatch of process or we can say the time of completion of process

14. Why CPU scheduling is required?. (AUC JUN 2009)

Selects from among the processes in memory that are ready to execute, and allocates the

CPU to one of them.

15. List the three requirements that must be satisfy by the critical-section problem.

(AUC JUN 2009)

Mutual Exclusion. If process Pi is executing in its critical section, then no other processes can

be executing in their critical sections.

Progress. If no process is executing in its critical section and there exist some processes that

wish to enter their critical section, then the selection of the processes that will enter the critical

section next cannot be postponed indefinitely.

Bounded Waiting. A bound must exist on the number of times that other processes are

allowed to enter their critical sections after a process has made a request to enter its critical

section and before that request is granted.

16. Define throughput

Throughput in CPU scheduling is the number of processes that are completed per unit time.

For long processes, this rate may be one process per hour; for short transactions, throughput

might be 10 processes per second.

17. Define race condition

When several process access and manipulate same data concurrently, then the outcome of the

execution depends on particular order in which the access takes place is called race condition.

To avoid race condition, only one process at a time can manipulate the shared variable

18. What is a resource-allocation graph?

Deadlock can be described through a resource allocation graph.

• The RAG consists of a set of vertices P={P1,P2 ,…,P n} of processes and R={R1,R2,…,Rm}

of resources.

• A directed edge from a processes to a resource, Pi->R j, implies that Pi has requested Rj.

Page 4: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 4

• A directed edge from a resource to a process, Rj->Pi, implies that Rj has been allocated by

Pi.

• If the graph has no cycles, deadlock cannot exist. If the graph has a cycle, deadlock may

exist.

19. Define deadlock prevention

Deadlock prevention is a set of methods for ensuring that at least one of the four necessary

conditions like mutual exclusion, hold and wait, no preemption and circular wait cannot hold. By

ensuring that that at least one of these conditions cannot hold, the occurrence of a deadlock

can be prevented.

20. Define deadlock avoidance.

Avoiding deadlocks is to require additional information about how resources are to be

requested. Each request requires the system consider the resources currently available, the

resources currently allocated to each process, and the future requests and releases of each

process, to decide whether the could be satisfied or must wait to avoid a possible future

deadlock.

21. What are a safe state and an unsafe state?

A state is safe if the system can allocate resources to each process in some order and still

avoid a deadlock. A system is in safe state only if there exists a safe sequence. A

sequence of processes <P1,P2,....Pn> is a safe sequence for the current allocation state if, for

each Pi, the resource that Pi can still request can be satisfied by the current available resource

plus the resource held by all the Pj, with j<i. if no such sequence exists, then the system state

is said to be unsafe.

22. What is banker’s algorithm?

Banker's algorithm is a deadlock avoidance algorithm that is applicable to a resource allocation

system with multiple instances of each resource type. The two algorithms used for its

implementation are:

a. Safety algorithm: The algorithm for finding out whether or not a system is in a safe state.

b. Resource-request algorithm: if the resulting resource allocation is safe, the transaction is

completed and process Pi is allocated its resources. If the new state is unsafe Pi must wait and

the old resource-allocation state is restored.

Page 5: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 5

PART-B (16 Marks )

1. Explain in detail about any two CPU scheduling algorithms with suitable

examples. (16)

(AUC APR’10,NOV’11)

CPU Scheduler

Selects from among the processes in memory that are ready to execute, and allocates the

CPU to one of them.

CPU scheduling decisions may take place when a process:

Switches from running to waiting state.

Switches from running to ready state.

Switches from waiting to ready.

Terminates.

Scheduling under 1 and 4 is non preemptive.

All other scheduling is preemptive

Scheduling Criteria

CPU utilization – keep the CPU as busy as possible

Throughput – # of processes that complete their execution per time unit

Turnaround time – amount of time to execute a particular process

Waiting time – amount of time a process has been waiting in the ready queue

Response time – amount of time it takes from when a request was submitted until the

first

response is produced, not output

Scheduling Algorithm

First-Come, First-Served (FCFS) Scheduling

1. Suppose that the processes arrive in the order: P1, P2, and P3

The Gantt chart for the schedule is

2. Waiting time for P1 = 0; P2 = 24; P3 = 27

3. Average waiting time: (0 + 24 + 27)/3 = 17

Page 6: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 6

Suppose that the processes arrive in the order P2, P3, P1…..n

The Gantt chart for the schedule is:

Average waiting time: (6 + 0 + 3)/3 = 3

Convoy effect short process behind long process

Waiting time for P1 = 6; P2 = 0; P3 = 3

Average waiting time: (6 + 0 + 3)/3 = 3

Convoy effect short process behind long process

Shortest-Job-First (SJR) Scheduling

1. Associate with each process the length of its next CPU burst. Use these lengths to schedule

the process with the shortest time.

2. Two schemes:

a. nonpreemptive – once CPU given to the process it cannot be preempted until Completes its

CPU burst.

b. preemptive – if a new process arrives with CPU burst length less than remaining time of

current executing process, preempt. This scheme is known as the Shortest-Remaining-Time-

First (SRTF).

3. SJF is optimal – gives minimum average waiting time for a given set of processes

Example of Non-Preemptive SJF

Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

1. SJF (non-preemptive)

Average waiting time = (0 + 6 + 3 + 7)/4 - 4

Page 7: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 7

Example of Preemptive SJF

2. SJF (preemptive)

4. Average waiting time = (9 + 1 + 0 +2)/4 – 3

Determining Length of Next CPU Burst

Can only estimate the length.

Can be done by using the length of previous CPU bursts, using exponential averaging.

o tn actual length of n th CPU burst.

o tn+1=Predicted value for the next CPU burst

o α, 0 ≤α ≤ 1

o Define :

Examples of Exponential Averaging

Recent history does not count.

Only the actual last CPU burst counts.

2. What is a deadlock? What are the necessary conditions for a deadlock to occur?

(AUC NOV/DEC 2011)

A process requests resources; if the resources are not available at that time, the process

enters a wait state. Waiting processes may never again change state, because the resources

they have requested are held by other waiting processes. This situation is called a deadlock.

Deadlock can arise if four conditions hold simultaneously.

Page 8: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 8

Mutual exclusion: only one process at a time can use a resource.

Hold and wait: a process holding at least one resource is waiting to acquire additional

resources held by other processes.

No preemption: a resource can be released only voluntarily by the process holding it, after

that process has completed its task.

Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting

for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is

waiting for a resource that is held by Pn, and P0 is waiting for a resource that is held by P0.

Resource-Allocation Graph

A set of vertices V and a set of edges E.

V is partitioned into two types:

a. P = {P1, P2… Pn}, the set consisting of all the processes in the system.

b. R = {R1, R2… Rm}, the set consisting of all resource types in the system.

Request edge – directed edge 1P Rj

Assignment edge – directed edge Rj Pi

Process

Resource Type with 4 instances

Pi requests instance of Rj

Pi is holding an instance of Rj

Page 9: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 9

If graph contains no cycles no deadlock.

If graph contains a cycle

a. if only one instance per resource type, then deadlock.

b. if several instances per resource type, possibility of deadlock.

3. How can a system recover from deadlock? (10) (AUCNOV/DEC 2011)

When a detection algorithm determines that a deadlock exists, several alternatives exist. One

possibility is to inform the operator that a deadlock has spurred, and to let the operator deal

with the deadlock manually. The other possibility is to let the system recover from the deadlock

automatically. There are two options for breaking a deadlock. One solution is simply to abort

one or more processes to break the circular wait. The second option is to preempt some

resources from one or more of the deadlocked processes.

1. Process Termination

To eliminate deadlocks by aborting a process, we use one of two methods. In both methods,

the system reclaims all resources allocated to the terminated processes.

• Abort all deadlocked processes: This method clearly will break the dead – lock cycle, but

at a great expense, since these processes may have computed for a long time, and the results

of these partial computations must be discarded, and probably must be recomputed.

• Abort one process at a time until the deadlock cycle is eliminated: This method incurs

considerable overhead, since after each process is aborted a deadlock-detection algorithm

must be invoked to determine whether a processes are still deadlocked.

2. Resource Preemption

To eliminate deadlocks using resource preemption, we successively preempt some resources

from processes and give these resources to other processes until he deadlock cycle is broken.

Selecting a victim – minimize cost.

Rollback – return to some safe state, restart process for that state.

Starvation – same process may always be picked as victim, include number of rollback

incost factor.

Combined Approach to Deadlock Handling

Combine the three basic approaches

Prevention,avoidance,detection allowing the use of the optimal approach for each of resources

in the system.

Partition resources into hierarchically ordered classes.

Page 10: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 10

Use most appropriate technique for handling deadlocks within each class

4. What is synchronization? Explain how semaphores can be used to deal with n-

process critical section problem. ( 8) (AUC MAY 2006 APR/MAY 2010)]

1. Synchronization tool that does not require busy waiting.

2. Semaphore S – integer variable

3. can only be accessed via two indivisible (atomic) operations

wait (S):

while S

0 do no-op;

S--;

signal (S):

S++;

Critical Section of n Processes

Shared data:

semaphore mutex; //initially mutex = 1

Process Pi:

do {

wait(mutex);

critical section

signal(mutex);

remainder section

} while (1);

Semaphore Implementation

Define a semaphore as a record typedef struct

{

int value;

struct process *L;

} semaphore;

Page 11: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 11

Assume two simple operations:

Block suspends the process that invokes it.

Wakeup (P) resumes the execution of a blocked process P.

Implementation

Semaphore operations now defined as

wait(S):

S.value--;

if (S.value < 0) {

block;

add this process to S.L;

}

signal(S):

S.value++;

if (S.value <= 0) {

remove a process P from S.L;

wakeup(P);

}

Semaphore as a General Synchronization Tool

1) Execute B in Pj only after A executed in Pi

2) Use semaphore flag initialized to 0

3) Code:

Pi Pj A wait (flag)

Signal (flag) B

5. Explain Banker's deadlock-avoidance algorithm with an illustration. (8) (AUC

APR/MAY 2010)

Banker’s Algorithm

Multiple instances.

Each process must a priori claim maximum use.

When a process requests a resource it may have to wait.

Page 12: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 12

When a process gets all its resources it must return them in a finite amount of time.

Data Structures for the Banker’s Algorithm

Let n = number of processes, and m = number of resources types.

1. Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj

available.

2. Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of

resource type Rj.

3. Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.

4. Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its

task.

5. Need [i,j] = Max[i,j] – Allocation [i,j].

Safety Algorithm

1. Let Work and Finish be vectors of length m and n, respectively. Initialize:

Work = Available

Finish [i] = false for i - 1,3, …, n.

2. Find Available [i]

(a) Finish [i] = false

(b) Need [i] = work

If so such i exists, go to step 4.

3. Work = Work + Allocation[i]

Finish[i] = true

go to step 2.

4. If Finish [i] == true for all i, then the system is in a safe state.

Resource-Request Algorithm for Process Pi

Request = request vector for process Pi.

If Request i [j] = k then process Pi wants k instances of resource type Rj.

5. .If Request [i]=Need [i] go to step 2.

Otherwise, raise error condition, since process has exceeded its maximum claim.

6. If Request [i]=Available, go to step 3.

Page 13: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 13

Otherwise Pi must wait, since resources are not available.

Pretend to allocate requested resources to Pi by modifying the state as follows:

Available = Request [i];

Allocation [i] = Allocation [i] + Request [i];

Need [i] = Need [i] – Request [i];

If safe the resources are allocated to Pi.

If unsafe Pi must wait, and the old resource-allocation state is restored

Example of Banker’s Algorithm

1. 5 processes P0 through P4; 3 resource types A (10 instances), B (5instances, and C (7

instances).

2. Snapshot at time T0:

The content of the matrix. Need is defined to be Max – Allocation.

The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria.

Page 14: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 14

6. (i) What is a Gantt chart? Explain how it is used?

(ii) Consider the following set of processes, with the length of the CPU-burst time given

in milliseconds:

Process Burst Time Priority

P1 10 3

P2 1 1

P3 2 3

P4 1 4

P5 5 2

The processes are arrived in the order P1, P2, P3, P4, P5, ALL AT TIME 0.

(1) Draw four Gantt charts illustrating the execution of these processes using FCFS,

SJF, a non preemptive priority (a smaller priority number implies a higher priority), and

RR (quantum=1) scheduling.

(2) What is the turnaround time of each process for each of the scheduling algorithms in

part a?

(3) What is the waiting time of each process for each of the scheduling algorithms in

part a?

(4) Which of the schedules in part a results in the minimal average waiting time (overall

processes)? (16) (AUC MAY/JUNE

2012)

a)FCFS algorithm

P1 P2 P3 P4 P5

0 10 11 13 14 19

i)waiting time

process schedule time – arrival time = waiting time

p1 0 0 0

p2 10 0 10

p3 11 0 11

p4 13 0 13

p5 14 0 14

48

Average Waiting time=48/5=9.6

ii)Turn around time

process complete time – arrival time =Turnaround time

Page 15: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 15

p1 10 0 10

p2 11 0 11

p3 13 0 13

p4 14 0 14

p5 19 0 19

67

Average turn around time = 67/5 = 13.4

b)waiting time

process schedule time – arrival time = waiting time

p1 9 0 9

p2 0 0 0

p3 2 0 2

p4 1 0 1

p5 4 0 4

16

Average waiting time = 16/5 =3.2

ii)Turn around time

process completed time – arrival time = waiting time

p1 19 0 19

p2 1 0 1

p3 4 0 4

p4 2 0 2

p5 9 0 9

35

Average waiting time= 35/5 =7

c) Non pre emptive scheduling

P1 P5 P1 P3 P4

0 1 3 13 15 19

i)waiting time

Page 16: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 16

process schedule time – arrival time = waiting time

p1 3 0 3

p2 0 0 0

p3 13 0 13

p4 15 0 15

p5 1 0 14

32

Average waiting time = 32/5 =6.4

ii)Turn around time

process schedule time – arrival time = waiting time

p1 13 0 13

p2 1 0 1

p3 15 0 15

p4 19 0 19

p5 3 0 3

51

Average turn around time = 51/5 =10.2

d)Round Robin Scheduling

Page 17: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 17

Page 18: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 18

7. What do you mean by busy waiting? What other kinds of waiting are there? Can

busy waiting be avoided altogether? Explain your answer. (8) (AUC

MAY/JUNE 2012)

Busy waiting means that a process is waiting for a condition to be satisfied in a tight loop

without relinquish the processor. Alternatively,a process could wait by relinquishing the

processor, and block on acondition and wait to be awakened at some appropriate time in

thefuture. Busy waiting can be avoided but incurs the overhead associated with putting a

Page 19: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 19

process to sleep and having to wake it up when the appropriate program state is reached.

Page 20: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 20

8. Consider the following snapshot of a system:

Allocation Max Available

ABCD ABCD ABCD

P0 0012 0012 1520

P1 1000 1750

P2 1354 2356

P3 0632 0652

P4 0014 0656

Answer the following questions based on the bankers algorithm:

(1) Define safety algorithm.

(2) What is the content of the matrix need?

(3) Is the system in a safe state?

(4) If a request from process P1 arrives for (0, 4, 2, 0), can the request be granted

immediately? (AUC NOV 2010,

MAY2012)

Safety algorithm is used to find the state of the system: That is, system may be safe state or

unsafe state. Method for this is as follows:

1) Let work and finish be vector of length m and n respectively. Initialize work = Available and

Finish [i] = False for i = 1, 2, 3, 4, … n.

2) Find an i such that both

a) Finish [i] = False b) Need fj] < work

If no such i exist, go to step 4.

3) Work : = Work + Allocation i

Finish [i] = true to step 2

4) If Finish [i] = true for all i, then the system is in a safe state. Resource-request algorithm:

Let request, be the request vector for process P. If Request, fj] = k, then process P. wants k

instances of resource type R.. When a request for resources is made by process P, the

following actions are taken.

1) If Request. < Need(, then go to step 2. Otherwise, raise an editor condition since the

process has exceeded its maximum claim.

2) If Request < Available, then go to step 3. Otherwise, P. must wait since the resources are

not available.

3) Available : = Available – Request.; Allocation : = Allocation + Request.; Need; : = Needt –

Request.;

Page 21: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 21

Page 22: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 22

Page 23: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 23

9. What is critical section problem? Explain the two processes, multiple solutions.

Explain the Dining philosopher’s problem using semaphores. (AUCMAY

2006,NOV 2010)

1. n processes all competing to use some shared data

2. Each process has a code segment, called critical section, in which the shared data is

accessed.

3. Problem – ensure that when one process is executing in its critical section, no other process

is allowed to execute in its critical section.

Solution to Critical-Section Problem

Mutual Exclusion. If process Pi is executing in its critical section, then no other processes

canbe executing in their critical sections.

Progress. If no process is executing in its critical section and there exist some processes that

wish to enter their critical section, then the selection of the processes that will enter the critical

section next cannot be postponed indefinitely.

Bounded Waiting. A bound must exist on the number of times that other processes are

allowed to enter their critical sections after a process has made a request to enter its critical

section and before that request is granted.

Assume that each process executes at a nonzero speed

No assumption concerning relative speed of the n processes.

Initial Attempts to Solve Problem

Only 2 processes, P0 and P1

General structure of process Pi (other process Pj)

do {

entry section

critical section

exit section

reminder section

} while (1);

Processes may share some common variables to synchronize their actions.

Algorithm 1

Page 24: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 24

1. Shared variables:

int turn;

initially turn = 0

turn – i // Pi can enter its critical section

2. Process Pi

do {

while (turn != i) ;

critical section

turn = j; //reminder section

} while (1);

3. Satisfies mutual exclusion, but not progress

Algorithm 2

1) Shared variables

a) boolean flag[2];

initially flag [0] = flag [1] = false.

b) flag [i] = true => Pi ready to enter its critical section

2) Process Pi

do

{

flag[i] := true;

while (flag[j]) ;

//critical section

flag [i] = false;

} while (1);

i.remainder section

3) Satisfies mutual exclusion, but not progress requirement.

Algorithm 3

Page 25: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 25

Combined shared variables of algorithms 1 and 2. Process Pi

do {

flag [i]:= true;

turn = j;

while (flag [j] and turn = j);

critical section

flag [i] = false;

remainder section

} while (1);

Meets all three requirements; solves the critical-section problem for two processes.

Bakery Algorithm

Critical section for n processes

1) Before entering its critical section, process receives a number. Holder of the smallest

number

enters the critical section.

2) If processes Pi and Pj receive the same number, if i < j, then Pi is served first; else Pj is

served first.

3) The numbering scheme always generates numbers in increasing order of enumeration; i.e.,

1,2,3,3,3,3,4,5...

4) Notation <lexicographical order (ticket #, process id #)

a) (a,b) < c,d) if a < c or if a = c and b < d

b) max (a0,…, an-1) is a number, k, such that k

ai for i - 0, …, n – 1

5) Shared data

(a) boolean choosing[n];

(b) int number[n];

6) Data structures are initialized to false and 0 respectively

do {

choosing[i] = true;

Page 26: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 26

number*i+ = max(number*0+, number*1+, …, number *n – 1])+1;

choosing[i] = false;

for (j = 0; j < n; j++) {

while (choosing[j]) ;

while ((number[j] != 0) && (number[j,j] < number[i,i])) ;

}

critical section

number[i] = 0;

remainder section

} while (1);

Dining-Philosophers Problem

The dining philosopher’s problem is an example problem often used in concurrent algorithm

design to illustrate synchronization issues and techniques for resolving them.

IssuesThe problem was designed to illustrate the problem of avoiding deadlock, a system

state inwhich no progress is possible.

One idea is to instruct each philosopher to behave as follows:

Think until the left fork is available and when it is pick it up

Think until the right fork is available and when it is pick it up

Eat for a fixed amount of time

Put the right fork down

Put the left fork down

Repeat from the beginning.

This attempt at a solution fails: It allows the system to reach a deadlock state in which each

philosopher has picked up the fork to the left, waiting for the fork to the right to be put down—

which never happens, because

A) Each right fork is another philosopher's left fork, and no philosopher will put down that

forkuntil s/he eats, and

B) no philosopher can eat until s/he acquires the fork to his/her own right, which has

alreadybeen picked up by the philosopher to his/her right as described above.

wait(mutex);

readcount++;

Page 27: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 27

if (readcount == 1)

wait(rt);

signal(mutex);

reading is performed

… wait(mutex); readcount--;

if (readcount == 0)

signal(wrt);

signal(mutex):

The situation of the dining philosophers.

1. Shared data

semaphore chopstick[5];

Initially all values are 1

2. Philosopher i:

do {

Page 28: Sub Code:CS2257 Sub Name: Operating Systems Dept: CSE Sem ...mahalakshmiengineeringcollege.com/pdf/cse/IVsem/CS2254/UNIT 3.… · UNIT – II: PROCESS SCHEDULING AND SYNCHRONIZATION

A.JOYCE-AP/CSE - MAHALAKSHMI ENGINEERING COLLEGE Page 28

wait(chopstick[i])

wait(chopstick[(i+1) % 5])

eat

… signal(chopstick[i]); signal(chopstick[(i+1) % 5]);

think

} while (1);

10. Explain about the methods used to prevent deadlocks(8)

Restrain the ways request can be made.

1. Mutual Exclusion – not required for sharable resources; must hold for non-sharable

resources.

2. Hold and Wait – must guarantee that whenever a process requests a resource, it does not

hold any other resources.

Require process to request and be allocated all its resources before it begins

execution, or allow process to request resources only when the process has none.

Low resource utilization; starvation possible.

3. No Preemption – If a process that is holding some resources requests another resource

that cannot be immediately allocated to it, then all resources currently being held are released.

Preempted resources are added to the list of resources for which the process is waiting.

Process will be restarted only when it can regain its old resources, as well as the new ones that

it is requesting.

4. Circular Wait – impose a total ordering of all resource types, and require that each process

requests resources in an increasing order of enumeration.

B