SlideShare a Scribd company logo
PROCESS SYNCHRONIZATION SONALI C. UDIT-SYBSc-IT 2008-09
INTRODUCTION Cooperating Processes is one that can be affected or will affect other process executing in the system. Concurrent access to share data may result in to data inconsistency So we will discuss mechanisms to  ensure the orderly   execution  of  cooperating   processes  that share logical address space, so that  consistency  is  maintained .
CRITICAL SECTION n  processes all competing to use some shared data Each process has a code segment, called  critical section , in which the shared data is accessed. Set of instructions that must be controlled so as to allow exclusive access to one process.  execution of the critical section by processes is mutually exclusive in time .
CRITICAL SECTION Do{ critical section reminder section }while(1) Each process must request permission to enter critical section –  ENTRY   SECTION Critical section may follow by  EXIT   SECTION . Remaining code is   REMAINDER   SECTION Cont… Entry section exit section GENERAL STRUCTURE  OF A TYPICAL PROCESS Pi
CRITICAL SECTION Solution to the Critical Section Problem must meet three conditions...   Mutual Exclusion  - If process  P i  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 Cont…
TWO-PROCESS SOLUTION Algorithm is only for 2 processes at a time Processes are P 0 P 1 Or can also be represented as P i  and P j  , i.e. j=1-i
TWO-PROCESS SOLUTION -Algorithm 1 If process  turn   == i  ,  P i  is allowed to execute in critical section But, Guarantees mutual exclusion.  Does not guarantee progress --- enforces strict alternation of processes entering CS's ( if P j  decides not to re-enter or crashes outside CS, then P i  cannot ever get in) .  Let the processes share common integer variable  turn Let  int turn =0  (or 1) Do{   Critical section remainder section }while(1) For Process P i while (turn != i); Turn = j;
TWO-PROCESS SOLUTION -Algorithm 2 Shared variables boolean flag[2];  // “interest” bits initially flag [0] = flag [1] = false. flag [i] = true    P i   declares   interest  in entering its critical section Process P i  // where the “other” process is P j do {   flag[i] = true;   // declare your own interest     while (flag[ j]) ;   //wait if the other guy is interested     critical section   flag [i] = false;   // declare that you lost interest   remainder section  // allows other guy to enter } while (1);
Satisfies mutual exclusion, but not progress requirement. If flag[ i]  == flag[ j] == true, then deadlock - no progress but barring this event, a non-CS guy cannot block you from entering  Can make consecutive re-entries to CS if other not interested TWO-PROCESS SOLUTION -Algorithm 2
Combined shared variables & approaches  of algorithms 1 and 2. TWO-PROCESS SOLUTION -Algorithm 3 For Process P i do  { flag [i] = true;  // declare your interest to enter turn = j;  // assume it is the other’s turn-give PJ a chance 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 ( the best of all worlds - almost!). Turn variable breaks any deadlock possibility of previous example, AND prevents “hogging” – P i  setting turn to j gives P J  a chance after each pass of P i ’s CS Flag[ ] variable prevents getting locked out if other guy never re-enters or crashes outside and allows CS consecutive access other not interested in entering. TWO-PROCESS SOLUTION -Algorithm 3
MULTIPLE-PROCESS SOLUTIONS - Bakery Algorithm Algo solves the critical-section problem for  n-process. Before entering its critical section, process receives a number. Holder of the smallest number enters the critical section. If processes  P i  and  P j  receive the same number, if  i  <  j , then  P i  is served first; else  P j  is served first. Since process name are unique The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5...
The common data structure are: boolean choosing[n]; Int number[n]; Initially,  boolean choosing[n]=false; int number[n]=o; Notation ( a,b ) <  c,d ) if  a  <  c  or if  a  =  c  and  b  <  d max ( a 0 ,…,  a n -1 ) is a number,  k , such that  k     a i     for  i  = 0, … ,  n  – 1 MULTIPLE-PROCESS SOLUTIONS - Bakery Algorithm
The structure of process P i  in the bakery algorithm is as follow… MULTIPLE-PROCESS SOLUTIONS - Bakery Algorithm
choosing[i] = true;  //process does not “compete” while choosing a # number[i] = max(number[0], number[1], …, number [n – 1]) + 1; choosing[i] = false; /*scan all processes to see if if Pi has lowest number: */ for (k = 0; k < n; k++) {  /* check  process k,  book uses dummy variable “j” */ while (choosing[ k ]);  /*if Pk is choosing a number wait till done */  while ((number[k] != 0) && (number[k], k < number[ i ], i)) ; /*if (Pk is waiting (or in CS) and Pk  is “ahead” of Pi then  Pi waits */ /*if Pk is not in CS and is not waiting, OR Pk is waiting with a larger number,  then skip over Pk  - when Pi gets to end of scan, it will have lowest number and  Will fall thru to the CS */ /*If Pi is waiting on Pk, then number[k] will go to 0 because Pk will eventually  Get served –thus causing Pi to break out of  the while loop and check out the status of the next process if any */ /*the while loop skips over the case k == i  */ } do {   /* Has four states: choosing, scanning, CS, remainder section */ critical section /* no longer a candidate for CS entry */ remainder section } while (1); number[i] = 0;
SEMAPHORSES Solve critical-section problem, Synchronization tool-  semaphores Two or more processes can cooperate by means of simple signals, such that a process can be forced to stop at a specific place until it has received a specific signal For signaling, special variables, called  semaphores  are used Semaphore S is an integer variable that, apart from initialization, is accessed only through two standard atomic operation :  wait  and  signal. They are termed as P for wait (to test) V for signal (to increment)
SEMAPHORSES It can only be accessed via two indivisible ( atomic ) operations. Definition-Wait wait(S) { while(S<=0) ; //no-op S--; } Definition-signal Signal(S) { S++; }
A queue is used to hold processes waiting on a  semaphore. struct  semaphore { int  count; queueType  queue; } void wait(semaphore s) { s.count--; if  (s.count < 0) { place this process in s.queue; block this process } } void signal(semaphore s) { s.count++; if  (s.count <= 0) { remove a process P from s.queue; place process P on ready list; } }
SEMAPHORES -Two Type  Counting  semaphore – integer value can range over an unrestricted domain. Binary  semaphore – integer value can range only between 0 and 1; can be simpler to implement. Can implement a counting semaphore  S  as a binary semaphore
SEMAPHORES -Binary Semaphores Struct b-semaphore { int value;  /* boolean, only 0 or 1 allowed */ struct process queue; } Void wait-b (b-semaphore s)  {  /* see alternate def. Below */ if (s.value == 1) s.value = 0;  // Lock the “door” & let process enter CS else { place this process in s.queue and block it;}   // no indication of size of queue ... compare to general semaphore }  // wait unconditionally leaves b-semaphore value at 0 Void signal-b(b-semaphore s)  {  //s.value==0 is necessary   but not sufficient   if (s.queue is empty) s.value = 1;  // condition for empty queue else move a process from s.queue to ready list;  }  // if only 1 proc in queue, leave value at 0, “moved” proc will go to CS   ********************************************************************* Alternate definition of wait-b (simpler): Void wait-b (b-semaphore s)  { if (s.value == 0) {place this process in s.queue and block it;} s.value =0;  // value was 1, set to 0 }

More Related Content

PPTX
Deadlock in Operating system concept, Types of Deadlock
PPTX
Assemblers
PPTX
Types of Addressing modes- COA
PDF
5 process synchronization
PPT
OS Process Synchronization, semaphore and Monitors
PPTX
Deadlock ppt
PPTX
MULTILEVEL QUEUE SCHEDULING
PPTX
Input-Buffering
Deadlock in Operating system concept, Types of Deadlock
Assemblers
Types of Addressing modes- COA
5 process synchronization
OS Process Synchronization, semaphore and Monitors
Deadlock ppt
MULTILEVEL QUEUE SCHEDULING
Input-Buffering

What's hot (20)

PPT
Logic Micro Operation
PPTX
Deadlock dbms
PPTX
The n Queen Problem
PPTX
Single source Shortest path algorithm with example
PPTX
Instruction Set Architecture
PPTX
Greedy algorithms
PPTX
Priority Queue in Data Structure
PDF
Processor Organization
PDF
Compiler lec 8
PPTX
Concurrency: Mutual Exclusion and Synchronization
PPT
Deadlock detection and recovery by saad symbian
PPT
Operating System: Deadlock
PPTX
closure properties of regular language.pptx
PPTX
Data Structure and Algorithm - Divide and Conquer
PPT
Dinive conquer algorithm
PPT
Binary Search
PPTX
4.2 variantsof turing machines (types of tm)
PPTX
PPTX
0 1 knapsack using branch and bound
PPT
Computer architecture pipelining
Logic Micro Operation
Deadlock dbms
The n Queen Problem
Single source Shortest path algorithm with example
Instruction Set Architecture
Greedy algorithms
Priority Queue in Data Structure
Processor Organization
Compiler lec 8
Concurrency: Mutual Exclusion and Synchronization
Deadlock detection and recovery by saad symbian
Operating System: Deadlock
closure properties of regular language.pptx
Data Structure and Algorithm - Divide and Conquer
Dinive conquer algorithm
Binary Search
4.2 variantsof turing machines (types of tm)
0 1 knapsack using branch and bound
Computer architecture pipelining
Ad

Viewers also liked (20)

PPTX
Process synchronization in Operating Systems
PPT
Chapter 6 - Process Synchronization
PPT
Process synchronization(deepa)
PDF
Operating Systems - Synchronization
PDF
Semaphores
PPT
Ch7: Process Synchronization
PPT
Process Synchronization And Deadlocks
PPT
Peterson Critical Section Problem Solution
ODP
Semaphore
PPT
Semaphores OS Basics
PPTX
Operating system critical section
PDF
Operating Systems - Process Synchronization and Deadlocks
PDF
Unit II - 3 - Operating System - Process Synchronization
PPT
Lec11 semaphores
PPSX
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
PPT
System software
PPTX
Producer consumer
PDF
Process Scheduling
PPT
CPU Scheduling Algorithms
PPTX
Process synchronization
Process synchronization in Operating Systems
Chapter 6 - Process Synchronization
Process synchronization(deepa)
Operating Systems - Synchronization
Semaphores
Ch7: Process Synchronization
Process Synchronization And Deadlocks
Peterson Critical Section Problem Solution
Semaphore
Semaphores OS Basics
Operating system critical section
Operating Systems - Process Synchronization and Deadlocks
Unit II - 3 - Operating System - Process Synchronization
Lec11 semaphores
06 lcd slides 1 - PROCESS SYNCHRONIZATION POWERPOINT
System software
Producer consumer
Process Scheduling
CPU Scheduling Algorithms
Process synchronization
Ad

Similar to Process Synchronization (20)

PPT
PPT
Operating System
PPT
U3-PPT-1 (1).ppt
PDF
OperatingSystem-Unit2_Process Management
PPTX
Chapter6 Synchronization in Operating systems.pptx
PDF
Process Synchronization
PDF
CH05.pdf
PPTX
Synchronization Peterson’s Solution.pptx
PPT
PDF
Slides for OS 06-Sync.pdf
PDF
OS Process synchronization Unit3 synchronization
PPT
Lecture16-17.ppt
PPTX
Synchronization in os.pptx
PDF
Process Synchronization
PPTX
Cs problem [repaired]
PPT
Ch7 OS
 
PPT
Ch7 Process Synchronization galvin
Operating System
U3-PPT-1 (1).ppt
OperatingSystem-Unit2_Process Management
Chapter6 Synchronization in Operating systems.pptx
Process Synchronization
CH05.pdf
Synchronization Peterson’s Solution.pptx
Slides for OS 06-Sync.pdf
OS Process synchronization Unit3 synchronization
Lecture16-17.ppt
Synchronization in os.pptx
Process Synchronization
Cs problem [repaired]
Ch7 OS
 
Ch7 Process Synchronization galvin

More from Sonali Chauhan (20)

PDF
Chapter 2 enterprise an overview - alexis leon
PDF
Chapter 10 Future Directions In ERP
PDF
Chapter 5 E R P Modules Alexis Leon
PDF
Chapter 7 E R P Implementation Lifecycle Alexis Leon
PDF
Chapter 10 Future Directions In Erp A Lexis Leon
PDF
Chapter 9 Vendors Consultants Users Alexis Leon
PDF
Chapter 3 E R P And Related Tech Alexis Leon
PDF
Chapter 1 enterprise resource planning alexis leon
PDF
Os Question Bank
PPS
Chapter 2 Enterprise An Overview Alexis Leon
PPT
Chapter 1 Enterprise Resource Planning Alexis Leon
PPS
Chapter 7 Erp Implementation Lifecycle Alexis Leon
PPT
Mobile Communication Broadcast System Jochen Schiller
PPT
3.Medium Access Control
PPT
PPT
Operating System Deadlock Galvin
PPT
Cpu Scheduling Galvin
PPT
Erp Alex Leon Chapter 10
PPT
Erp Alex Leon Chapter 5
Chapter 2 enterprise an overview - alexis leon
Chapter 10 Future Directions In ERP
Chapter 5 E R P Modules Alexis Leon
Chapter 7 E R P Implementation Lifecycle Alexis Leon
Chapter 10 Future Directions In Erp A Lexis Leon
Chapter 9 Vendors Consultants Users Alexis Leon
Chapter 3 E R P And Related Tech Alexis Leon
Chapter 1 enterprise resource planning alexis leon
Os Question Bank
Chapter 2 Enterprise An Overview Alexis Leon
Chapter 1 Enterprise Resource Planning Alexis Leon
Chapter 7 Erp Implementation Lifecycle Alexis Leon
Mobile Communication Broadcast System Jochen Schiller
3.Medium Access Control
Operating System Deadlock Galvin
Cpu Scheduling Galvin
Erp Alex Leon Chapter 10
Erp Alex Leon Chapter 5

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Big Data Technologies - Introduction.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Approach and Philosophy of On baking technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
KodekX | Application Modernization Development
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
Encapsulation theory and applications.pdf
NewMind AI Monthly Chronicles - July 2025
Big Data Technologies - Introduction.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
20250228 LYD VKU AI Blended-Learning.pptx
Approach and Philosophy of On baking technology
Review of recent advances in non-invasive hemoglobin estimation
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Understanding_Digital_Forensics_Presentation.pptx
A Presentation on Artificial Intelligence
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
KodekX | Application Modernization Development
Digital-Transformation-Roadmap-for-Companies.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Network Security Unit 5.pdf for BCA BBA.
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Dropbox Q2 2025 Financial Results & Investor Presentation

Process Synchronization

  • 1. PROCESS SYNCHRONIZATION SONALI C. UDIT-SYBSc-IT 2008-09
  • 2. INTRODUCTION Cooperating Processes is one that can be affected or will affect other process executing in the system. Concurrent access to share data may result in to data inconsistency So we will discuss mechanisms to ensure the orderly execution of cooperating processes that share logical address space, so that consistency is maintained .
  • 3. CRITICAL SECTION n processes all competing to use some shared data Each process has a code segment, called critical section , in which the shared data is accessed. Set of instructions that must be controlled so as to allow exclusive access to one process. execution of the critical section by processes is mutually exclusive in time .
  • 4. CRITICAL SECTION Do{ critical section reminder section }while(1) Each process must request permission to enter critical section – ENTRY SECTION Critical section may follow by EXIT SECTION . Remaining code is REMAINDER SECTION Cont… Entry section exit section GENERAL STRUCTURE OF A TYPICAL PROCESS Pi
  • 5. CRITICAL SECTION Solution to the Critical Section Problem must meet three conditions... Mutual Exclusion - If process P i 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 Cont…
  • 6. TWO-PROCESS SOLUTION Algorithm is only for 2 processes at a time Processes are P 0 P 1 Or can also be represented as P i and P j , i.e. j=1-i
  • 7. TWO-PROCESS SOLUTION -Algorithm 1 If process turn == i , P i is allowed to execute in critical section But, Guarantees mutual exclusion. Does not guarantee progress --- enforces strict alternation of processes entering CS's ( if P j decides not to re-enter or crashes outside CS, then P i cannot ever get in) . Let the processes share common integer variable turn Let int turn =0 (or 1) Do{ Critical section remainder section }while(1) For Process P i while (turn != i); Turn = j;
  • 8. TWO-PROCESS SOLUTION -Algorithm 2 Shared variables boolean flag[2]; // “interest” bits initially flag [0] = flag [1] = false. flag [i] = true  P i declares interest in entering its critical section Process P i // where the “other” process is P j do { flag[i] = true; // declare your own interest while (flag[ j]) ; //wait if the other guy is interested critical section flag [i] = false; // declare that you lost interest remainder section // allows other guy to enter } while (1);
  • 9. Satisfies mutual exclusion, but not progress requirement. If flag[ i] == flag[ j] == true, then deadlock - no progress but barring this event, a non-CS guy cannot block you from entering Can make consecutive re-entries to CS if other not interested TWO-PROCESS SOLUTION -Algorithm 2
  • 10. Combined shared variables & approaches of algorithms 1 and 2. TWO-PROCESS SOLUTION -Algorithm 3 For Process P i do { flag [i] = true; // declare your interest to enter turn = j; // assume it is the other’s turn-give PJ a chance while (flag [ j ] and turn == j) ; critical section flag [i] = false; remainder section } while (1);
  • 11. Meets all three requirements; solves the critical-section problem for two processes ( the best of all worlds - almost!). Turn variable breaks any deadlock possibility of previous example, AND prevents “hogging” – P i setting turn to j gives P J a chance after each pass of P i ’s CS Flag[ ] variable prevents getting locked out if other guy never re-enters or crashes outside and allows CS consecutive access other not interested in entering. TWO-PROCESS SOLUTION -Algorithm 3
  • 12. MULTIPLE-PROCESS SOLUTIONS - Bakery Algorithm Algo solves the critical-section problem for n-process. Before entering its critical section, process receives a number. Holder of the smallest number enters the critical section. If processes P i and P j receive the same number, if i < j , then P i is served first; else P j is served first. Since process name are unique The numbering scheme always generates numbers in increasing order of enumeration; i.e., 1,2,3,3,3,3,4,5...
  • 13. The common data structure are: boolean choosing[n]; Int number[n]; Initially, boolean choosing[n]=false; int number[n]=o; Notation ( a,b ) < c,d ) if a < c or if a = c and b < d max ( a 0 ,…, a n -1 ) is a number, k , such that k  a i for i = 0, … , n – 1 MULTIPLE-PROCESS SOLUTIONS - Bakery Algorithm
  • 14. The structure of process P i in the bakery algorithm is as follow… MULTIPLE-PROCESS SOLUTIONS - Bakery Algorithm
  • 15. choosing[i] = true; //process does not “compete” while choosing a # number[i] = max(number[0], number[1], …, number [n – 1]) + 1; choosing[i] = false; /*scan all processes to see if if Pi has lowest number: */ for (k = 0; k < n; k++) { /* check process k, book uses dummy variable “j” */ while (choosing[ k ]); /*if Pk is choosing a number wait till done */ while ((number[k] != 0) && (number[k], k < number[ i ], i)) ; /*if (Pk is waiting (or in CS) and Pk is “ahead” of Pi then Pi waits */ /*if Pk is not in CS and is not waiting, OR Pk is waiting with a larger number, then skip over Pk - when Pi gets to end of scan, it will have lowest number and Will fall thru to the CS */ /*If Pi is waiting on Pk, then number[k] will go to 0 because Pk will eventually Get served –thus causing Pi to break out of the while loop and check out the status of the next process if any */ /*the while loop skips over the case k == i */ } do { /* Has four states: choosing, scanning, CS, remainder section */ critical section /* no longer a candidate for CS entry */ remainder section } while (1); number[i] = 0;
  • 16. SEMAPHORSES Solve critical-section problem, Synchronization tool- semaphores Two or more processes can cooperate by means of simple signals, such that a process can be forced to stop at a specific place until it has received a specific signal For signaling, special variables, called semaphores are used Semaphore S is an integer variable that, apart from initialization, is accessed only through two standard atomic operation : wait and signal. They are termed as P for wait (to test) V for signal (to increment)
  • 17. SEMAPHORSES It can only be accessed via two indivisible ( atomic ) operations. Definition-Wait wait(S) { while(S<=0) ; //no-op S--; } Definition-signal Signal(S) { S++; }
  • 18. A queue is used to hold processes waiting on a semaphore. struct semaphore { int count; queueType queue; } void wait(semaphore s) { s.count--; if (s.count < 0) { place this process in s.queue; block this process } } void signal(semaphore s) { s.count++; if (s.count <= 0) { remove a process P from s.queue; place process P on ready list; } }
  • 19. SEMAPHORES -Two Type Counting semaphore – integer value can range over an unrestricted domain. Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement. Can implement a counting semaphore S as a binary semaphore
  • 20. SEMAPHORES -Binary Semaphores Struct b-semaphore { int value; /* boolean, only 0 or 1 allowed */ struct process queue; } Void wait-b (b-semaphore s) { /* see alternate def. Below */ if (s.value == 1) s.value = 0; // Lock the “door” & let process enter CS else { place this process in s.queue and block it;} // no indication of size of queue ... compare to general semaphore } // wait unconditionally leaves b-semaphore value at 0 Void signal-b(b-semaphore s) { //s.value==0 is necessary but not sufficient if (s.queue is empty) s.value = 1; // condition for empty queue else move a process from s.queue to ready list; } // if only 1 proc in queue, leave value at 0, “moved” proc will go to CS ********************************************************************* Alternate definition of wait-b (simpler): Void wait-b (b-semaphore s) { if (s.value == 0) {place this process in s.queue and block it;} s.value =0; // value was 1, set to 0 }