Transcript
8/3/2019 Process syncronization
1/27
Operating System
8/3/2019 Process syncronization
2/27
Operating System
8/3/2019 Process syncronization
3/27
Topics Covered
Background Cooperating Processes Race Condition The Critical-Section Problem Synchronization Hardware Synchronization Software
Petersons Semaphores
Synchronization Examples
Presentated By: Rabia Javed 3
OPERATING SYSTEM
Synchronization
8/3/2019 Process syncronization
4/27
Objective To introduce the critical section problem, whose
solutions can be used to ensure the consistency ofshared data.
To present both software and hardware solution ofcritical section problem.
4Presentated By: Rabia Javed
8/3/2019 Process syncronization
5/27
Background
A B C
BA ACB C BMultiprogramming
ABC
Multiprocessing
Concurrent access to shared data may result in data inconsistency.Maintaining data consistency requires mechanisms to ensure the orderlyexecution of cooperating processes.Example:
Producer-ConsumerBounded-Buffer Scheme
8/3/2019 Process syncronization
6/27
Concurrent Processes
Concurrent processing is a computing
model in which multiple processorsexecute instructions simultaneously
for better performance.
Concurrent Processes can be
Independent processesCannot affect or be affected by the execution of another
process.Cooperating processes
Can affect or be affected by the execution of another process.
Advantages of process cooperation:
Information sharingComputation speedupConvenience(e.g. editing, printing, compiling)
6Presentated By: Rabia Javed
8/3/2019 Process syncronization
7/27
Race Condition Race condition: The situation where several processes access and
manipulate shared data concurrently. The final value of the shared datadepends upon the order of process accessing the data.
To prevent race conditions, concurrent processes must besynchronized.
e.g. The statements
count++;count--;
must be performed atomically.
Atomic operation means an operation that completes in its entiretywithout interruption.
8/3/2019 Process syncronization
8/27
Concurrent Processes: Basic Issues
Concurrency Problems can arise at the program execution level.
Presentated By: Rabia Javed 8
Example: Suppose two processes, P1 and P2 share variables aand b.
Assume a = 7, b = 2 initially.
P1:a = a + 1;b = b + 1;
P2:b = b * 2;a = a * 2;
Question:What are the possible ending values for a and b if P1and P2 execute concurrently?
8/3/2019 Process syncronization
9/27
Critical Section In concurrent programming a critical section is a piece
of code that accesses a shared resource (data structureor device) that must not be concurrently accessed bymore than one thread of execution.
A critical section will usually terminate in fixed time,
and a thread, task or process will have to wait a fixedtime to enter it.
Presentated By: Rabia Javed 9
8/3/2019 Process syncronization
10/27
Critical SectionsA section of code, common to n cooperating processes, in which the processesmay be accessing common variables.
A Critical Section Environment contains:
Entry Section Code requesting entry into the critical section.
Critical Section Code in which only one process can execute at any one time.
Exit Section The end of the critical section, releasing or allowing othersin.
Remainder Section Rest of the code AFTER the critical section.
Presentated By: Rabia Javed 10
8/3/2019 Process syncronization
11/27
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
remainder section}while (1);
Processes may share some common variables tosynchronize their actions.
8/3/2019 Process syncronization
12/27
The Critical Section Problem Consider a system with n processes. Each process
has a segment of code, called a critical section.
When one process is executing in the criticalsection, no other process is to be allowed in itscritical section.
The critical section problem is to design a protocol
that the process can use to cooperate.
12Presentated By: Rabia Javed
8/3/2019 Process syncronization
13/27
The Critical Section Requirements
Mutual ExclusionIf process P is executing in its critical section,then no other processes can be executed in their critical section.
Progress a process in his reminder section cant participate in thedecision of which will enter its critical section next , and this selection cannotbe postponed indefinitely.
Bounded Waiting There exist a bound on the number of timesthat other processes are allowed to enter their critical section after a process hasmade a request to enter his critical section and before that request is granted.
13Presentated By: Rabia Javed
8/3/2019 Process syncronization
14/27
Critical Section Solutions Hardware Solution:
Many systems provide hardware support for criticalsection code
Uni-processors -> Could disable interrupts
Currently running code would execute without preemption
Generally too inefficient on multiprocessor systems
Modern machines provide special atomic(noninterruptsable) hardware instruction.
Presentated By: Rabia Javed 14
8/3/2019 Process syncronization
15/27
Critical Section Solutions
Software Solution:
Petersons solution Semaphore
Presentated By: Rabia Javed 15
8/3/2019 Process syncronization
16/27
PROCESS SYNCHRONIZATION
Here we try a succession of increasingly complicated solutions to the problemof creating valid entry sections.
NOTE: In all examples, i is the current process, j the "other" process. Inthese examples, envision the same code running on two processors at the sametime.
TOGGLED ACCESS:
Presentated By: Rabia Javed 16
do {
while ( turn ^= i );/* critical section */turn = j;/* remainder section */
} while(TRUE);
Two Processes
Software
Are the three Critical SectionRequirements Met?
Algorithm 1
8/3/2019 Process syncronization
17/27
PROCESS SYNCHRONIZATION FLAG FOR EACH PROCESS GIVES STATE:
Each process maintains a flag indicating that it wants to get into the criticalsection. It checks the flag of the other process and doesnt enter the criticalsection if that other process wants to get in.
Presentated By: Rabia Javed 17
Are the three CriticalSection Requirements
Met?
Two Processes
Software
do {
flag[i] := true;while (flag[j]) ;critical sectionflag [i] = false;remainder section
} while (1);
Algorithm 2Shared variablesboolean flag[2];
initially flag [0] = flag [1] = false.flag [i] = truePi ready to enter its critical section
8/3/2019 Process syncronization
18/27
PROCESS SYNCHRONIZATION FLAG TO REQUEST ENTRY:
Each processes sets a flag to request entry. Then each process toggles a bit toallow the other in first.
This code is executed for each process i.
Presentated By: Rabia Javed 18
Are the three CriticalSection Requirements Met?
Two Processes
Software
do {
flag [i]:= true;turn = j;while (flag [j] and turn == j) ;critical sectionflag [i] = false;remainder section
} while (1);
Algorithm 3Shared variablesboolean flag[2];
initially flag [0] = flag [1] = false.flag [i] = trueP
iready to enter its critical section
This is PetersonsSolution
8/3/2019 Process syncronization
19/27
Semaphores (OS Support) A flag used to indicate that a routine cannot proceed if a
shared resource is already in use by another routine.
The allowable operations on a semaphore are V(signal)and P(wait) both are atomic operations.
Two types of semaphore:
Counting
Binary (mutex locks)
Semaphore is a variable that has an integer value May be initialized to a nonnegative number
Wait operation decrements the semaphore value
Signal operation increments semaphore value
19Presentated By: Rabia Javed
8/3/2019 Process syncronization
20/27
Semaphores Like Integers Except Operations must be atomic
Two Ps together cant decrement value below zero Similarly, thread going to sleep in P wont miss wakeup from V even if they both
happen at same time
Semaphore from railway analogy
Here is a semaphore initialized to 2 for resource control:
Value= 210
8/3/2019 Process syncronization
21/27
Semaphore Implementation with
no Busy waitingWith each semaphore there is an associated waiting
queue.
Two operations: block place the process invoking the operation on the
appropriate waiting queue.
wakeup remove one of processes in the waiting queueand place it in the ready queue.
8/3/2019 Process syncronization
22/27
Advantages of Semaphores Powerful--Can solve a variety of synchronization
problems
Simple solution for multiple processes. Solution exists for multiple processors (not as simple).
No busy waiting.
No starvation.
8/3/2019 Process syncronization
23/27
What is the Problem here?
Semaphores provide a powerful synchronization tool, but:
wait() and signal() operations are scattered amongseveral processes. It is difficult to understand their effects.
Usage must be correct in all the processes. One bad process (i.e. one programming error) can kill whole
system.
Wrong initialization or placement of wait and signal
May cause following problems: Violation of M.E.
Dead Lock.
Starvation.
8/3/2019 Process syncronization
24/27
Violation of Mutual Exclusion By mistake the programmer has placed the signal operation beforethe wait operation in P0.
S is initialized to 1.
Let P1 executes first, decrements s to 0 and enter it CS.
Let P0 now executes, and instead of a wait gives a signal to s,increments s to 1 and enter it Cs.
Both p0 and p1 are in Cs (M.E violated)
P0 P1
signal(s); wait(s);C.S
wait(s); signal(s);
C.S
8/3/2019 Process syncronization
25/27
Dead LockA set of processes are said to be in a state of dead lock,
if every process is waiting for an event that can becaused only by another process in the set.
P0 P1
wait(s1); wait(s2);
wait(s2); wait(s1);
.. .
signal(s1); signal(s2);
signal(s2); signal(s1);
8/3/2019 Process syncronization
26/27
Synchronization Examples
OSUni-Processor Systems Multi-Processor Systems
Window XpNon-preemptive
(disable Interrupt)
Spin Locks
Mutexes
Semaphores
SolarisPreemptive Semaphores, condition variables, adaptive
Mutexes
Locks/mutex
LinuxDisables interrupts(version 2.6)
Fully preemptive(later version 2.6)
Semaphores
Spin locks
Mac OS
X/DarwinPreemptible Mach mutexes at the BSD layer
29Presentated By: Rabia Javed
8/3/2019 Process syncronization
27/27
Queries???
Presentated By: Rabia Javed 30
top related