Download presentation
Presentation is loading. Please wait.
1
G.Anuradha Reference: William Stallings
Process concurrency G.Anuradha Reference: William Stallings
2
Contents • Principles of concurrency • Mutual Exclusion-hardware approaches • Mutual Exclusion-software support • Semaphores • Monitors • Message Passing • Readers/Writers problem • Deadlock and starvation • Principles of deadlock • Deadlock prevention • Deadlock avoidance • Deadlock detection • An integrated Deadlock Strategy • Dining Philosophers Problem
3
Introduction Multiprogramming:- management of multiple processes within a set of processor system Multiprocessing : Management of multiple processes within a multiprocessor Distributed Processing: Management of multiple processes executing on multiple distributed computer system
4
Key terms related to concurrency
5
Concurrency What is it? Communication among processes
Sharing of and competing for resources Synchronization of activities of multiple processes Allocation of processor time to processes
6
When concurrency arises
Multiple applications: Structured applications Operating system structure
7
Race Conditions A Race Condition occurs, if two or more processes/threads access and manipulate the same data concurrently, and the outcome of the execution depends on the particular order in which the access takes place. Synchronization is needed to prevent race conditions from happening
8
Principles of concurrency
Problems encountered during concurrency Sharing of global resources is fraught with peril Difficulty in managing the allocation of resources optimally Difficulty in locating a programming error because results are typically not deterministic and reproducible
9
Problem can be solved by controlled access to shared resource
Simple example Echo program is loaded in global memory and shared by applications Problem can be solved by controlled access to shared resource
10
OS Concerns What design and management issues are raised by concurrency? OS should keep track of active processes OS should allocate and deallocate resources to active processes Processor time/memory/files/I-O devices OS should protect against the interference by other processes Result of a process should be independent of the speed of execution relative to other concurrent process (Process interaction)
11
Process interaction
12
Competition among Processes for Resources
3 control problems Mutual exclusion:- eg. Printer Mutual exclusion leads to two more additional problems Deadlock Starvation Mutual exclusion can be achieved by locking a resource prior to its use.
13
Mutual exclusion
17
Cooperation among processes by sharing
Eg:- shared variables/files/database Data items may be accessed in reading and writing mode and only the writing mode must be mutually exclusive Requirement: data coherence
18
Cooperation among processes by Communication
Communication provides a way to synchronize or coordinate the various activities This is done by messaging. So Mutual exclusion is not a control requirement for this sort of cooperation Has deadlock and starvation problems
19
Requirements of mutual exclusion(ME)
ME should be enforced A process that halts in its noncritical section should not interfere with other processes No deadlock and starvation When no process is there in the CS, a process requiring CS should be granted permission Process remains in CS only for finite time
20
Ways to arrive at mutual exclusion
Software approaches Leave the responsibility to the processes that wish to execute concurrently. Disadv is high processing overhead and bugs Hardware approaches Special purpose machine instructions Adv of reducing overhead Some level of support within the OS or programming language Semaphores monitors
22
First Attempt Buzy Waiting : waiting process repeatedly reads the value of turn(global memory location) until its allowed to enter its critical section Disadvantage:- Pace of execution is dictated by the slower of the two processes If one process fails the other process is permanently blocked.
23
Second attempt If one process fails outside the critical section, other process is not blocked. But if it fails within the critical section or after setting the flag then it blocks the other process
24
Third attempt Eliminates the problems in second attempt. This guarantees mutual exclusion but creates deadlock, because each process can insist on its right to enter its critical section.
25
Livelock The process keeps setting and resetting the flags alternatively and gets neither process could enter its critical section. Alteration in the relative speed of the two processes will break this cycle and allow one to enter the critical section This is called as livelock
26
Peterson’s Solution Two process solution
The two processes share two variables: int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical section. The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready!
27
Algorithm for Process Pi
31
Mutual exclusion-Hardware approach
Interrupt Disabling Special machine instructions Compare and swap exchange
32
Interrupt Disabling In an uniprocessor system concurrent processes can have only interleaved execution Process runs until its interrupted To guarantee ME its enough to prevent a process from being interrupted Disadvantages Degree of interleaving is limited Does not work in a multiprocessor architecture
33
Special machine instructions
Processor designers have proposed several machine instructions that carry out two actions automatically(Read-Write/Read-Test) through a single instruction fetch cycle Two such instruction Compare and swap Exchange instruction
34
Compare and swap Checks a memory location (*word) against a test value(testval). If memory location currval=testval, its replaced by newval Otherwise left unchanged
35
Compare and swap Shared variable bolt is initialized to 0
The only process that can enter critical section is one that finds bolt equal to 0 All other processes would go to buzy waiting mode
36
Exchange
37
Exchange contd… Shared variable bolt is initialized to zero
Each process has a local variable key that is initialized to 1 Process that find bolt =0 alone enters the critical section Excludes all other processes by setting bolt=1 Once on exiting it resend bold to 0
38
Properties of machine-instruction approroach
39
Disadvantage Buzy waiting is employed Starvation is possible
Deadlock is possible
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.