QUESTION BANK. UNIT II: PROCESS SCHEDULING AND SYNCHRONIZATION PART A (2 Marks)

Similar documents
Maximum CPU utilization obtained with multiprogramming. CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait

Silberschatz, Galvin and Gagne 2013! CPU cycles, memory space, I/O devices! " Silberschatz, Galvin and Gagne 2013!

Process Synchronization

Chapter 7: Process Synchronization. Background

The Deadlock Problem (1)

Chapter 7: Deadlocks

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter 7: Deadlocks

Chapter 7: Process Synchronization. Background. Illustration

Chapter 8: Deadlocks

Chapter 8: Deadlocks. The Deadlock Problem. System Model. Bridge Crossing Example. Resource-Allocation Graph. Deadlock Characterization

The Deadlock Problem

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

Chapter 8: Deadlocks. The Deadlock Problem

Module 7: Deadlocks. The Deadlock Problem. Bridge Crossing Example. System Model

CMSC 412. Announcements

UNIT-5 Q1. What is deadlock problem? Explain the system model of deadlock.

Chapter 7: Deadlocks. Operating System Concepts 8 th Edition,

Chapter 7: Deadlocks. Operating System Concepts 8 th Edition,! Silberschatz, Galvin and Gagne 2009!

Deadlocks. Deadlock Overview

Chapter 7: Deadlocks. Operating System Concepts 9th Edition DM510-14

Deadlock. Concepts to discuss. A System Model. Deadlock Characterization. Deadlock: Dining-Philosophers Example. Deadlock: Bridge Crossing Example

Chapter 7: Deadlocks. Operating System Concepts 8th Edition, modified by Stewart Weiss

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition! Silberschatz, Galvin and Gagne 2013!

The Slide does not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams.

Chapter 7: Deadlocks. Chapter 7: Deadlocks. The Deadlock Problem. Chapter Objectives. System Model. Bridge Crossing Example

Chapter 7: Deadlocks. Operating System Concepts with Java 8 th Edition

Chapter 7 : 7: Deadlocks Silberschatz, Galvin and Gagne 2009 Operating System Concepts 8th Edition, Chapter 7: Deadlocks

System Model. Types of resources Reusable Resources Consumable Resources

Deadlocks. Prepared By: Kaushik Vaghani

Chapter 7: Deadlocks

Chapter 7: Deadlocks

Lecture 7 Deadlocks (chapter 7)

Background. Module 6: Process Synchronization. Bounded-Buffer (Cont.) Bounded-Buffer. Background

OPERATING SYSTEMS. Deadlocks

CS307 Operating Systems Deadlocks

Deadlocks. Bridge Crossing Example. The Problem of Deadlock. Deadlock Characterization. Resource-Allocation Graph. System Model

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

Principles of Operating Systems

Chapter 8: Deadlocks. Operating System Concepts with Java

CSE Opera+ng System Principles

Process Coordination

Module 7: Deadlocks. The Deadlock Problem

Principles of Operating Systems

Deadlock Prevention. Restrain the ways request can be made. Mutual Exclusion not required for sharable resources; must hold for nonsharable resources.

CS420: Operating Systems. Deadlocks & Deadlock Prevention

COP 4610: Introduction to Operating Systems (Spring 2016) Chapter 7 Deadlocks. Zhi Wang Florida State University

Operating Systems Antonio Vivace revision 4 Licensed under GPLv3

PESIT Bangalore South Campus

Lecture 3: Synchronization & Deadlocks

CS307: Operating Systems

CHAPTER 7 - DEADLOCKS

Module 7: Deadlocks. System Model. Deadlock Characterization. Methods for Handling Deadlocks. Deadlock Prevention. Deadlock Avoidance

ICS Principles of Operating Systems. Lectures Set 5- Deadlocks Prof. Nalini Venkatasubramanian

Deadlock Risk Management

Deadlock Risk Management

Module 6: Process Synchronization

The Deadlock Problem

Process Synchronization

Operating Systems 2015 Spring by Euiseong Seo DEAD LOCK

Deadlocks. Dr. Yingwu Zhu

CHAPTER 7: DEADLOCKS. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Processes-Process Concept:

Chapter 7: Process Synchronization!

UNIT:2. Process Management

Chapter 7: Deadlocks. Operating System Concepts 8 th Edition,

Chapter 6: Process Synchronization

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Module 6: Deadlocks. Reading: Chapter 7

Deadlocks. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

University of Babylon / College of Information Technology / Network Department. Operating System / Dr. Mahdi S. Almhanna & Dr. Rafah M.

Chapter 7: Deadlocks CS370 Operating Systems

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Process Synchronisation (contd.) Deadlock. Operating Systems. Spring CS5212

OPERATING SYSTEMS. Prescribed Text Book. Operating System Principles, Seventh Edition. Abraham Silberschatz, Peter Baer Galvin and Greg Gagne

Bridge Crossing Example

Deadlock. Chapter Objectives

The deadlock problem

The Deadlock Problem

Deadlocks. Operating System Concepts - 7 th Edition, Feb 14, 2005

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Fall 2015 COMP Operating Systems. Lab 06

Unit-03 Deadlock and Memory Management Unit-03/Lecture-01

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

The Deadlock Problem. A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set.

Roadmap. Deadlock Prevention. Deadlock Prevention (Cont.) Deadlock Detection. Exercise. Tevfik Koşar. CSE 421/521 - Operating Systems Fall 2012

Chapter 8: Deadlocks. The Deadlock Problem

The Deadlock Problem. Chapter 8: Deadlocks. Bridge Crossing Example. System Model. Deadlock Characterization. Resource-Allocation Graph

CHAPTER 6: PROCESS SYNCHRONIZATION

Interprocess Communication By: Kaushik Vaghani

Lesson 6: Process Synchronization

Operating Systems: Quiz2 December 15, Class: No. Name:

Deadlocks. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

CS370 Operating Systems

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Chapter 7: Deadlocks. Operating System Concepts 9 th Edition

Transcription:

QUESTION BANK DEPARTMENT: EEE SEMESTER VII SUBJECT CODE: CS2411 SUBJECT NAME: OS 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. No. This follows directly from the hold-and-wait condition. 4. What are conditions under which a deadlock situation may arise? (AUC NOV2008) 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? (AUC NOV 2013) 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 MAHALAKSHMI ENGINEERING COLLEGE Page 1

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) 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) 1. Ensure that the system will never enter a deadlock state. 2. Allow the system to enter a deadlock state and then recover. 3. 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? 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? turn around 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. MAHALAKSHMI ENGINEERING COLLEGE Page 2

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. 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? (AUC NOV 2013) 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. MAHALAKSHMI ENGINEERING COLLEGE Page 3

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. 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 Suppose that the processes arrive in the order P2, P3, P1..n The Gantt chart for the schedule is: MAHALAKSHMI ENGINEERING COLLEGE Page 4

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 toschedule 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 Example of Preemptive SJF 2. SJF (preemptive) 4. Average waiting time = (9 + 1 + 0 +2)/4 3 MAHALAKSHMI ENGINEERING COLLEGE Page 5

Determining Length of Next CPU Burst 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. 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. 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. MAHALAKSHMI ENGINEERING COLLEGE Page 6

directed edge 1P Rj directed edge Rj Pi Pi requests instance of Rj Pi is holding an instance of Rj 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. MAHALAKSHMI ENGINEERING COLLEGE Page 7

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. 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 MAHALAKSHMI ENGINEERING COLLEGE Page 8

Define a semaphore as a record typedef struct { int value; struct process *L; } semaphore; 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 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 MAHALAKSHMI ENGINEERING COLLEGE Page 9

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. 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. MAHALAKSHMI ENGINEERING COLLEGE Page 10

The system is in a safe state since the sequence < P1, P3, P4, P2, P0> satisfies safety criteria. 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 MAHALAKSHMI ENGINEERING COLLEGE Page 11

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 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 MAHALAKSHMI ENGINEERING COLLEGE Page 12

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 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 MAHALAKSHMI ENGINEERING COLLEGE Page 13

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) 2012) (AUC MAY/JUNE Busy waiting means that a process is waiting for a condition tobe 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 associatedwith putting a process to sleep and having to wake it up when theappropriate program state is reached. To implement the wait() and signal() semaphore operations in multiprocessor environments using the TestAndSet() instruction. The solution should exhibit minimal busy waiting. MAHALAKSHMI ENGINEERING COLLEGE Page 14

Answer: Here is the pseudocode for implementing the operations: int guard = 0; int semaphore value = 0; wait() { while (TestAndSet(&guard) == 1); if (semaphore value == 0) { atomically add process to a queue of processes waiting for the semaphore and set guard to 0; } else { semaphore value--; guard = 0; } } signal() { while (TestAndSet(&guard) == 1); if (semaphore value == 0 && there is a process on the wait queue) wake up the first process in the queue of waiting processes else semaphore value++; guard = 0; } 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) MAHALAKSHMI ENGINEERING COLLEGE Page 15

(b) The system is in a safe state. For example: P0, P1, P3, P4, is a safe sequence. (c) After satisfying P1 request, the system becomes the following state. By using the safety algorithm, the system is still in a safe state and P0, P2, P1, P3, P4 is a safe sequence. 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. MAHALAKSHMI ENGINEERING COLLEGE Page 16

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 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); MAHALAKSHMI ENGINEERING COLLEGE Page 17

i.remainder section 3) Satisfies mutual exclusion, but not progress requirement. Algorithm 3 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; 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 MAHALAKSHMI ENGINEERING COLLEGE Page 18

} 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++; if (readcount == 1) wait(rt); signal(mutex); reading is performed wait(mutex); readcount--; if (readcount == 0) signal(wrt); signal(mutex): MAHALAKSHMI ENGINEERING COLLEGE Page 19

The situation of the dining philosophers. 1. Shared data semaphore chopstick[5]; Initially all values are 1 2. Philosopher i: do { 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. MAHALAKSHMI ENGINEERING COLLEGE Page 20

Low resource utilization; starvation possible. 3. No Preemption If a process that is holding some resources requests another resource thatcannot 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 MAHALAKSHMI ENGINEERING COLLEGE Page 21