Process Synchronization
CHAPTER OBJECTIVES
• To introduce the critical-section problem, whose solutions can
be used to ensure the consistency of shared data.
• To present both software and hardware solutions of the
critical-section problem.
• To examine several classical process-synchronization
problems.
• To explore several tools that are used to solve process
synchronization problems.
The Critical-Section Problem
• Consider a system consisting of n processes {P0, P1, …, Pn−1}.
• Each process has a segment of code, called a critical section,
in which the process may be changing common variables,
updating a table, writing a file, and so on.
• The important feature of the system is that, when one process
is executing in its critical section, no other process is allowed
to execute in its critical section.
• That is, no two processes are executing in their critical
sections at the same time.
• The critical-section problem is to design a protocol that the
processes can use to cooperate.
• Each process must request permission to enter its critical
section.
• The section of code implementing this request is the entry
section.
• The critical section may be followed by an exit section.
• The remaining code is the remainder section.
A solution to the critical-section problem must satisfy the
following three requirements:
• Mutual exclusion. If process Pi 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
some processes wish to enter their critical sections, then only
those processes that are not executing in their remainder
sections can participate in deciding which will enter its critical
section next, and this selection cannot be postponed
indefinitely.
• Bounded waiting. There exists a bound, or limit, 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.
Common solutions to the Critical Section
Problem
• Peterson’s solution
• Synchronization Hardware
• Mutex Locks
• Semaphore Solution
Peterson's Solution
• Classic software-based solution to the critical-section problem
known as Peterson's solution.
• Peterson's solution is restricted to two processes
• The processes are numbered P0 and P1.
• Peterson's solution requires
– int turn;
– boolean flag[2];
• The variable turn indicates whose turn it is to enter its critical
section.
• That is, if turn == i, then process Pi is allowed to execute in its
critical section.
• The flag array is used to indicate if a process is ready to enter
its critical section.
• For example, if flag[i] is true, this value indicates that Pi is
ready to enter its critical section.
• To enter the critical section, process Pi first sets flag[i] to be
true and then sets turn to the value j
Synchronization Hardware
• Peterson's are not guaranteed to work on modern computer
architectures.
• Hardware features can make any programming task easier
and improve system efficiency.
• The critical-section problem could be solved simply in a single-
processor environment if we could prevent interrupts from
occurring while a shared variable was being modified.
• This solution is not as feasible in a multiprocessor
environment. Disabling interrupts on a multiprocessor can be
time consuming.
Two commonly used Hardware Instructions
• TestandSet()
• Swap()
test_and_set() instruction
• This instruction is used to test & write to the memory location
before setting a value. If process Pi is executing this
instruction, then process Pj has to wait to execute this
instruction.
•
Swap() instruction
• A global variable (lock) is declared and is initialized to 0.
• The first process that invokes compare_and_swap() will set
lock to 1.
• It will then enter its critical section
• When a process exits its critical section, it sets lock back to 0.
• Later allows another process to enter its critical section.
Mutex Locks
• The hardware-based solutions are complicated and
inaccessible to programmers.
• Instead, operating-systems designers build software tools to
solve the critical-section problem.
• The simplest of these tools is the mutex lock (mutual
exclusion).
• A process must acquire the lock before entering a critical
section; it releases the lock when it exits the critical section.
• The acquire() function acquires the lock, and the release()
function releases the lock.
• A mutex lock has a boolean variable available whose value
indicates if the lock is available or not. If the lock is available, a
call to acquire() succeeds.
• The main disadvantage here is that it requires busy waiting.
When a process is in its critical section, any other process that
tries to enter its critical section must call to acquire()
continuously. This type of mutex lock is called as spinlock.
Semaphores
• A semaphore S is a non-negative integer variable which is
shared between threads.
• Semaphore value is accessible through wait() and signal()
operations.
• When one process modifies the semaphore value, no other
process can simultaneously modify that same semaphore
value.
• wait() operation - It decrements the value of Semaphore S.
• signal() operation – It increments the value of Semaphore S.
Types of Semaphores
• Binary Semaphore
• Counting Semaphore
Binary Semaphore
• A Semaphore whose variable is allowed to take only 0 & 1 is
called Binary Semaphore . 0 – busy , 1 – free
• Binary Semaphore are also called as mutex locks, as they
provide mutual exclusion.
Counting Semaphore
• Used to control access to the given resource which consists of
finite number of instances (copies).
• First the semaphore initialized to the no. of resource
available.
• Each process that wishes to use a resource perform a wait()
operation on the semaphore (decrement the count)
• When a process exit from a critical section, it performs signal()
operation (increment the count)
Classic Problems of Synchronization
• There are number of synchronization problems as examples.
• We will discuss following problems as an example.
– The Bounded-Buffer (Producer-Consumer) Problem
– The Readers–Writers Problem
– The Dining-Philosophers Problem
Bounded-Buffer (Producer-Consumer) Problem
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
• Here mutex, empty and full are semaphores
• Mutex is initialized to 1
• Empty is initialized to n
• Full is initialized to 0
• After the item is produced, wait operation is executed on
empty. This indicates the empty space has decreased by 1.
Wait operation is executed on mutex also so that consumer
process should not interfere.
• After the item is put in the buffer, signal operation is carried
out on mutex and full. This indicates that consumer process
can now act
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
• The wait operation is executed on full. This indicates that item
in the buffer have decreased by 1
• Then the wait operation is executed on mutex so that
producer process should not interfere.
• The item is removed from buffer then signal operation is
executed on mutex and empty.
• Consumer process can act now.
The Readers–Writers Problem
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
The Dining-Philosophers Problem
• Consider five philosophers who spend their lives thinking and
eating.
• The philosophers share a circular table surrounded by five
chairs, each belonging to one philosopher.
• In the center of the table is a bowl of rice, and the table is laid
with five single chopsticks.
• When a philosopher gets hungry, tries to pick up the two
chopsticks that are closest to them.
• They cannot pick up a chopstick that is already in the hand of
a neighbor.
• When they finished eating, they put down both chopsticks
and starts thinking again.
One simple solution is to represent each chopstick with a semaphore.
A philosopher tries to grab a chopstick by executing a wait() operation
on that semaphore. They release chopsticks by executing the signal()
operation on the appropriate semaphores.
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
Monitors
Semaphores can result in timing errors that are difficult to
detect. To overcome these errors monitors are used for
achieving process synchronization.
The Monitor is a collection of variables, data structures and
procedures that operates on those variables.
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
• The procedures defined within a monitor can access only
those variables declared within the monitor.
• The variables of the monitor can be accessed only the
procedures within the monitor.
• The monitor ensures that only one process at a time can be
active within the monitor.

More Related Content

PDF
Ch5 process synchronization
PPTX
UNIT-2 - Concurrency & Interprocess Communicatio.pptx
PPT
Process Synchronization -1.ppt
PPTX
MODULE 3 process synchronizationnnn.pptx
PPTX
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PPTX
operating system notes of unit 3 explanation
PPTX
B.Tech. Computer Science Engineering OS Notes Unit 2
PDF
Lecture 5 process synchronization
Ch5 process synchronization
UNIT-2 - Concurrency & Interprocess Communicatio.pptx
Process Synchronization -1.ppt
MODULE 3 process synchronizationnnn.pptx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
operating system notes of unit 3 explanation
B.Tech. Computer Science Engineering OS Notes Unit 2
Lecture 5 process synchronization

Similar to OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf (20)

PPTX
794985751-Unit-3-Inter-Process-Communication.pptx
PPTX
Interprocess Communication important topic in iOS .pptx
DOCX
Critical section operating system
PPTX
synchronization in operating system structure
PPTX
Process cooperation and synchronisation
PPTX
Process synchronization in Operating Systems
PPTX
OS Semaphore.pptx
PPTX
1 Synchronization.pptx
PPTX
Process management in Operating System_Unit-2
PPTX
OS 6.pptx
PPTX
Lecture 5 inter process communication
PPT
Ipc feb4
PPT
Inter process communication
PPTX
Operating system 23 process synchronization
PPT
process syn.ppt
PPTX
Operating system 27 semaphores
PDF
Lecture 5- Process Synchonization_revised.pdf
PPT
Ipc feb4
PPTX
Lecture 5- Process Synchronization (1).pptx
PPTX
5 Inter Process note Communication.pptx
794985751-Unit-3-Inter-Process-Communication.pptx
Interprocess Communication important topic in iOS .pptx
Critical section operating system
synchronization in operating system structure
Process cooperation and synchronisation
Process synchronization in Operating Systems
OS Semaphore.pptx
1 Synchronization.pptx
Process management in Operating System_Unit-2
OS 6.pptx
Lecture 5 inter process communication
Ipc feb4
Inter process communication
Operating system 23 process synchronization
process syn.ppt
Operating system 27 semaphores
Lecture 5- Process Synchonization_revised.pdf
Ipc feb4
Lecture 5- Process Synchronization (1).pptx
5 Inter Process note Communication.pptx
Ad

More from CHETHANKUMAR274045 (12)

PPTX
bug management system important notes.pptx
PPTX
BLOCK-CHAIN for information systemm.pptx
PPT
OPERATING SYSTEM IMPORTANT NOTES_UNIT-4.ppt
DOCX
DIGITAL FLENCY_INTERNAL_IMPORTANT QNS.docx
DOCX
BSC DIGITAL FLUENCY SYLLABUS OVER ALL.docx
PPTX
mechanical_vibration,_unit_2_ppt,_krishna_R [1].pptx
PPTX
introductiontoangularwithasimplebutcompleteproject-171127023401.pptx
PPT
BUSINESS INTELLIGENCE DATA WAREHOUSE.ppt
PPTX
Artificial intelligence AND Machine PPT.pptx
PPTX
NUMPY LIBRARY study materials PPT 2.pptx
PPTX
ANDROID DEVELOPER introduction PPT.pptx
PPTX
PREDICTING ANALYTICS FOR BUSINESS INTELLIGENCE PPT.pptx
bug management system important notes.pptx
BLOCK-CHAIN for information systemm.pptx
OPERATING SYSTEM IMPORTANT NOTES_UNIT-4.ppt
DIGITAL FLENCY_INTERNAL_IMPORTANT QNS.docx
BSC DIGITAL FLUENCY SYLLABUS OVER ALL.docx
mechanical_vibration,_unit_2_ppt,_krishna_R [1].pptx
introductiontoangularwithasimplebutcompleteproject-171127023401.pptx
BUSINESS INTELLIGENCE DATA WAREHOUSE.ppt
Artificial intelligence AND Machine PPT.pptx
NUMPY LIBRARY study materials PPT 2.pptx
ANDROID DEVELOPER introduction PPT.pptx
PREDICTING ANALYTICS FOR BUSINESS INTELLIGENCE PPT.pptx
Ad

Recently uploaded (20)

PDF
Dongguan Sunnew ESS Profile for the year of 2023
PPT
Introduction to Hybrid Electric Vehicles
PPTX
Applications of SAP S4HANA in Mechanical by Sidhant Vohra (SET23A24040166).pptx
PPTX
Independence_Day_Patriotic theme (1).pptx
PDF
Transmission John Deere 370E 410E 460E Technical Manual.pdf
PDF
John Deere 410E II Articulated Dump Truck Service Manual.pdf
PDF
Volvo EC55 Compact Excavator Service Repair Manual Instant Download.pdf
PDF
6. Chapter Twenty_Managing Mass Communications Advertising Sales Promotions E...
PDF
Engine Volvo EC55 Compact Excavator Service Repair Manual.pdf
PPTX
45-Days-of-Engineering-Excellence-132-kV-Grid-Substation-Training.pptx
PPTX
internal combustion engine renewable new
PDF
MES Chapter 3 Combined UNIVERSITY OF VISVESHWARAYA
PPTX
Cloud_Computing_ppt[1].pptx132EQ342RRRRR1
PDF
Diagnostic Trouble Codes John Deere 370E 410E 460E Technical Manual.pdf
PDF
John Deere 410E service Repair Manual.pdf
PDF
Governor Volvo EC55 Service Repair Manual.pdf
PPTX
Training Material_Verification Station.pptx
PPTX
368455847-Relibility RJS-Relibility-PPT-1.pptx
PPTX
Victory precisions_Die casting foundry_.pptx
PPTX
description of motor equipments and its process.pptx
Dongguan Sunnew ESS Profile for the year of 2023
Introduction to Hybrid Electric Vehicles
Applications of SAP S4HANA in Mechanical by Sidhant Vohra (SET23A24040166).pptx
Independence_Day_Patriotic theme (1).pptx
Transmission John Deere 370E 410E 460E Technical Manual.pdf
John Deere 410E II Articulated Dump Truck Service Manual.pdf
Volvo EC55 Compact Excavator Service Repair Manual Instant Download.pdf
6. Chapter Twenty_Managing Mass Communications Advertising Sales Promotions E...
Engine Volvo EC55 Compact Excavator Service Repair Manual.pdf
45-Days-of-Engineering-Excellence-132-kV-Grid-Substation-Training.pptx
internal combustion engine renewable new
MES Chapter 3 Combined UNIVERSITY OF VISVESHWARAYA
Cloud_Computing_ppt[1].pptx132EQ342RRRRR1
Diagnostic Trouble Codes John Deere 370E 410E 460E Technical Manual.pdf
John Deere 410E service Repair Manual.pdf
Governor Volvo EC55 Service Repair Manual.pdf
Training Material_Verification Station.pptx
368455847-Relibility RJS-Relibility-PPT-1.pptx
Victory precisions_Die casting foundry_.pptx
description of motor equipments and its process.pptx

OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf

  • 1. Process Synchronization CHAPTER OBJECTIVES • To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data. • To present both software and hardware solutions of the critical-section problem. • To examine several classical process-synchronization problems. • To explore several tools that are used to solve process synchronization problems.
  • 2. The Critical-Section Problem • Consider a system consisting of n processes {P0, P1, …, Pn−1}. • Each process has a segment of code, called a critical section, in which the process may be changing common variables, updating a table, writing a file, and so on. • The important feature of the system is that, when one process is executing in its critical section, no other process is allowed to execute in its critical section. • That is, no two processes are executing in their critical sections at the same time. • The critical-section problem is to design a protocol that the processes can use to cooperate.
  • 3. • Each process must request permission to enter its critical section. • The section of code implementing this request is the entry section. • The critical section may be followed by an exit section. • The remaining code is the remainder section.
  • 4. A solution to the critical-section problem must satisfy the following three requirements: • Mutual exclusion. If process Pi 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 some processes wish to enter their critical sections, then only those processes that are not executing in their remainder sections can participate in deciding which will enter its critical section next, and this selection cannot be postponed indefinitely. • Bounded waiting. There exists a bound, or limit, 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.
  • 5. Common solutions to the Critical Section Problem • Peterson’s solution • Synchronization Hardware • Mutex Locks • Semaphore Solution
  • 6. Peterson's Solution • Classic software-based solution to the critical-section problem known as Peterson's solution. • Peterson's solution is restricted to two processes • The processes are numbered P0 and P1. • Peterson's solution requires – int turn; – boolean flag[2]; • The variable turn indicates whose turn it is to enter its critical section. • That is, if turn == i, then process Pi is allowed to execute in its critical section.
  • 7. • The flag array is used to indicate if a process is ready to enter its critical section. • For example, if flag[i] is true, this value indicates that Pi is ready to enter its critical section. • To enter the critical section, process Pi first sets flag[i] to be true and then sets turn to the value j
  • 8. Synchronization Hardware • Peterson's are not guaranteed to work on modern computer architectures. • Hardware features can make any programming task easier and improve system efficiency. • The critical-section problem could be solved simply in a single- processor environment if we could prevent interrupts from occurring while a shared variable was being modified. • This solution is not as feasible in a multiprocessor environment. Disabling interrupts on a multiprocessor can be time consuming.
  • 9. Two commonly used Hardware Instructions • TestandSet() • Swap()
  • 10. test_and_set() instruction • This instruction is used to test & write to the memory location before setting a value. If process Pi is executing this instruction, then process Pj has to wait to execute this instruction. •
  • 11. Swap() instruction • A global variable (lock) is declared and is initialized to 0. • The first process that invokes compare_and_swap() will set lock to 1. • It will then enter its critical section • When a process exits its critical section, it sets lock back to 0. • Later allows another process to enter its critical section.
  • 12. Mutex Locks • The hardware-based solutions are complicated and inaccessible to programmers. • Instead, operating-systems designers build software tools to solve the critical-section problem. • The simplest of these tools is the mutex lock (mutual exclusion). • A process must acquire the lock before entering a critical section; it releases the lock when it exits the critical section. • The acquire() function acquires the lock, and the release() function releases the lock.
  • 13. • A mutex lock has a boolean variable available whose value indicates if the lock is available or not. If the lock is available, a call to acquire() succeeds. • The main disadvantage here is that it requires busy waiting. When a process is in its critical section, any other process that tries to enter its critical section must call to acquire() continuously. This type of mutex lock is called as spinlock.
  • 14. Semaphores • A semaphore S is a non-negative integer variable which is shared between threads. • Semaphore value is accessible through wait() and signal() operations. • When one process modifies the semaphore value, no other process can simultaneously modify that same semaphore value.
  • 15. • wait() operation - It decrements the value of Semaphore S. • signal() operation – It increments the value of Semaphore S.
  • 16. Types of Semaphores • Binary Semaphore • Counting Semaphore
  • 17. Binary Semaphore • A Semaphore whose variable is allowed to take only 0 & 1 is called Binary Semaphore . 0 – busy , 1 – free • Binary Semaphore are also called as mutex locks, as they provide mutual exclusion.
  • 18. Counting Semaphore • Used to control access to the given resource which consists of finite number of instances (copies). • First the semaphore initialized to the no. of resource available. • Each process that wishes to use a resource perform a wait() operation on the semaphore (decrement the count) • When a process exit from a critical section, it performs signal() operation (increment the count)
  • 19. Classic Problems of Synchronization • There are number of synchronization problems as examples. • We will discuss following problems as an example. – The Bounded-Buffer (Producer-Consumer) Problem – The Readers–Writers Problem – The Dining-Philosophers Problem
  • 23. • Here mutex, empty and full are semaphores • Mutex is initialized to 1 • Empty is initialized to n • Full is initialized to 0 • After the item is produced, wait operation is executed on empty. This indicates the empty space has decreased by 1. Wait operation is executed on mutex also so that consumer process should not interfere. • After the item is put in the buffer, signal operation is carried out on mutex and full. This indicates that consumer process can now act
  • 25. • The wait operation is executed on full. This indicates that item in the buffer have decreased by 1 • Then the wait operation is executed on mutex so that producer process should not interfere. • The item is removed from buffer then signal operation is executed on mutex and empty. • Consumer process can act now.
  • 28. The Dining-Philosophers Problem • Consider five philosophers who spend their lives thinking and eating. • The philosophers share a circular table surrounded by five chairs, each belonging to one philosopher. • In the center of the table is a bowl of rice, and the table is laid with five single chopsticks. • When a philosopher gets hungry, tries to pick up the two chopsticks that are closest to them. • They cannot pick up a chopstick that is already in the hand of a neighbor. • When they finished eating, they put down both chopsticks and starts thinking again.
  • 29. One simple solution is to represent each chopstick with a semaphore. A philosopher tries to grab a chopstick by executing a wait() operation on that semaphore. They release chopsticks by executing the signal() operation on the appropriate semaphores.
  • 31. Monitors Semaphores can result in timing errors that are difficult to detect. To overcome these errors monitors are used for achieving process synchronization. The Monitor is a collection of variables, data structures and procedures that operates on those variables.
  • 34. • The procedures defined within a monitor can access only those variables declared within the monitor. • The variables of the monitor can be accessed only the procedures within the monitor. • The monitor ensures that only one process at a time can be active within the monitor.