Chapter 6: ProcessChapter 6: ProcessSynchronizationSynchronization
6.2 Silberschatz, Galvin and GagneOperating System ConceptsModule 6: Process SynchronizationModule 6: Process Synchronizat...
6.3 Silberschatz, Galvin and GagneOperating System ConceptsBackgroundBackgroundConcurrent access to shared data may result...
6.4 Silberschatz, Galvin and GagneOperating System ConceptsProducerProducerwhile (true)/* produce an item and put in nextP...
6.5 Silberschatz, Galvin and GagneOperating System ConceptsConsumerConsumerwhile (1){while (count == 0); // do nothingnext...
6.6 Silberschatz, Galvin and GagneOperating System ConceptsRace ConditionRace Conditioncount++ could be implemented asregi...
6.7 Silberschatz, Galvin and GagneOperating System ConceptsSolution to Critical-Section ProblemSolution to Critical-Sectio...
6.8 Silberschatz, Galvin and GagneOperating System ConceptsPeterson’s SolutionPeterson’s SolutionTwo process solutionAssum...
6.9 Silberschatz, Galvin and GagneOperating System ConceptsAlgorithm for ProcessAlgorithm for Process PPiido {flag[i] = TR...
6.10 Silberschatz, Galvin and GagneOperating System ConceptsSynchronization HardwareSynchronization HardwareMany systems p...
6.11 Silberschatz, Galvin and GagneOperating System ConceptsTestAndndSet InstructionTestAndndSet InstructionDefinition:boo...
6.12 Silberschatz, Galvin and GagneOperating System ConceptsSolution using TestAndSetSolution using TestAndSetShared boole...
6.13 Silberschatz, Galvin and GagneOperating System ConceptsSwap InstructionSwap InstructionDefinition:void Swap (boolean ...
6.14 Silberschatz, Galvin and GagneOperating System ConceptsSolution using SwapSolution using SwapShared Boolean variable ...
6.15 Silberschatz, Galvin and GagneOperating System ConceptsSemaphoreSemaphoreSynchronization tool that does not require b...
6.16 Silberschatz, Galvin and GagneOperating System ConceptsSemaphore as General Synchronization ToolSemaphore as General ...
6.17 Silberschatz, Galvin and GagneOperating System ConceptsSemaphore ImplementationSemaphore ImplementationMust guarantee...
6.18 Silberschatz, Galvin and GagneOperating System ConceptsSemaphore Implementation with no Busy waitingSemaphore Impleme...
6.19 Silberschatz, Galvin and GagneOperating System ConceptsSemaphore Implementation with no Busy waitingSemaphore Impleme...
6.20 Silberschatz, Galvin and GagneOperating System ConceptsDeadlock and StarvationDeadlock and StarvationDeadlock – two o...
6.21 Silberschatz, Galvin and GagneOperating System ConceptsClassical Problems ofClassical Problems ofSynchronizationSynch...
6.22 Silberschatz, Galvin and GagneOperating System ConceptsBounded-Buffer ProblemBounded-Buffer ProblemN buffers, each ca...
6.23 Silberschatz, Galvin and GagneOperating System ConceptsBounded Buffer Problem (Cont.)Bounded Buffer Problem (Cont.)Th...
6.24 Silberschatz, Galvin and GagneOperating System ConceptsBounded Buffer Problem (Cont.)Bounded Buffer Problem (Cont.)Th...
6.25 Silberschatz, Galvin and GagneOperating System ConceptsReaders-Writers ProblemReaders-Writers ProblemA data set is sh...
6.26 Silberschatz, Galvin and GagneOperating System ConceptsReaders-Writers Problem (Cont.)Readers-Writers Problem (Cont.)...
6.27 Silberschatz, Galvin and GagneOperating System ConceptsReaders-Writers Problem (Cont.)Readers-Writers Problem (Cont.)...
6.28 Silberschatz, Galvin and GagneOperating System ConceptsDining-Philosophers ProblemDining-Philosophers ProblemShared d...
6.29 Silberschatz, Galvin and GagneOperating System ConceptsDining-Philosophers Problem (Cont.)Dining-Philosophers Problem...
6.30 Silberschatz, Galvin and GagneOperating System ConceptsProblems with SemaphoresProblems with SemaphoresCorrect use of...
6.31 Silberschatz, Galvin and GagneOperating System ConceptsMonitorsMonitorsA high-level abstraction that provides a conve...
6.32 Silberschatz, Galvin and GagneOperating System ConceptsSchematic view of a MonitorSchematic view of a Monitor
6.33 Silberschatz, Galvin and GagneOperating System ConceptsCondition VariablesCondition Variablescondition x, y;Two opera...
6.34 Silberschatz, Galvin and GagneOperating System ConceptsMonitor with Condition VariablesMonitor with Condition Variables
6.35 Silberschatz, Galvin and GagneOperating System ConceptsSolution to Dining PhilosophersSolution to Dining Philosophers...
6.36 Silberschatz, Galvin and GagneOperating System ConceptsSolution to Dining Philosophers (cont)Solution to Dining Philo...
6.37 Silberschatz, Galvin and GagneOperating System ConceptsSynchronization ExamplesSynchronization ExamplesSolarisWindows...
6.38 Silberschatz, Galvin and GagneOperating System ConceptsSolaris SynchronizationSolaris SynchronizationImplements a var...
6.39 Silberschatz, Galvin and GagneOperating System ConceptsWindows XP SynchronizationWindows XP SynchronizationUses inter...
6.40 Silberschatz, Galvin and GagneOperating System ConceptsLinux SynchronizationLinux SynchronizationLinux:disables inter...
6.41 Silberschatz, Galvin and GagneOperating System ConceptsPthreads SynchronizationPthreads SynchronizationPthreads API i...
End of Chapter 6End of Chapter 6
Upcoming SlideShare
Loading in...5
×

6.Process Synchronization

1,233

Published on

Senthilkanth,MCA..
The following ppt's full topic covers Operating System for BSc CS, BCA, MSc CS, MCA students..
1.Introduction
2.OS Structures
3.Process
4.Threads
5.CPU Scheduling
6.Process Synchronization
7.Dead Locks
8.Memory Management
9.Virtual Memory
10.File system Interface
11.File system implementation
12.Mass Storage System
13.IO Systems
14.Protection
15.Security
16.Distributed System Structure
17.Distributed File System
18.Distributed Co Ordination
19.Real Time System
20.Multimedia Systems
21.Linux
22.Windows

Published in: Education, Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,233
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
125
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

6.Process Synchronization

  1. 1. Chapter 6: ProcessChapter 6: ProcessSynchronizationSynchronization
  2. 2. 6.2 Silberschatz, Galvin and GagneOperating System ConceptsModule 6: Process SynchronizationModule 6: Process SynchronizationBackgroundThe Critical-Section ProblemPeterson’s SolutionSynchronization HardwareSemaphoresClassic Problems of SynchronizationMonitorsSynchronization ExamplesAtomic Transactions
  3. 3. 6.3 Silberschatz, Galvin and GagneOperating System ConceptsBackgroundBackgroundConcurrent access to shared data may result in datainconsistencyMaintaining data consistency requires mechanisms toensure the orderly execution of cooperating processesSuppose that we wanted to provide a solution to theconsumer-producer problem that fills all the buffers. Wecan do so by having an integer count that keeps track ofthe number of full buffers. Initially, count is set to 0. It isincremented by the producer after it produces a newbuffer and is decremented by the consumer after itconsumes a buffer.
  4. 4. 6.4 Silberschatz, Galvin and GagneOperating System ConceptsProducerProducerwhile (true)/* produce an item and put in nextProducedwhile (count == BUFFER_SIZE); // do nothingbuffer [in] = nextProduced;in = (in + 1) % BUFFER_SIZE;count++;}
  5. 5. 6.5 Silberschatz, Galvin and GagneOperating System ConceptsConsumerConsumerwhile (1){while (count == 0); // do nothingnextConsumed = buffer[out];out = (out + 1) % BUFFER_SIZE;count--;/* consume the item in nextConsumed}
  6. 6. 6.6 Silberschatz, Galvin and GagneOperating System ConceptsRace ConditionRace Conditioncount++ could be implemented asregister1 = countregister1 = register1 + 1count = register1count-- could be implemented asregister2 = countregister2 = register2 - 1count = register2Consider this execution interleaving with “count = 5” initially:S0: producer execute register1 = count {register1 = 5}S1: producer execute register1 = register1 + 1 {register1 = 6}S2: consumer execute register2 = count {register2 = 5}S3: consumer execute register2 = register2 - 1 {register2 = 4}S4: producer execute count = register1 {count = 6 }S5: consumer execute count = register2 {count = 4}
  7. 7. 6.7 Silberschatz, Galvin and GagneOperating System ConceptsSolution to Critical-Section ProblemSolution to Critical-Section Problem1. Mutual Exclusion - If process Pi is executing in its critical section,then no other processes can be executing in their critical sections2. Progress - If no process is executing in its critical section andthere exist some processes that wish to enter their critical section,then the selection of the processes that will enter the criticalsection next cannot be postponed indefinitely3. Bounded Waiting - A bound must exist on the number of timesthat other processes are allowed to enter their critical sectionsafter a process has made a request to enter its critical section andbefore that request is granted Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes
  8. 8. 6.8 Silberschatz, Galvin and GagneOperating System ConceptsPeterson’s SolutionPeterson’s SolutionTwo process solutionAssume that the LOAD and STORE instructions are atomic;that is, cannot be interrupted.The two processes share two variables:int turn;Boolean flag[2]The variable turn indicates whose turn it is to enter thecritical section.The flag array is used to indicate if a process is ready toenter the critical section. flag[i] = true implies that process Piis ready!
  9. 9. 6.9 Silberschatz, Galvin and GagneOperating System ConceptsAlgorithm for ProcessAlgorithm for Process PPiido {flag[i] = TRUE;turn = j;while ( flag[j] && turn == j);CRITICAL SECTIONflag[i] = FALSE;REMAINDER SECTION} while (TRUE);
  10. 10. 6.10 Silberschatz, Galvin and GagneOperating System ConceptsSynchronization HardwareSynchronization HardwareMany systems provide hardware support for critical sectioncodeUniprocessors – could disable interruptsCurrently running code would execute withoutpreemptionGenerally too inefficient on multiprocessor systems Operating systems using this not broadly scalableModern machines provide special atomic hardwareinstructions Atomic = non-interruptableEither test memory word and set valueOr swap contents of two memory words
  11. 11. 6.11 Silberschatz, Galvin and GagneOperating System ConceptsTestAndndSet InstructionTestAndndSet InstructionDefinition:boolean TestAndSet (boolean *target){boolean rv = *target;*target = TRUE;return rv:}
  12. 12. 6.12 Silberschatz, Galvin and GagneOperating System ConceptsSolution using TestAndSetSolution using TestAndSetShared boolean variable lock., initialized to false.Solution:do {while ( TestAndSet (&lock )); /* do nothing// critical sectionlock = FALSE;// remainder section} while ( TRUE);
  13. 13. 6.13 Silberschatz, Galvin and GagneOperating System ConceptsSwap InstructionSwap InstructionDefinition:void Swap (boolean *a, boolean *b){boolean temp = *a;*a = *b;*b = temp:}
  14. 14. 6.14 Silberschatz, Galvin and GagneOperating System ConceptsSolution using SwapSolution using SwapShared Boolean variable lock initialized to FALSE; Eachprocess has a local Boolean variable key.Solution:do {key = TRUE;while ( key == TRUE)Swap (&lock, &key );// critical sectionlock = FALSE;// remainder section} while ( TRUE);
  15. 15. 6.15 Silberschatz, Galvin and GagneOperating System ConceptsSemaphoreSemaphoreSynchronization tool that does not require busy waitingSemaphore S – integer variableTwo standard operations modify S: wait() and signal()Originally called P() and V()Less complicatedCan only be accessed via two indivisible (atomic) operationswait (S) {while S <= 0; // no-opS--;}signal (S) {S++;}
  16. 16. 6.16 Silberschatz, Galvin and GagneOperating System ConceptsSemaphore as General Synchronization ToolSemaphore as General Synchronization ToolCounting semaphore – integer value can range over an unrestricteddomainBinary semaphore – integer value can range only between 0and 1; can be simpler to implementAlso known as mutex locksCan implement a counting semaphore S as a binary semaphoreProvides mutual exclusionSemaphore S; // initialized to 1wait (S);Critical Sectionsignal (S);
  17. 17. 6.17 Silberschatz, Galvin and GagneOperating System ConceptsSemaphore ImplementationSemaphore ImplementationMust guarantee that no two processes can execute wait () andsignal () on the same semaphore at the same timeThus, implementation becomes the critical section problemwhere the wait and signal code are placed in the crtical section.Could now have busy waiting in critical sectionimplementation But implementation code is short Little busy waiting if critical section rarely occupiedNote that applications may spend lots of time in critical sectionsand therefore this is not a good solution.
  18. 18. 6.18 Silberschatz, Galvin and GagneOperating System ConceptsSemaphore Implementation with no Busy waitingSemaphore Implementation with no Busy waitingWith each semaphore there is an associated waiting queue.Each entry in a waiting queue has two data items:value (of type integer)pointer to next record in the listTwo operations:block – place the process invoking the operation on theappropriate waiting queue.wakeup – remove one of processes in the waiting queueand place it in the ready queue.
  19. 19. 6.19 Silberschatz, Galvin and GagneOperating System ConceptsSemaphore Implementation with no Busy waitingSemaphore Implementation with no Busy waiting(Cont.)(Cont.)Implementation of wait:wait (S){value--;if (value < 0) {add this process to waiting queueblock(); }}Implementation of signal:Signal (S){value++;if (value <= 0) {remove a process P from the waiting queuewakeup(P); }}
  20. 20. 6.20 Silberschatz, Galvin and GagneOperating System ConceptsDeadlock and StarvationDeadlock and StarvationDeadlock – two or more processes are waiting indefinitely for anevent that can be caused by only one of the waiting processesLet S and Q be two semaphores initialized to 1P0 P1wait (S); wait (Q);wait (Q); wait (S);. .. .. .signal (S); signal (Q);signal (Q); signal (S);Starvation – indefinite blocking. A process may never be removedfrom the semaphore queue in which it is suspended.
  21. 21. 6.21 Silberschatz, Galvin and GagneOperating System ConceptsClassical Problems ofClassical Problems ofSynchronizationSynchronizationBounded-Buffer ProblemReaders and Writers ProblemDining-Philosophers Problem
  22. 22. 6.22 Silberschatz, Galvin and GagneOperating System ConceptsBounded-Buffer ProblemBounded-Buffer ProblemN buffers, each can hold one itemSemaphore mutex initialized to the value 1Semaphore full initialized to the value 0Semaphore empty initialized to the value N.
  23. 23. 6.23 Silberschatz, Galvin and GagneOperating System ConceptsBounded Buffer Problem (Cont.)Bounded Buffer Problem (Cont.)The structure of the producer processdo {// produce an itemwait (empty);wait (mutex);// add the item to the buffersignal (mutex);signal (full);} while (true);
  24. 24. 6.24 Silberschatz, Galvin and GagneOperating System ConceptsBounded Buffer Problem (Cont.)Bounded Buffer Problem (Cont.)The structure of the consumer processdo {wait (full);wait (mutex);// remove an item from buffersignal (mutex);signal (empty);// consume the removed item} while (true);
  25. 25. 6.25 Silberschatz, Galvin and GagneOperating System ConceptsReaders-Writers ProblemReaders-Writers ProblemA data set is shared among a number of concurrent processesReaders – only read the data set; they do not perform anyupdatesWriters – can both read and write.Problem – allow multiple readers to read at the same time. Onlyone single writer can access the shared data at the same time.Shared DataData setSemaphore mutex initialized to 1.Semaphore wrt initialized to 1.Integer readcount initialized to 0.
  26. 26. 6.26 Silberschatz, Galvin and GagneOperating System ConceptsReaders-Writers Problem (Cont.)Readers-Writers Problem (Cont.)The structure of a writer processdo {wait (wrt) ;// writing is performedsignal (wrt) ;} while (true)
  27. 27. 6.27 Silberschatz, Galvin and GagneOperating System ConceptsReaders-Writers Problem (Cont.)Readers-Writers Problem (Cont.)The structure of a reader processdo {wait (mutex) ;readcount ++ ;if (readercount == 1) wait (wrt) ;signal (mutex)// reading is performedwait (mutex) ;readcount - - ;if redacount == 0) signal (wrt) ;signal (mutex) ;} while (true)
  28. 28. 6.28 Silberschatz, Galvin and GagneOperating System ConceptsDining-Philosophers ProblemDining-Philosophers ProblemShared dataBowl of rice (data set)Semaphore chopstick [5] initialized to 1
  29. 29. 6.29 Silberschatz, Galvin and GagneOperating System ConceptsDining-Philosophers Problem (Cont.)Dining-Philosophers Problem (Cont.)The structure of Philosopher i:Do {wait ( chopstick[i] );wait ( chopStick[ (i + 1) % 5] );// eatsignal ( chopstick[i] );signal (chopstick[ (i + 1) % 5] );// think} while (true) ;
  30. 30. 6.30 Silberschatz, Galvin and GagneOperating System ConceptsProblems with SemaphoresProblems with SemaphoresCorrect use of semaphore operations:signal (mutex) …. wait (mutex)wait (mutex) … wait (mutex)Omitting of wait (mutex) or signal (mutex) (or both)
  31. 31. 6.31 Silberschatz, Galvin and GagneOperating System ConceptsMonitorsMonitorsA high-level abstraction that provides a convenient and effectivemechanism for process synchronizationOnly one process may be active within the monitor at a timemonitor monitor-name{// shared variable declarationsprocedure P1 (…) { …. }…procedure Pn (…) {……}Initialization code ( ….) { … }…}}
  32. 32. 6.32 Silberschatz, Galvin and GagneOperating System ConceptsSchematic view of a MonitorSchematic view of a Monitor
  33. 33. 6.33 Silberschatz, Galvin and GagneOperating System ConceptsCondition VariablesCondition Variablescondition x, y;Two operations on a condition variable:x.wait () – a process that invokes the operation issuspended.x.signal () – resumes one of processes (if any) thainvoked x.wait ()
  34. 34. 6.34 Silberschatz, Galvin and GagneOperating System ConceptsMonitor with Condition VariablesMonitor with Condition Variables
  35. 35. 6.35 Silberschatz, Galvin and GagneOperating System ConceptsSolution to Dining PhilosophersSolution to Dining Philosophersmonitor DP{enum { THINKING; HUNGRY, EATING) state [5] ;condition self [5];void pickup (int i) {state[i] = HUNGRY;test(i);if (state[i] != EATING) self [i].wait;}void putdown (int i) {state[i] = THINKING;// test left and right neighborstest((i + 4) % 5);test((i + 1) % 5);}
  36. 36. 6.36 Silberschatz, Galvin and GagneOperating System ConceptsSolution to Dining Philosophers (cont)Solution to Dining Philosophers (cont)void test (int i) {if ( (state[(i + 4) % 5] != EATING) &&(state[i] == HUNGRY) &&(state[(i + 1) % 5] != EATING) ) {state[i] = EATING ;self[i].signal () ;}}initialization_code() {for (int i = 0; i < 5; i++)state[i] = THINKING;}}
  37. 37. 6.37 Silberschatz, Galvin and GagneOperating System ConceptsSynchronization ExamplesSynchronization ExamplesSolarisWindows XPLinuxPthreads
  38. 38. 6.38 Silberschatz, Galvin and GagneOperating System ConceptsSolaris SynchronizationSolaris SynchronizationImplements a variety of locks to support multitasking,multithreading (including real-time threads), and multiprocessingUses adaptive mutexes for efficiency when protecting data fromshort code segmentsUses condition variables and readers-writers locks when longersections of code need access to dataUses turnstiles to order the list of threads waiting to acquire eitheran adaptive mutex or reader-writer lock
  39. 39. 6.39 Silberschatz, Galvin and GagneOperating System ConceptsWindows XP SynchronizationWindows XP SynchronizationUses interrupt masks to protect access to global resources onuniprocessor systemsUses spinlocks on multiprocessor systemsAlso provides dispatcher objects which may act as either mutexesand semaphoresDispatcher objects may also provide eventsAn event acts much like a condition variable
  40. 40. 6.40 Silberschatz, Galvin and GagneOperating System ConceptsLinux SynchronizationLinux SynchronizationLinux:disables interrupts to implement short critical sectionsLinux provides:semaphoresspin locks
  41. 41. 6.41 Silberschatz, Galvin and GagneOperating System ConceptsPthreads SynchronizationPthreads SynchronizationPthreads API is OS-independentIt provides:mutex lockscondition variablesNon-portable extensions include:read-write locksspin locks
  42. 42. End of Chapter 6End of Chapter 6
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×