SlideShare a Scribd company logo
1 of 14
UNIT – III
PROCESS SYNCHRONIZATION
What is Semaphore?
Semaphore is simply a variable that is non-negative and shared
between threads. A semaphore is a signaling mechanism, and a
thread that is waiting on a semaphore can be signaled by another
thread. It uses two atomic operations, 1) Wait, and 2) Signal for the
process synchronization.
A semaphore either allows or disallows access to the resource,
which depends on how it is set up.
Characteristic of Semaphore
 It is a mechanism that can be used to provide synchronization
of tasks.
 It is a low-level synchronization mechanism.
 Semaphore will always hold a non-negative integer value.
 Semaphore can be implemented using test operations and
interrupts, which should be executed using file descriptors.
Types of Semaphores
.
SEMAPHORE
COUNTING
SEMAPHORE
BINARY
SEMAPHORE
Counting Semaphores
This type of Semaphore uses a count that helps task to be acquired
or released numerous times. If the initial count = 0, the counting
semaphore should be created in the unavailable state.
However, If the count is > 0, the semaphore is created in the
available state, and the number of tokens it has equals to its count.
Binary Semaphores
The binary semaphores are quite similar to counting semaphores,
but their value is restricted to 0 and 1. In this type of semaphore,
the wait operation works only if semaphore = 1, and the signal
operation succeeds when semaphore= 0. It is easy to implement
than counting semaphores.
Counting Semaphore vs. Binary Semaphore
Counting Semaphore Binary Semaphore
No mutual exclusion Mutual exclusion
Any integer value Value only 0 and 1
More than one slot Only one slot
Provide a set of Processes It has a mutual exclusion mechanism.
The classical definitions of wait and signal are:
 Wait: This operation decrements the value of its argument S,
as soon as it would become non-negative(greater than or equal
to 1). This Operation mainly helps you to control the entry of a
task into the critical section. In the case of the negative or zero
value, no operation is executed. wait() operation was originally
termed as P; so it is also known as P(S) operation.
The definition of wait operation is as follows:
wait(S)
{
while (S<=0);//no operation
S--;
}
 Signal: Increments the value of its argument S, as there is no
more process blocked on the queue. This Operation is mainly
used to control the exit of a task from the critical
section.signal() operation was originally termed as V; so it is
also known as V(S) operation. The definition of signal
operation is as follows:
signal(S)
{
S++;
}
The classical problem depicting flaws of process synchronization in
systems where cooperating processes are present.
Three problems
1. Bounded Buffer (Producer-Consumer) Problem
2. Dining Philosophers Problem
3. The Readers Writers Problem
Bounded Buffer Problem
Because the buffer pool has a maximum size, this problem is often
called the Bounded buffer problem.
 This problem is generalised in terms of the Producer
Consumer problem, where a finite buffer pool is used to
exchange messages between producer and consumer
processes.
 Solution to this problem is, creating two counting semaphores
"full" and "empty" to keep track of the current number of full
and empty buffers respectively.
 In this Producers mainly produces a product and consumers
consume the product, but both can use of one of the
containers each time.
 The main complexity of this problem is that we must have to
maintain the count for both empty and full containers that are
available.
Dining Philosophers Problem
 The dining philosopher's problem involves the allocation of
limited resources to a group of processes in a deadlock-free
and starvation-free manner.
 There are five philosophers sitting around a table, in which
there are five chopsticks/forks kept beside them and a bowl of
rice in the centre, When a philosopher wants to eat, he uses
two chopsticks - one from their left and one from their right.
When a philosopher wants to think, he keeps down both
chopsticks at their original place.
The Readers Writers Problem
 In this problem there are some processes(called readers) that
only read the shared data, and never change it, and there are
other processes(called writers) who may change the data in
addition to reading, or instead of reading it.
 There are various type of readers-writers problem, most
centred on relative priorities of readers and writers.
 The main complexity with this problem occurs from allowing
more than one reader to access the data at the same time.
Bounded Buffer Problem
Bounded buffer problem, which is also called producer consumer
problem, is one of the classic problems of synchronization. Let's
start by understanding the problem here, before moving on to the
solution and program code.
Problem Statement:
There is a buffer of n slots and each slot is capable of storing one
unit of data. There are two processes running,
namely, producer and consumer, which are operating on the
buffer.
Bounded Buffer Problem
A producer tries to insert data into an empty slot of the buffer. A
consumer tries to remove data from a filled slot in the buffer. As
you might have guessed by now, those two processes won't produce
the expected output if they are being executed concurrently.
There needs to be a way to make the producer and consumer work
in an independent manner.
Solution:
One solution of this problem is to use semaphores. The semaphores
which will be used here are:
 m, a binary semaphore which is used to acquire and release
the lock.
 empty, a counting semaphore whose initial value is the
number of slots in the buffer, since, initially all slots are
empty.
 full, a counting semaphore whose initial value is 0.
At any instant, the current value of empty represents the number of
empty slots in the buffer and full represents the number of
occupied slots in the buffer.
The Producer Operation
The pseudocode of the producer function looks like this:
do
{
// wait until empty > 0 and then decrement 'empty'
wait(empty);
// acquire lock
wait(mutex);
/* perform the insert operation in a slot */
// release lock
signal(mutex);
// increment 'full'
signal(full);
}
while(TRUE)
Copy
 Looking at the above code for a producer, we can see that a
producer first waits until there is atleast one empty slot.
 Then it decrements the empty semaphore because, there will
now be one less empty slot, since the producer is going to
insert data in one of those slots.
 Then, it acquires lock on the buffer, so that the consumer
cannot access the buffer until producer completes its
operation.
 After performing the insert operation, the lock is released and
the value of full is incremented because the producer has just
filled a slot in the buffer.
The Consumer Operation
The pseudocode for the consumer function looks like this:
do
{
// wait until full > 0 and then decrement 'full'
wait(full);
// acquire the lock
wait(mutex);
/* perform the remove operation in a slot */
// release the lock
signal(mutex);
// increment 'empty'
signal(empty);
}
while(TRUE);
Copy
 The consumer waits until there is atleast one full slot in the
buffer.
 Then it decrements the full semaphore because the number of
occupied slots will be decreased by one, after the consumer
completes its operation.
 After that, the consumer acquires lock on the buffer.
 Following that, the consumer completes the removal operation
so that the data from one of the full slots is removed.
 Then, the consumer releases the lock.
 Finally, the empty semaphore is incremented by 1, because
the consumer has just removed data from an occupied slot,
thus making it empty.
Dining Philosophers Problem
The dining philosophers problem is another classic
synchronization problem which is used to evaluate situations where
there is a need of allocating multiple resources to multiple
processes.
Problem Statement
Consider there are five philosophers sitting around a circular dining
table. The dining table has five chopsticks and a bowl of rice in the
middle as shown in the below figure.
Dining Philosophers Problem
At any instant, a philosopher is either eating or thinking. When a
philosopher wants to eat, he uses two chopsticks - one from their
left and one from their right. When a philosopher wants to think, he
keeps down both chopsticks at their original place.
Solution
From the problem statement, it is clear that a philosopher can
think for an indefinite amount of time. But when a philosopher
starts eating, he has to stop at some point of time. The philosopher
is in an endless cycle of thinking and eating.
An array of five semaphores, stick[5], for each of the five chopsticks.
The code for each philosopher looks like:
while(TRUE)
{
wait(stick[i]);
/*
mod is used because if i=5, next
chopstick is 1 (dining table is circular)
*/
wait(stick[(i+1) % 5]);
/* eat */
signal(stick[i]);
signal(stick[(i+1) % 5]);
/* think */
}
Copy
When a philosopher wants to eat the rice, he will wait for the
chopstick at his left and picks up that chopstick. Then he waits for
the right chopstick to be available, and then picks it too. After
eating, he puts both the chopsticks down.
But if all five philosophers are hungry simultaneously, and each of
them pickup one chopstick, then a deadlock situation occurs
because they will be waiting for another chopstick forever. The
possible solutions for this are:
 A philosopher must be allowed to pick up the chopsticks only
if both the left and right chopsticks are available.
 Allow only four philosophers to sit at the table. That way, if all
the four philosophers pick up four chopsticks, there will be
one chopstick left on the table. So, one philosopher can start
eating and eventually, two chopsticks will be available. In this
way, deadlocks can be avoided.
What is Readers Writer Problem?
Readers writer problem is another example of a classic
synchronization problem. There are many variants of this problem,
one of which is examined below.
The Problem Statement
There is a shared resource which should be accessed by multiple
processes. There are two types of processes in this context. They
are reader and writer. Any number of readers can read from the
shared resource simultaneously, but only one writer can write to
the shared resource. When a writer is writing data to the resource,
no other process can access the resource. A writer cannot write to
the resource if there are non zero number of readers accessing the
resource at that time.
The Solution
From the above problem statement, it is evident that readers have
higher priority than writer. If a writer wants to write to the resource,
it must wait until there are no readers currently accessing that
resource.
Here, we use one mutex m and a semaphore w. An integer
variable read_count is used to maintain the number of readers
currently accessing the resource. The variable read_count is
initialized to 0. A value of 1 is given initially to m and w.
Instead of having the process to acquire lock on the shared
resource, we use the mutex m to make the process to acquire and
release lock whenever it is updating the read_count variable.
The code for the writer process looks like this:
while(TRUE)
{
wait(w);
/* perform the write operation */
signal(w);
}
Copy
And, the code for the reader process looks like this:
while(TRUE)
{
//acquire lock
wait(m);
read_count++;
if(read_count == 1)
wait(w);
//release lock
signal(m);
/* perform the reading operation */
// acquire lock
wait(m);
read_count--;
if(read_count == 0)
signal(w);
// release lock
signal(m);
}
Copy
Here is the Code uncoded(explained)
 As seen above in the code for the writer, the writer just waits
on the w semaphore until it gets a chance to write to the
resource.
 After performing the write operation, it increments w so that
the next writer can access the resource.
 On the other hand, in the code for the reader, the lock is
acquired whenever the read_count is updated by a process.
 When a reader wants to access the resource, first it
increments the read_count value, then accesses the resource
and then decrements the read_count value.
 The semaphore w is used by the first reader which enters the
critical section and the last reader which exits the critical
section.
 The reason for this is, when the first readers enters the critical
section, the writer is blocked from the resource. Only new
readers can access the resource now.
 Similarly, when the last reader exits the critical section, it
signals the writer using the w semaphore because there are
zero readers now and a writer can have the chance to access
the resource.

More Related Content

Similar to UNIT III Process Synchronization.docx

Operating systems
Operating systemsOperating systems
Operating systemsAbraham
 
Dining Philosopher Problem and Solution
Dining Philosopher Problem and SolutionDining Philosopher Problem and Solution
Dining Philosopher Problem and SolutionLahiru Danushka
 
Process cooperation and synchronisation
Process cooperation and synchronisation Process cooperation and synchronisation
Process cooperation and synchronisation JayeshGadhave1
 
Producer consumer.docx
Producer consumer.docxProducer consumer.docx
Producer consumer.docxAbhishek361641
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronizationlodhran-hayat
 
Classical problem of synchronization
Classical problem of synchronizationClassical problem of synchronization
Classical problem of synchronizationShakshi Ranawat
 
Semaphores and Monitors
 Semaphores and Monitors Semaphores and Monitors
Semaphores and Monitorssathish sak
 
Repetition Control and IO ErrorsPlease note that the mate.docx
Repetition Control and IO ErrorsPlease note that the mate.docxRepetition Control and IO ErrorsPlease note that the mate.docx
Repetition Control and IO ErrorsPlease note that the mate.docxsodhi3
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationAnas Ebrahim
 
Notes2
Notes2Notes2
Notes2hccit
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating SystemsRitu Ranjan Shrivastwa
 
Interprocess Communication
Interprocess CommunicationInterprocess Communication
Interprocess CommunicationDilum Bandara
 
UNIT 2-UNDERSTANDING THE SYNCHRONIZATION PROCESS.pptx
UNIT 2-UNDERSTANDING THE SYNCHRONIZATION PROCESS.pptxUNIT 2-UNDERSTANDING THE SYNCHRONIZATION PROCESS.pptx
UNIT 2-UNDERSTANDING THE SYNCHRONIZATION PROCESS.pptxLeahRachael
 
Describe synchronization techniques used by programmers who develop .pdf
Describe synchronization techniques used by programmers who develop .pdfDescribe synchronization techniques used by programmers who develop .pdf
Describe synchronization techniques used by programmers who develop .pdfexcellentmobiles
 
Linux synchronization tools
Linux synchronization toolsLinux synchronization tools
Linux synchronization toolsmukul bhardwaj
 

Similar to UNIT III Process Synchronization.docx (20)

Operating systems
Operating systemsOperating systems
Operating systems
 
Dining Philosopher Problem and Solution
Dining Philosopher Problem and SolutionDining Philosopher Problem and Solution
Dining Philosopher Problem and Solution
 
Process cooperation and synchronisation
Process cooperation and synchronisation Process cooperation and synchronisation
Process cooperation and synchronisation
 
Producer consumer.docx
Producer consumer.docxProducer consumer.docx
Producer consumer.docx
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
Process coordination
Process coordinationProcess coordination
Process coordination
 
Classical problem of synchronization
Classical problem of synchronizationClassical problem of synchronization
Classical problem of synchronization
 
Semaphores and Monitors
 Semaphores and Monitors Semaphores and Monitors
Semaphores and Monitors
 
Synchronization problems
Synchronization problemsSynchronization problems
Synchronization problems
 
Repetition Control and IO ErrorsPlease note that the mate.docx
Repetition Control and IO ErrorsPlease note that the mate.docxRepetition Control and IO ErrorsPlease note that the mate.docx
Repetition Control and IO ErrorsPlease note that the mate.docx
 
Concurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and SynchronizationConcurrency: Mutual Exclusion and Synchronization
Concurrency: Mutual Exclusion and Synchronization
 
Notes2
Notes2Notes2
Notes2
 
Os3
Os3Os3
Os3
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
Interprocess Communication
Interprocess CommunicationInterprocess Communication
Interprocess Communication
 
UNIT 2-UNDERSTANDING THE SYNCHRONIZATION PROCESS.pptx
UNIT 2-UNDERSTANDING THE SYNCHRONIZATION PROCESS.pptxUNIT 2-UNDERSTANDING THE SYNCHRONIZATION PROCESS.pptx
UNIT 2-UNDERSTANDING THE SYNCHRONIZATION PROCESS.pptx
 
Semaphore
SemaphoreSemaphore
Semaphore
 
Describe synchronization techniques used by programmers who develop .pdf
Describe synchronization techniques used by programmers who develop .pdfDescribe synchronization techniques used by programmers who develop .pdf
Describe synchronization techniques used by programmers who develop .pdf
 
Linux synchronization tools
Linux synchronization toolsLinux synchronization tools
Linux synchronization tools
 
1 Synchronization.pptx
1 Synchronization.pptx1 Synchronization.pptx
1 Synchronization.pptx
 

More from karthikaparthasarath

BASIC COMPUTER ORGANIZATION unit 1.pptx
BASIC COMPUTER ORGANIZATION unit 1.pptxBASIC COMPUTER ORGANIZATION unit 1.pptx
BASIC COMPUTER ORGANIZATION unit 1.pptxkarthikaparthasarath
 
Fundamentals of Computers MCQS.docx
Fundamentals of Computers MCQS.docxFundamentals of Computers MCQS.docx
Fundamentals of Computers MCQS.docxkarthikaparthasarath
 
Software Engineering Question Bank.docx
Software Engineering Question Bank.docxSoftware Engineering Question Bank.docx
Software Engineering Question Bank.docxkarthikaparthasarath
 
BASIC COMPUTER ORGANIZATION unit 1.pptx
BASIC COMPUTER ORGANIZATION unit 1.pptxBASIC COMPUTER ORGANIZATION unit 1.pptx
BASIC COMPUTER ORGANIZATION unit 1.pptxkarthikaparthasarath
 
ATTACKER TECHNIQUES AND MOTIVATION.pptx
ATTACKER TECHNIQUES AND MOTIVATION.pptxATTACKER TECHNIQUES AND MOTIVATION.pptx
ATTACKER TECHNIQUES AND MOTIVATION.pptxkarthikaparthasarath
 
Unit - I cyber security fundamentals part -1.pptx
Unit - I cyber security fundamentals part -1.pptxUnit - I cyber security fundamentals part -1.pptx
Unit - I cyber security fundamentals part -1.pptxkarthikaparthasarath
 
BUilt in Functions and Simple programs in R.pdf
BUilt in Functions and Simple programs in R.pdfBUilt in Functions and Simple programs in R.pdf
BUilt in Functions and Simple programs in R.pdfkarthikaparthasarath
 
Heuristic Search Techniques Unit -II.ppt
Heuristic Search Techniques Unit -II.pptHeuristic Search Techniques Unit -II.ppt
Heuristic Search Techniques Unit -II.pptkarthikaparthasarath
 
Heuristic Search Techniques Unit -II.ppt
Heuristic Search Techniques Unit -II.pptHeuristic Search Techniques Unit -II.ppt
Heuristic Search Techniques Unit -II.pptkarthikaparthasarath
 
Android Lab Mannual 18SUITSP5.docx
Android Lab Mannual 18SUITSP5.docxAndroid Lab Mannual 18SUITSP5.docx
Android Lab Mannual 18SUITSP5.docxkarthikaparthasarath
 
Cyber Security Unit I Part -I.pptx
Cyber Security Unit I Part -I.pptxCyber Security Unit I Part -I.pptx
Cyber Security Unit I Part -I.pptxkarthikaparthasarath
 

More from karthikaparthasarath (20)

BASIC COMPUTER ORGANIZATION unit 1.pptx
BASIC COMPUTER ORGANIZATION unit 1.pptxBASIC COMPUTER ORGANIZATION unit 1.pptx
BASIC COMPUTER ORGANIZATION unit 1.pptx
 
Fundamentals of Computers MCQS.docx
Fundamentals of Computers MCQS.docxFundamentals of Computers MCQS.docx
Fundamentals of Computers MCQS.docx
 
Software Engineering Question Bank.docx
Software Engineering Question Bank.docxSoftware Engineering Question Bank.docx
Software Engineering Question Bank.docx
 
BASIC COMPUTER ORGANIZATION unit 1.pptx
BASIC COMPUTER ORGANIZATION unit 1.pptxBASIC COMPUTER ORGANIZATION unit 1.pptx
BASIC COMPUTER ORGANIZATION unit 1.pptx
 
ATTACKER TECHNIQUES AND MOTIVATION.pptx
ATTACKER TECHNIQUES AND MOTIVATION.pptxATTACKER TECHNIQUES AND MOTIVATION.pptx
ATTACKER TECHNIQUES AND MOTIVATION.pptx
 
Unit - I cyber security fundamentals part -1.pptx
Unit - I cyber security fundamentals part -1.pptxUnit - I cyber security fundamentals part -1.pptx
Unit - I cyber security fundamentals part -1.pptx
 
BUilt in Functions and Simple programs in R.pdf
BUilt in Functions and Simple programs in R.pdfBUilt in Functions and Simple programs in R.pdf
BUilt in Functions and Simple programs in R.pdf
 
Heuristic Search Techniques Unit -II.ppt
Heuristic Search Techniques Unit -II.pptHeuristic Search Techniques Unit -II.ppt
Heuristic Search Techniques Unit -II.ppt
 
simple programs.docx
simple programs.docxsimple programs.docx
simple programs.docx
 
Heuristic Search Techniques Unit -II.ppt
Heuristic Search Techniques Unit -II.pptHeuristic Search Techniques Unit -II.ppt
Heuristic Search Techniques Unit -II.ppt
 
Android Lab Mannual 18SUITSP5.docx
Android Lab Mannual 18SUITSP5.docxAndroid Lab Mannual 18SUITSP5.docx
Android Lab Mannual 18SUITSP5.docx
 
Activity playfair cipher.pptx
Activity playfair cipher.pptxActivity playfair cipher.pptx
Activity playfair cipher.pptx
 
Activity Hill Cipher.pptx
Activity  Hill Cipher.pptxActivity  Hill Cipher.pptx
Activity Hill Cipher.pptx
 
Activity Caesar Cipher.pptx
Activity Caesar Cipher.pptxActivity Caesar Cipher.pptx
Activity Caesar Cipher.pptx
 
Cyber Security Unit I Part -I.pptx
Cyber Security Unit I Part -I.pptxCyber Security Unit I Part -I.pptx
Cyber Security Unit I Part -I.pptx
 
Unit I Q&A.docx
Unit I Q&A.docxUnit I Q&A.docx
Unit I Q&A.docx
 
Unit 1 QB.docx
Unit 1 QB.docxUnit 1 QB.docx
Unit 1 QB.docx
 
cyber security.pptx
cyber security.pptxcyber security.pptx
cyber security.pptx
 
UNIT I - Part 1.pptx
UNIT I - Part 1.pptxUNIT I - Part 1.pptx
UNIT I - Part 1.pptx
 
UNIT II - CPU SCHEDULING.docx
UNIT II - CPU SCHEDULING.docxUNIT II - CPU SCHEDULING.docx
UNIT II - CPU SCHEDULING.docx
 

Recently uploaded

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
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
 
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
 
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
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
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
 
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
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
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
 
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
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 

Recently uploaded (20)

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
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
 
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
 
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
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
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
 
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...
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
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
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
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
 
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
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 

UNIT III Process Synchronization.docx

  • 1. UNIT – III PROCESS SYNCHRONIZATION What is Semaphore? Semaphore is simply a variable that is non-negative and shared between threads. A semaphore is a signaling mechanism, and a thread that is waiting on a semaphore can be signaled by another thread. It uses two atomic operations, 1) Wait, and 2) Signal for the process synchronization. A semaphore either allows or disallows access to the resource, which depends on how it is set up. Characteristic of Semaphore  It is a mechanism that can be used to provide synchronization of tasks.  It is a low-level synchronization mechanism.  Semaphore will always hold a non-negative integer value.  Semaphore can be implemented using test operations and interrupts, which should be executed using file descriptors. Types of Semaphores . SEMAPHORE COUNTING SEMAPHORE BINARY SEMAPHORE
  • 2. Counting Semaphores This type of Semaphore uses a count that helps task to be acquired or released numerous times. If the initial count = 0, the counting semaphore should be created in the unavailable state. However, If the count is > 0, the semaphore is created in the available state, and the number of tokens it has equals to its count. Binary Semaphores The binary semaphores are quite similar to counting semaphores, but their value is restricted to 0 and 1. In this type of semaphore, the wait operation works only if semaphore = 1, and the signal operation succeeds when semaphore= 0. It is easy to implement than counting semaphores. Counting Semaphore vs. Binary Semaphore Counting Semaphore Binary Semaphore No mutual exclusion Mutual exclusion Any integer value Value only 0 and 1 More than one slot Only one slot
  • 3. Provide a set of Processes It has a mutual exclusion mechanism. The classical definitions of wait and signal are:  Wait: This operation decrements the value of its argument S, as soon as it would become non-negative(greater than or equal to 1). This Operation mainly helps you to control the entry of a task into the critical section. In the case of the negative or zero value, no operation is executed. wait() operation was originally termed as P; so it is also known as P(S) operation. The definition of wait operation is as follows: wait(S) { while (S<=0);//no operation S--; }  Signal: Increments the value of its argument S, as there is no more process blocked on the queue. This Operation is mainly used to control the exit of a task from the critical section.signal() operation was originally termed as V; so it is also known as V(S) operation. The definition of signal operation is as follows: signal(S) { S++; } The classical problem depicting flaws of process synchronization in systems where cooperating processes are present.
  • 4. Three problems 1. Bounded Buffer (Producer-Consumer) Problem 2. Dining Philosophers Problem 3. The Readers Writers Problem Bounded Buffer Problem Because the buffer pool has a maximum size, this problem is often called the Bounded buffer problem.  This problem is generalised in terms of the Producer Consumer problem, where a finite buffer pool is used to exchange messages between producer and consumer processes.  Solution to this problem is, creating two counting semaphores "full" and "empty" to keep track of the current number of full and empty buffers respectively.  In this Producers mainly produces a product and consumers consume the product, but both can use of one of the containers each time.  The main complexity of this problem is that we must have to maintain the count for both empty and full containers that are available. Dining Philosophers Problem  The dining philosopher's problem involves the allocation of limited resources to a group of processes in a deadlock-free and starvation-free manner.  There are five philosophers sitting around a table, in which there are five chopsticks/forks kept beside them and a bowl of rice in the centre, When a philosopher wants to eat, he uses two chopsticks - one from their left and one from their right. When a philosopher wants to think, he keeps down both chopsticks at their original place. The Readers Writers Problem
  • 5.  In this problem there are some processes(called readers) that only read the shared data, and never change it, and there are other processes(called writers) who may change the data in addition to reading, or instead of reading it.  There are various type of readers-writers problem, most centred on relative priorities of readers and writers.  The main complexity with this problem occurs from allowing more than one reader to access the data at the same time. Bounded Buffer Problem Bounded buffer problem, which is also called producer consumer problem, is one of the classic problems of synchronization. Let's start by understanding the problem here, before moving on to the solution and program code. Problem Statement: There is a buffer of n slots and each slot is capable of storing one unit of data. There are two processes running, namely, producer and consumer, which are operating on the buffer. Bounded Buffer Problem A producer tries to insert data into an empty slot of the buffer. A consumer tries to remove data from a filled slot in the buffer. As
  • 6. you might have guessed by now, those two processes won't produce the expected output if they are being executed concurrently. There needs to be a way to make the producer and consumer work in an independent manner. Solution: One solution of this problem is to use semaphores. The semaphores which will be used here are:  m, a binary semaphore which is used to acquire and release the lock.  empty, a counting semaphore whose initial value is the number of slots in the buffer, since, initially all slots are empty.  full, a counting semaphore whose initial value is 0. At any instant, the current value of empty represents the number of empty slots in the buffer and full represents the number of occupied slots in the buffer. The Producer Operation The pseudocode of the producer function looks like this: do { // wait until empty > 0 and then decrement 'empty' wait(empty); // acquire lock wait(mutex); /* perform the insert operation in a slot */
  • 7. // release lock signal(mutex); // increment 'full' signal(full); } while(TRUE) Copy  Looking at the above code for a producer, we can see that a producer first waits until there is atleast one empty slot.  Then it decrements the empty semaphore because, there will now be one less empty slot, since the producer is going to insert data in one of those slots.  Then, it acquires lock on the buffer, so that the consumer cannot access the buffer until producer completes its operation.  After performing the insert operation, the lock is released and the value of full is incremented because the producer has just filled a slot in the buffer. The Consumer Operation The pseudocode for the consumer function looks like this: do { // wait until full > 0 and then decrement 'full' wait(full); // acquire the lock wait(mutex);
  • 8. /* perform the remove operation in a slot */ // release the lock signal(mutex); // increment 'empty' signal(empty); } while(TRUE); Copy  The consumer waits until there is atleast one full slot in the buffer.  Then it decrements the full semaphore because the number of occupied slots will be decreased by one, after the consumer completes its operation.  After that, the consumer acquires lock on the buffer.  Following that, the consumer completes the removal operation so that the data from one of the full slots is removed.  Then, the consumer releases the lock.  Finally, the empty semaphore is incremented by 1, because the consumer has just removed data from an occupied slot, thus making it empty. Dining Philosophers Problem The dining philosophers problem is another classic synchronization problem which is used to evaluate situations where there is a need of allocating multiple resources to multiple processes.
  • 9. Problem Statement Consider there are five philosophers sitting around a circular dining table. The dining table has five chopsticks and a bowl of rice in the middle as shown in the below figure. Dining Philosophers Problem At any instant, a philosopher is either eating or thinking. When a philosopher wants to eat, he uses two chopsticks - one from their left and one from their right. When a philosopher wants to think, he keeps down both chopsticks at their original place. Solution From the problem statement, it is clear that a philosopher can think for an indefinite amount of time. But when a philosopher
  • 10. starts eating, he has to stop at some point of time. The philosopher is in an endless cycle of thinking and eating. An array of five semaphores, stick[5], for each of the five chopsticks. The code for each philosopher looks like: while(TRUE) { wait(stick[i]); /* mod is used because if i=5, next chopstick is 1 (dining table is circular) */ wait(stick[(i+1) % 5]); /* eat */ signal(stick[i]); signal(stick[(i+1) % 5]); /* think */ } Copy
  • 11. When a philosopher wants to eat the rice, he will wait for the chopstick at his left and picks up that chopstick. Then he waits for the right chopstick to be available, and then picks it too. After eating, he puts both the chopsticks down. But if all five philosophers are hungry simultaneously, and each of them pickup one chopstick, then a deadlock situation occurs because they will be waiting for another chopstick forever. The possible solutions for this are:  A philosopher must be allowed to pick up the chopsticks only if both the left and right chopsticks are available.  Allow only four philosophers to sit at the table. That way, if all the four philosophers pick up four chopsticks, there will be one chopstick left on the table. So, one philosopher can start eating and eventually, two chopsticks will be available. In this way, deadlocks can be avoided. What is Readers Writer Problem? Readers writer problem is another example of a classic synchronization problem. There are many variants of this problem, one of which is examined below. The Problem Statement There is a shared resource which should be accessed by multiple processes. There are two types of processes in this context. They are reader and writer. Any number of readers can read from the shared resource simultaneously, but only one writer can write to the shared resource. When a writer is writing data to the resource, no other process can access the resource. A writer cannot write to the resource if there are non zero number of readers accessing the resource at that time. The Solution
  • 12. From the above problem statement, it is evident that readers have higher priority than writer. If a writer wants to write to the resource, it must wait until there are no readers currently accessing that resource. Here, we use one mutex m and a semaphore w. An integer variable read_count is used to maintain the number of readers currently accessing the resource. The variable read_count is initialized to 0. A value of 1 is given initially to m and w. Instead of having the process to acquire lock on the shared resource, we use the mutex m to make the process to acquire and release lock whenever it is updating the read_count variable. The code for the writer process looks like this: while(TRUE) { wait(w); /* perform the write operation */ signal(w); } Copy And, the code for the reader process looks like this: while(TRUE) {
  • 13. //acquire lock wait(m); read_count++; if(read_count == 1) wait(w); //release lock signal(m); /* perform the reading operation */ // acquire lock wait(m); read_count--; if(read_count == 0) signal(w); // release lock signal(m);
  • 14. } Copy Here is the Code uncoded(explained)  As seen above in the code for the writer, the writer just waits on the w semaphore until it gets a chance to write to the resource.  After performing the write operation, it increments w so that the next writer can access the resource.  On the other hand, in the code for the reader, the lock is acquired whenever the read_count is updated by a process.  When a reader wants to access the resource, first it increments the read_count value, then accesses the resource and then decrements the read_count value.  The semaphore w is used by the first reader which enters the critical section and the last reader which exits the critical section.  The reason for this is, when the first readers enters the critical section, the writer is blocked from the resource. Only new readers can access the resource now.  Similarly, when the last reader exits the critical section, it signals the writer using the w semaphore because there are zero readers now and a writer can have the chance to access the resource.