SlideShare a Scribd company logo
1 of 14
6.1 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts – 7th Edition, Feb 8, 2005
Process Synchronization
Background
Critical-Section
Deadlock and Starvation
 Concvurrent access to shared data may result in data
inconsistency
 Maintaining data consistency requires mechanisms to
ensure the orderly execution of cooperating processes
6.2 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts – 7th Edition, Feb 8, 2005
Solve the Critical-Section
1. Peterson’s Solution : software solution :can be interrupted
:waiting problem
2. Test Memory Word and Set : Hardware : :waiting problem
3. swap contents of two memory words : Hardware : :waiting problem
 Semaphore 1- Semaphore with waiting :waiting problem
2- Semaphore with no Busy waiting
6.3 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts – 7th Edition, Feb 8, 2005
Peterson’s Solution
 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 Pi is ready!
6.4 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts – 7th Edition, Feb 8, 2005
Algorithm for Process Pi
while (true) {
flag[i] = TRUE;
turn = j;
while ( flag[j] && turn == j);
CRITICAL SECTION
flag[i] = FALSE;
REMAINDER SECTION
}
6.5 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts – 7th Edition, Feb 8, 2005
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
1. Either test memory word and set value
2. Or swap contents of two memory words
6.6 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts – 7th Edition, Feb 8, 2005
TestAndndSet Instruction
 Definition:
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
6.7 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts – 7th Edition, Feb 8, 2005
Solution using TestAndSet
 Shared boolean variable lock., initialized to false.
 Solution:
while (true) {
while ( TestAndSet (&lock ))
; /* do nothing
// critical section
lock = FALSE;
// remainder section
}
6.8 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts – 7th Edition, Feb 8, 2005
Semaphore
 Synchronization tool that does not require busy waiting
 Semaphore S – integer variable
 Two standard operations modify S: wait() and signal()
 Originally called P() and V()
 Less complicated
 Can only be accessed via two indivisible (atomic) operations
 Two Methods: 1- Semaphore with waiting
2- Semaphore with no Busy waiting
 wait (S) {
while S = 0
; // no-op
S--;
}
C.S.
signal (S) {
S++;
}
6.9 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts – 7th Edition, Feb 8, 2005
Semaphore as General Synchronization Tool
 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
 Also known as mutex locks
 Can implement a counting semaphore S as a binary
semaphore
 Provides mutual exclusion
 Semaphore S; // initialized to 1
 wait (S);
Critical Section
signal (S);
6.10 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts – 7th Edition, Feb 8, 2005
Semaphore Implementation
 Must guarantee that no two processes can execute wait ()
and signal () on the same semaphore at the same time
 Thus, implementation becomes the critical section
problem where the wait and signal code are placed in the
crtical section.
 Could now have busy waiting in critical section
implementation
 But implementation code is short
 Little busy waiting if critical section rarely occupied
 Note that applications may spend lots of time in critical
sections and therefore this is not a good solution.
6.11 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts – 7th Edition, Feb 8, 2005
Semaphore Implementation with no Busy waiting
 With 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 list
 Two operations:
 block – place the process invoking the operation on
the appropriate waiting queue.
 wakeup – remove one of processes in the waiting
queue and place it in the ready queue.
6.12 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts – 7th Edition, Feb 8, 2005
Semaphore Implementation with no Busy waiting (Cont.)
 Implementation of wait:
wait (S){
value--;
if (value < 0) {
add this process to waiting queue
block(); }
}
 Implementation of signal:
Signal (S){
value++;
if (value <= 0) {
remove a process P from the waiting queue
wakeup(P); }
}
6.13 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts – 7th Edition, Feb 8, 2005
Deadlock and Starvation
 Deadlock – two or more processes are waiting indefinitely for
an event that can be caused by only one of the waiting
processes
 Let S and Q be two semaphores initialized to 1
P0 P1
wait (S); wait (Q);
wait (Q); wait (S);
. .
. .
. .
signal (S); signal (Q);
signal (Q); signal (S);
 Starvation – indefinite blocking. A process may never be
removed from the semaphore queue in which it is suspended.
6.14 Silberschatz, Galvin and Gagne ©2005
Operating System Concepts – 7th Edition, Feb 8, 2005
Dining-Philosophers Problem
 Shared data
 Bowl of rice (data set)
 Semaphore chopstick [5] initialized to 1

More Related Content

Similar to Process Synchronization Techniques

ch5 [Autosaved].ppt
ch5 [Autosaved].pptch5 [Autosaved].ppt
ch5 [Autosaved].pptmonirJihad2
 
Operating System-Process Synchronization
Operating System-Process SynchronizationOperating System-Process Synchronization
Operating System-Process Synchronizationbismahmalik22
 
6.Process Synchronization
6.Process Synchronization6.Process Synchronization
6.Process SynchronizationSenthil Kanth
 
Process synchonization : operating system ( Btech cse )
Process synchonization : operating system ( Btech cse )Process synchonization : operating system ( Btech cse )
Process synchonization : operating system ( Btech cse )HimanshuSharma1389
 
chapter5 processes of synchronizatio ppt
chapter5 processes of synchronizatio pptchapter5 processes of synchronizatio ppt
chapter5 processes of synchronizatio pptAbdikani34
 
ch5-Process_Synchronization.pdf
ch5-Process_Synchronization.pdfch5-Process_Synchronization.pdf
ch5-Process_Synchronization.pdfsharada3
 
ch5.ppt operating system
ch5.ppt operating systemch5.ppt operating system
ch5.ppt operating systemSami Mughal
 
Unit II - 3 - Operating System - Process Synchronization
Unit II - 3 - Operating System - Process SynchronizationUnit II - 3 - Operating System - Process Synchronization
Unit II - 3 - Operating System - Process Synchronizationcscarcas
 
ch5.ppt Operating System Ppt for ptu student
ch5.ppt Operating System Ppt for ptu studentch5.ppt Operating System Ppt for ptu student
ch5.ppt Operating System Ppt for ptu studentBhartiRani14
 
Galvin-operating System(Ch7)
Galvin-operating System(Ch7)Galvin-operating System(Ch7)
Galvin-operating System(Ch7)dsuyal1
 
Operating Systems Chapter 6 silberschatz
Operating Systems Chapter 6 silberschatzOperating Systems Chapter 6 silberschatz
Operating Systems Chapter 6 silberschatzGiulianoRanauro
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Ra'Fat Al-Msie'deen
 
Lec11 semaphores
Lec11 semaphoresLec11 semaphores
Lec11 semaphoresanandammca
 

Similar to Process Synchronization Techniques (20)

ch5 [Autosaved].ppt
ch5 [Autosaved].pptch5 [Autosaved].ppt
ch5 [Autosaved].ppt
 
Operating System-Process Synchronization
Operating System-Process SynchronizationOperating System-Process Synchronization
Operating System-Process Synchronization
 
6.Process Synchronization
6.Process Synchronization6.Process Synchronization
6.Process Synchronization
 
Lecture#5
Lecture#5Lecture#5
Lecture#5
 
Process synchonization : operating system ( Btech cse )
Process synchonization : operating system ( Btech cse )Process synchonization : operating system ( Btech cse )
Process synchonization : operating system ( Btech cse )
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
chapter5 processes of synchronizatio ppt
chapter5 processes of synchronizatio pptchapter5 processes of synchronizatio ppt
chapter5 processes of synchronizatio ppt
 
ch5-Process_Synchronization.pdf
ch5-Process_Synchronization.pdfch5-Process_Synchronization.pdf
ch5-Process_Synchronization.pdf
 
ch5.ppt operating system
ch5.ppt operating systemch5.ppt operating system
ch5.ppt operating system
 
Unit II - 3 - Operating System - Process Synchronization
Unit II - 3 - Operating System - Process SynchronizationUnit II - 3 - Operating System - Process Synchronization
Unit II - 3 - Operating System - Process Synchronization
 
ch5.ppt Operating System Ppt for ptu student
ch5.ppt Operating System Ppt for ptu studentch5.ppt Operating System Ppt for ptu student
ch5.ppt Operating System Ppt for ptu student
 
Ch6
Ch6Ch6
Ch6
 
chapter4.pptx
chapter4.pptxchapter4.pptx
chapter4.pptx
 
ch6.ppt
ch6.pptch6.ppt
ch6.ppt
 
Galvin-operating System(Ch7)
Galvin-operating System(Ch7)Galvin-operating System(Ch7)
Galvin-operating System(Ch7)
 
Operating Systems Chapter 6 silberschatz
Operating Systems Chapter 6 silberschatzOperating Systems Chapter 6 silberschatz
Operating Systems Chapter 6 silberschatz
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
 
Lec11 semaphores
Lec11 semaphoresLec11 semaphores
Lec11 semaphores
 

More from ssuser893014

ال ثريد في البرمجة.pdf
ال ثريد في البرمجة.pdfال ثريد في البرمجة.pdf
ال ثريد في البرمجة.pdfssuser893014
 
الموائمة ...pdf
الموائمة ...pdfالموائمة ...pdf
الموائمة ...pdfssuser893014
 
موائمة الانسان مع الكمبيوتر pdf
موائمة الانسان مع الكمبيوتر pdfموائمة الانسان مع الكمبيوتر pdf
موائمة الانسان مع الكمبيوتر pdfssuser893014
 
null-3_Cookies & Sessions in php.pdf
null-3_Cookies & Sessions in php.pdfnull-3_Cookies & Sessions in php.pdf
null-3_Cookies & Sessions in php.pdfssuser893014
 
1_Introduction to human computer interaction.pdf
1_Introduction to human computer interaction.pdf1_Introduction to human computer interaction.pdf
1_Introduction to human computer interaction.pdfssuser893014
 
Machine learning fundamentals.pptx
Machine learning fundamentals.pptxMachine learning fundamentals.pptx
Machine learning fundamentals.pptxssuser893014
 

More from ssuser893014 (7)

ال ثريد في البرمجة.pdf
ال ثريد في البرمجة.pdfال ثريد في البرمجة.pdf
ال ثريد في البرمجة.pdf
 
الموائمة ...pdf
الموائمة ...pdfالموائمة ...pdf
الموائمة ...pdf
 
موائمة الانسان مع الكمبيوتر pdf
موائمة الانسان مع الكمبيوتر pdfموائمة الانسان مع الكمبيوتر pdf
موائمة الانسان مع الكمبيوتر pdf
 
null-3_Cookies & Sessions in php.pdf
null-3_Cookies & Sessions in php.pdfnull-3_Cookies & Sessions in php.pdf
null-3_Cookies & Sessions in php.pdf
 
1_Introduction to human computer interaction.pdf
1_Introduction to human computer interaction.pdf1_Introduction to human computer interaction.pdf
1_Introduction to human computer interaction.pdf
 
web2_lec6.pdf
web2_lec6.pdfweb2_lec6.pdf
web2_lec6.pdf
 
Machine learning fundamentals.pptx
Machine learning fundamentals.pptxMachine learning fundamentals.pptx
Machine learning fundamentals.pptx
 

Recently uploaded

Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 

Recently uploaded (20)

Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 

Process Synchronization Techniques

  • 1. 6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7th Edition, Feb 8, 2005 Process Synchronization Background Critical-Section Deadlock and Starvation  Concvurrent access to shared data may result in data inconsistency  Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes
  • 2. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7th Edition, Feb 8, 2005 Solve the Critical-Section 1. Peterson’s Solution : software solution :can be interrupted :waiting problem 2. Test Memory Word and Set : Hardware : :waiting problem 3. swap contents of two memory words : Hardware : :waiting problem  Semaphore 1- Semaphore with waiting :waiting problem 2- Semaphore with no Busy waiting
  • 3. 6.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7th Edition, Feb 8, 2005 Peterson’s Solution  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 Pi is ready!
  • 4. 6.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7th Edition, Feb 8, 2005 Algorithm for Process Pi while (true) { flag[i] = TRUE; turn = j; while ( flag[j] && turn == j); CRITICAL SECTION flag[i] = FALSE; REMAINDER SECTION }
  • 5. 6.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7th Edition, Feb 8, 2005 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 1. Either test memory word and set value 2. Or swap contents of two memory words
  • 6. 6.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7th Edition, Feb 8, 2005 TestAndndSet Instruction  Definition: boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv: }
  • 7. 6.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7th Edition, Feb 8, 2005 Solution using TestAndSet  Shared boolean variable lock., initialized to false.  Solution: while (true) { while ( TestAndSet (&lock )) ; /* do nothing // critical section lock = FALSE; // remainder section }
  • 8. 6.8 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7th Edition, Feb 8, 2005 Semaphore  Synchronization tool that does not require busy waiting  Semaphore S – integer variable  Two standard operations modify S: wait() and signal()  Originally called P() and V()  Less complicated  Can only be accessed via two indivisible (atomic) operations  Two Methods: 1- Semaphore with waiting 2- Semaphore with no Busy waiting  wait (S) { while S = 0 ; // no-op S--; } C.S. signal (S) { S++; }
  • 9. 6.9 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7th Edition, Feb 8, 2005 Semaphore as General Synchronization Tool  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  Also known as mutex locks  Can implement a counting semaphore S as a binary semaphore  Provides mutual exclusion  Semaphore S; // initialized to 1  wait (S); Critical Section signal (S);
  • 10. 6.10 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7th Edition, Feb 8, 2005 Semaphore Implementation  Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the same time  Thus, implementation becomes the critical section problem where the wait and signal code are placed in the crtical section.  Could now have busy waiting in critical section implementation  But implementation code is short  Little busy waiting if critical section rarely occupied  Note that applications may spend lots of time in critical sections and therefore this is not a good solution.
  • 11. 6.11 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7th Edition, Feb 8, 2005 Semaphore Implementation with no Busy waiting  With 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 list  Two operations:  block – place the process invoking the operation on the appropriate waiting queue.  wakeup – remove one of processes in the waiting queue and place it in the ready queue.
  • 12. 6.12 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7th Edition, Feb 8, 2005 Semaphore Implementation with no Busy waiting (Cont.)  Implementation of wait: wait (S){ value--; if (value < 0) { add this process to waiting queue block(); } }  Implementation of signal: Signal (S){ value++; if (value <= 0) { remove a process P from the waiting queue wakeup(P); } }
  • 13. 6.13 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7th Edition, Feb 8, 2005 Deadlock and Starvation  Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes  Let S and Q be two semaphores initialized to 1 P0 P1 wait (S); wait (Q); wait (Q); wait (S); . . . . . . signal (S); signal (Q); signal (Q); signal (S);  Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended.
  • 14. 6.14 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7th Edition, Feb 8, 2005 Dining-Philosophers Problem  Shared data  Bowl of rice (data set)  Semaphore chopstick [5] initialized to 1