SlideShare a Scribd company logo
PROCESS SYNCHRONIZATION BY R DEEPA LAKSHMI
CONTENTS Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Suppose that we wanted to provide a solution to the consumer-producer problem that fills  all  the buffers. We can do so by having an integer  count  that keeps track of the number of full buffers.  Initially, count is set to 0. It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer.
Producer-Consumer Example
Producer  while (true)  { /* produce an item and put in nextProduced*/ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }
Consumer while (1)  { while (count == 0) ; // do nothing nextConsumed =  buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /*  consume the item in nextConsumed*/   }
Race Condition count++ could be implemented as   register1 = count   register1 = register1 + 1   count = register1 count-- could be implemented as   register2 = count   register2 = register2 - 1   count = register2 Consider 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}
Solution to Critical-Section Problem 1 . Mutual Exclusion  - If process  P i  is executing in its critical section, then no other processes can be executing in their critical sections 2. 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 3. 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
Critical-Section Problem Race Condition  - When there is concurrent access to shared data and the final outcome depends upon order of execution. Critical Section  - Section of code where shared data is accessed.  Entry Section  -  Code that requests permission to enter its critical section. Exit Section  - Code that is run after exiting the critical section
Structure of a Typical Process While (true) { Entry section CRITICAL SECTION Exit section REMAINDER SECTION }
Peterson’s Solution
Algorithm 1 do { While (turn != i); critical section Turn = j; remainder section } while(1);
Algorithm 2 do { Flag[i] = true; While(flag[j]); critical section Flag[i] = flase; remainder section } while(1);
Algorithm 3 Two process solution Assume 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 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  P i  is ready!
Algorithm for Process P i do { flag[i] = TRUE; turn = j; while ( flag[j] && turn == j); CRITICAL SECTION flag[i] = FALSE; REMAINDER SECTION } while (TRUE);
Synchronization Hardware Many systems provide hardware support for critical section code Uniprocessors – could disable interrupts Currently running code would execute without preemption Generally too inefficient on multiprocessor systems Operating systems using this not broadly scalable Modern machines provide special atomic hardware instructions Atomic = non-interruptable Either test memory word and set value Or swap contents of two memory words
TestAndndSet Instruction   Definition: boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv: }
Mutual exclusion implemention with TestAndSet Shared boolean variable lock., initialized to false. Solution: do { while ( TestAndSet (&lock )) ;  /* do nothing //  critical section lock = FALSE; //  remainder section  } while ( TRUE);
Swap  Instruction Definition: void Swap (boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp: }
Mutual exclusion implemention with  swap instruction Shared Boolean variable lock initialized to FALSE; Each process has a local Boolean variable key. Solution: do { key = TRUE; while ( key == TRUE) Swap (&lock, &key ); //  critical section lock = FALSE; //  remainder section  } while ( TRUE);
Bounded-waiting Mutual Exclusion with TestAndSet() do {  waiting[i] = TRUE;  key = TRUE;  while (waiting[i] && key)  key = TestAndSet(&lock);  waiting[i] = FALSE;  // critical section  j = (i + 1) % n;  while ((j != i) && !waiting[j])  j = (j + 1) % n;  if (j == i)  lock = FALSE;  else  waiting[j] = FALSE;  // remainder section  } while (TRUE);
REFERENCES BOOK Operating Systems Concepts - Silberschatz, Galvin, Gagne WEBSITE www.en.wikipedia.org
THANK YOU

More Related Content

What's hot (20)

PPTX
Demand paging
Trinity Dwarka
 
PPTX
Arithmatic pipline
A. Shamel
 
PPT
Scheduling algorithms
Chankey Pathak
 
PPTX
Thread scheduling...................pptx
arahanthavarma4
 
PPTX
Cpu scheduling
Karthick Sekar
 
PPT
Operating Systems - "Chapter 5 Process Synchronization"
Ra'Fat Al-Msie'deen
 
PPT
Chapter 3 - Processes
Wayne Jones Jnr
 
PPT
Transaction slide
shawon roy
 
PPT
Memory management
Vishal Singh
 
PPTX
Distributed Mutual Exclusion and Distributed Deadlock Detection
SHIKHA GAUTAM
 
PPTX
Operating system critical section
Harshana Madusanka Jayamaha
 
PPTX
File system structure
sangrampatil81
 
DOC
Unit 1 architecture of distributed systems
karan2190
 
PPTX
Cpu scheduling in operating System.
Ravi Kumar Patel
 
PPTX
SCHEDULING ALGORITHMS
Dhaval Sakhiya
 
PDF
Process scheduling (CPU Scheduling)
Mukesh Chinta
 
PPT
Chapter 10 - File System Interface
Wayne Jones Jnr
 
PPTX
Dining Philosopher Problem
Raval Vijay
 
PDF
Semaphores
Mohd Arif
 
PPT
Contiguous Memory Allocation.ppt
infomerlin
 
Demand paging
Trinity Dwarka
 
Arithmatic pipline
A. Shamel
 
Scheduling algorithms
Chankey Pathak
 
Thread scheduling...................pptx
arahanthavarma4
 
Cpu scheduling
Karthick Sekar
 
Operating Systems - "Chapter 5 Process Synchronization"
Ra'Fat Al-Msie'deen
 
Chapter 3 - Processes
Wayne Jones Jnr
 
Transaction slide
shawon roy
 
Memory management
Vishal Singh
 
Distributed Mutual Exclusion and Distributed Deadlock Detection
SHIKHA GAUTAM
 
Operating system critical section
Harshana Madusanka Jayamaha
 
File system structure
sangrampatil81
 
Unit 1 architecture of distributed systems
karan2190
 
Cpu scheduling in operating System.
Ravi Kumar Patel
 
SCHEDULING ALGORITHMS
Dhaval Sakhiya
 
Process scheduling (CPU Scheduling)
Mukesh Chinta
 
Chapter 10 - File System Interface
Wayne Jones Jnr
 
Dining Philosopher Problem
Raval Vijay
 
Semaphores
Mohd Arif
 
Contiguous Memory Allocation.ppt
infomerlin
 

Viewers also liked (8)

PDF
Operating Systems - Synchronization
Emery Berger
 
PPT
Chapter 6 - Process Synchronization
Wayne Jones Jnr
 
PDF
Unit II - 3 - Operating System - Process Synchronization
cscarcas
 
PDF
Web technology
Selvin Josy Bai Somu
 
PPTX
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
PPT
Web Tech
Rupsee
 
PPT
OS Process Synchronization, semaphore and Monitors
sgpraju
 
PPT
Process Synchronization And Deadlocks
tech2click
 
Operating Systems - Synchronization
Emery Berger
 
Chapter 6 - Process Synchronization
Wayne Jones Jnr
 
Unit II - 3 - Operating System - Process Synchronization
cscarcas
 
Web technology
Selvin Josy Bai Somu
 
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
Web Tech
Rupsee
 
OS Process Synchronization, semaphore and Monitors
sgpraju
 
Process Synchronization And Deadlocks
tech2click
 
Ad

Similar to Process synchronization(deepa) (20)

PPT
OSCh7
Joe Christensen
 
PDF
CH05.pdf
ImranKhan880955
 
PPTX
Operating system 23 process synchronization
Vaibhav Khanna
 
PPTX
Synchronization in os.pptx
AbdullahBhatti53
 
PPTX
Synchronization hardware
Saeram Butt
 
PPTX
operating system notes about deadlock 3.pptx
panditestmail
 
PDF
OperatingSystem-Unit2_Process Management
SonaShaiju1
 
PDF
Lecture 5- Process Synchonization_revised.pdf
Amanuelmergia
 
PPTX
synchronization in operating system structure
gaurav77712
 
PDF
Ch5 process synchronization
Welly Dian Astika
 
PPT
Operating System
Subhasis Dash
 
PPTX
Chapter6 Synchronization in Operating systems.pptx
abdullahsbusiness123
 
PPT
Ch7
Bilal Arshad
 
PPTX
Lecture 8.pptx Operating system lecture
ReelsShortVideo
 
PPTX
Chapter 5 - Operating Synchronization.pptx
MujtabaVlogs
 
PPTX
MODULE 3 process synchronizationnnn.pptx
senthilkumar969017
 
PPT
U3-PPT-1 (1).ppt
AJAYVISHALRP
 
PPT
Process Synchronization
Sonali Chauhan
 
PPT
Lecture16-17.ppt
ssuserf67e3a
 
CH05.pdf
ImranKhan880955
 
Operating system 23 process synchronization
Vaibhav Khanna
 
Synchronization in os.pptx
AbdullahBhatti53
 
Synchronization hardware
Saeram Butt
 
operating system notes about deadlock 3.pptx
panditestmail
 
OperatingSystem-Unit2_Process Management
SonaShaiju1
 
Lecture 5- Process Synchonization_revised.pdf
Amanuelmergia
 
synchronization in operating system structure
gaurav77712
 
Ch5 process synchronization
Welly Dian Astika
 
Operating System
Subhasis Dash
 
Chapter6 Synchronization in Operating systems.pptx
abdullahsbusiness123
 
Lecture 8.pptx Operating system lecture
ReelsShortVideo
 
Chapter 5 - Operating Synchronization.pptx
MujtabaVlogs
 
MODULE 3 process synchronizationnnn.pptx
senthilkumar969017
 
U3-PPT-1 (1).ppt
AJAYVISHALRP
 
Process Synchronization
Sonali Chauhan
 
Lecture16-17.ppt
ssuserf67e3a
 
Ad

More from Nagarajan (18)

PPT
Chapter3
Nagarajan
 
PPT
Chapter2
Nagarajan
 
PPT
Chapter1
Nagarajan
 
PPTX
Minimax
Nagarajan
 
PPT
I/O System
Nagarajan
 
PPT
Scheduling algorithm (chammu)
Nagarajan
 
PPT
Real time os(suga)
Nagarajan
 
PPT
Posix threads(asha)
Nagarajan
 
PPT
Monitor(karthika)
Nagarajan
 
PPT
Cpu scheduling(suresh)
Nagarajan
 
PPT
Backward chaining(bala,karthi,rajesh)
Nagarajan
 
PPTX
Inferno
Nagarajan
 
PPTX
Javascript
Nagarajan
 
PPTX
Introduction Of Artificial neural network
Nagarajan
 
PPT
Perceptron
Nagarajan
 
PPT
Back propagation
Nagarajan
 
PPTX
Ms access
Nagarajan
 
PPT
Adaline madaline
Nagarajan
 
Chapter3
Nagarajan
 
Chapter2
Nagarajan
 
Chapter1
Nagarajan
 
Minimax
Nagarajan
 
I/O System
Nagarajan
 
Scheduling algorithm (chammu)
Nagarajan
 
Real time os(suga)
Nagarajan
 
Posix threads(asha)
Nagarajan
 
Monitor(karthika)
Nagarajan
 
Cpu scheduling(suresh)
Nagarajan
 
Backward chaining(bala,karthi,rajesh)
Nagarajan
 
Inferno
Nagarajan
 
Javascript
Nagarajan
 
Introduction Of Artificial neural network
Nagarajan
 
Perceptron
Nagarajan
 
Back propagation
Nagarajan
 
Ms access
Nagarajan
 
Adaline madaline
Nagarajan
 

Recently uploaded (20)

PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 

Process synchronization(deepa)

  • 1. PROCESS SYNCHRONIZATION BY R DEEPA LAKSHMI
  • 2. CONTENTS Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware
  • 3. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Suppose that we wanted to provide a solution to the consumer-producer problem that fills all the buffers. We can do so by having an integer count that keeps track of the number of full buffers. Initially, count is set to 0. It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer.
  • 5. Producer while (true) { /* produce an item and put in nextProduced*/ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }
  • 6. Consumer while (1) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /* consume the item in nextConsumed*/ }
  • 7. Race Condition count++ could be implemented as register1 = count register1 = register1 + 1 count = register1 count-- could be implemented as register2 = count register2 = register2 - 1 count = register2 Consider 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}
  • 8. Solution to Critical-Section Problem 1 . Mutual Exclusion - If process P i is executing in its critical section, then no other processes can be executing in their critical sections 2. 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 3. 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
  • 9. Critical-Section Problem Race Condition - When there is concurrent access to shared data and the final outcome depends upon order of execution. Critical Section - Section of code where shared data is accessed. Entry Section - Code that requests permission to enter its critical section. Exit Section - Code that is run after exiting the critical section
  • 10. Structure of a Typical Process While (true) { Entry section CRITICAL SECTION Exit section REMAINDER SECTION }
  • 12. Algorithm 1 do { While (turn != i); critical section Turn = j; remainder section } while(1);
  • 13. Algorithm 2 do { Flag[i] = true; While(flag[j]); critical section Flag[i] = flase; remainder section } while(1);
  • 14. Algorithm 3 Two process solution Assume 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 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 P i is ready!
  • 15. Algorithm for Process P i do { flag[i] = TRUE; turn = j; while ( flag[j] && turn == j); CRITICAL SECTION flag[i] = FALSE; REMAINDER SECTION } while (TRUE);
  • 16. Synchronization Hardware Many systems provide hardware support for critical section code Uniprocessors – could disable interrupts Currently running code would execute without preemption Generally too inefficient on multiprocessor systems Operating systems using this not broadly scalable Modern machines provide special atomic hardware instructions Atomic = non-interruptable Either test memory word and set value Or swap contents of two memory words
  • 17. TestAndndSet Instruction Definition: boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv: }
  • 18. Mutual exclusion implemention with TestAndSet Shared boolean variable lock., initialized to false. Solution: do { while ( TestAndSet (&lock )) ; /* do nothing // critical section lock = FALSE; // remainder section } while ( TRUE);
  • 19. Swap Instruction Definition: void Swap (boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp: }
  • 20. Mutual exclusion implemention with swap instruction Shared Boolean variable lock initialized to FALSE; Each process has a local Boolean variable key. Solution: do { key = TRUE; while ( key == TRUE) Swap (&lock, &key ); // critical section lock = FALSE; // remainder section } while ( TRUE);
  • 21. Bounded-waiting Mutual Exclusion with TestAndSet() do { waiting[i] = TRUE; key = TRUE; while (waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // critical section j = (i + 1) % n; while ((j != i) && !waiting[j]) j = (j + 1) % n; if (j == i) lock = FALSE; else waiting[j] = FALSE; // remainder section } while (TRUE);
  • 22. REFERENCES BOOK Operating Systems Concepts - Silberschatz, Galvin, Gagne WEBSITE www.en.wikipedia.org