0% found this document useful (0 votes)
19 views35 pages

Final PPT of Os

This document discusses process synchronization in operating systems, focusing on semaphores and their role in managing concurrent processes. It covers various synchronization problems, including the Bounded Buffer, Readers-Writers, and Dining Philosophers problems, providing solutions using semaphores. Additionally, it explains counting semaphores and monitors as tools for resource management and mutual exclusion.

Uploaded by

zb96946
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views35 pages

Final PPT of Os

This document discusses process synchronization in operating systems, focusing on semaphores and their role in managing concurrent processes. It covers various synchronization problems, including the Bounded Buffer, Readers-Writers, and Dining Philosophers problems, providing solutions using semaphores. Additionally, it explains counting semaphores and monitors as tools for resource management and mutual exclusion.

Uploaded by

zb96946
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Operating System:

Mam: Ayesha Ali


Department of IT
UNIVERSITY OF SAHIWAL
Chapter no : 5
Process
synchronization:

Group members:
• Zeenat Masood
• Fozia Nawaz
• Saqlain Abbas
• Sana Imran
 Semaphores:
 Classic problems of synchronization:
 Semaphore:
Semaphore proposed by Edsger Dijkstra , is a technique to
manage concurrent process by using a simple integer value. Semaphore is
an integer variable that solves the critical section problem. After
initialization ,it can be accessed by two following operations.

 P(wait or Down)
 V(Signal and Up)
 Binary Semaphores:

The definition of wait is as The definition of signal is


follows: as follows:

Wait(s) Signal (s)


{ {
While (S<=0);// do s=s+1;
nothing }
s=s-1;
 Solution of critical section problem
using semaphores:

Let p1,p2,p3,p4,……Pn are the process that wants to go to the


critical section:

Do
{
Wait(S)
//Critical section
Signal(S);
//remainder section
}
While(t)
Counting Semaphores:
 Integer value can
range over an unrestricted domain.

Down (Semaphore S)
{
S .Value = S. Value -1;
If(S .Value < 0 )
{
Put Process (PCB) in Suspended list,
Sleep(),
}
else
Return;
}
UP (Semaphore S)
{
S. Value = S . Value+1;
If (S . Value <= 0)
{
Select a process
from Suspended list &
Wake up();
}

}
 There are three criteria to solve the problem
of critical section:
1. Mutual Exclusion
2. Progress
3. Bounded wait

 There are two types of semaphore:


 Counting semaphore:
Integer value can range over an unrestricted
domain.
 Binary semaphore:
Integer value can range only between 0 and
1
 Classic problems of synchronization:
Following are the classic problem of synchronization in operating system:
 Bounded- Buffer Producer –consumer
problem:
Suppose there are producer and consumer process. Producer produes
object, from which consumer uses for something. There is one Buffer
object used to pass object from producers to consumers. A Buffer is used to
store production of producer.
• Problems:
1. Producer must not insert data when buffer is full.
2. Consumer must not remove data when buffer is empty.
3. Producer and consumer should not insert and remove that simultaneously.
• Solution:
Use three semaphores to solve this problem:
Mutex (m) (initialised to 1)
Empty (initialised to n it is the size of buffer)
Full (initialized to 0)
Bounded- Buffer Problem (Producer
consumer Problem)
Size =8

nextc Consumer

Producer nextp
Bounded- Buffer Problem (Producer consumer
Problem)
Semaphore full=0, empty=n , Mutex=1;

Producer process Consumer process:

Do Do
{ {
….. Wait(full);
Produce an item in nextp Wait(mutex);
….. …..
Wait(empty); Remove an item from buffer to
Wait(mutex); nextc
….. …..
Add nextp to buffer Signal (mutex);
….. Signal(empty);
Signal (mutex) …..
Signal(full); Consume the item in nextc
} …..
While(1); }
While(1);
Readers Writers Problem:
Dining philosophers Problem
 Readers Writers Problem:
In this problem, a number of concurrent
processes require access to some object
(such as file). Some processes extract
information from the object and are called
Readers. Some processes change or insert
information in the object and are called
Writers.
 “First readers/writers” problem (reader
priority):
 No reader will wait ( for other readers to finish) even
if a writer is waiting
 Writer starvation possible
 Semaphore solution

 “Second readers/writers”
problem( writer priority):
 No new readers allowed once a writer has asked for
access
 This solution will be discussed with monitors.
 Readers and writers problem

Reade
r
(User
Writer 3)
(User
File
1)
Reade
r
(user
2)
 Readers and writers problem
Semaphore wrt=1, mutex=1;
Int readcount =0;

 Writer
do
{
wait(wrt);
writing is
performed
signal(wrt);
}
while(1);
 Structure of Reader process:
do {

Wait(mutex );
readcount++;
If(read count==1)
Wait( wrt );
Signal(mutex );

Reading is performed

Wait( mutex );
Readcount --;
If( readcount==0)
Signal( wrt );
Signal(mutex )

}
while(1);
R-W Problem

W-R Problem

W-W
Problem

R-R No Problem
int rc = 0;
Semaphore Mutex = 1;
Semaphore db = 1;
Void Reader ( void)
{
While ( true)
{
down ( mutex);
rc = rc+1;
if (rc ==1) than down (db);
up( mutex)

DB
Down(mutex)
rc = rc -1;
If ( rc == 0) than up (db);
up (mutex)
Process _ data
}
}
Void Writer (void)
{
While ( true )
{
down (db);
DB
up(db);
}
}
Dining philosophers Problem
do
{
wait (chopstick[i] );
wait (chopstick
[ (i+1)%5 ]);
//eat

Signal (chopstick [i]);


Signal (chopstick[(i+1)%5]);

//think

} while (true);
Dining philosophers Problem Solution:

 Allow at most 4 philosophers to be sitting


simultaneously at the table.
 Allow a philosophers to pick up the forks only if
both are available ( picking must be done in a
critical section)
 Use an asymmetric solution – an odd numbered
philosopher picks up first the left chopstick and
then the right chopstick. Even numbered
philosophers pick up first the right chopstick and
then left chopstick
Sleeping barber problem with i
semaphore solution
Counting semaphores
Sleeping Barber Problem:
 A Barber has a chair to cut hair of
customer, and when barber is done
with customer then he will cut the
next customer from the waiting
room.
 If there are no customers to be
served, the barber goes to sleep.
 The number of seats in the waiting
room is limited(suppose ‘n’).
 If a customer enters the barber shop
and all chairs are occupied, then
customer leaves the shop.
 If the barber is busy but the chairs
are available, then customer sits in
one of the free chairs.
 If the barber is sleeping, the
customer wakes up the barber.
Semaphore solution:

3 semaphores
Customers = 0, barber =0, mutix
= 1;
Int customer_ count =0

do{
wait(customers);
wait(mutex);
customer_count - - ;
signal(barber);
Signal(mutex);
cut_hair();
}while(1);
do{
wait(mutex);
if(customer_count < n ) {
custometer_count = customer_count++
signal(customers);
signal(mutex);
wait(barber);
get_haircut()
}
else{
// do nothing just leave
signal (mutex);
}
}while(1);
Counting Semaphores:
In operating systems, a counting semaphore is a synchronization
primitive
used to control access to a certain number of identical resources.
It is a type of semaphore that can have a non-negative integer value,
multiple threads or processes to access a fixed pool of resources.

Key characteristics of a counting


semaphore:

1)Integer Value:
Unlike binary semaphores that have values 0 and 1 (indicating
availability
or unavailability), counting semaphores can have values equal to or
greater than zero.
2)Resource pool:
Counting semaphores are often used when there are a fixed number
of resources, and the semaphore's value represents the number of
Operations

1)Initialization:
The semaphore is initialized with the total number of available
resources.

2) P (Proberen or "Test" in Dutch) Operation:


When a process wants to use a resource, it performs a "P" operation
on the semaphore.If the semaphore's value is greater than 0, it
decrements the value (indicating resource allocation).If the value is 0, the
process will wait (block) until another process releases a resource
(increments the semaphore).

3) V (Verhogen or "Increment" in Dutch) Operation:


When a process finishes using a resource, it performs a "V" operation
on the semaphore.This operation increments the semaphore's value,
indicating that a resource has been released and is available.If there were
processes waiting due to a semaphore value of 0, this increment allows
one of those processes to proceed.
Example Usage:
Let's say we have a pool of 5 printers, and we want to control access to
these printers using a counting semaphore:Initialize semaphore
printers_sem with a value of 5.When a process wants to print:It performs
a "P" operation on printers_sem.If printers_sem is greater than 0, it
decrements printers_sem and the process can use a printer.If
printers_sem is 0, the process waits until a printer is available (another
process does a "V" operation).When a process finishes printing:It
performs a "V" operation on printers_sem, incrementing the count to
indicate that a printer is now available.

Benefits:
 Resource Allocation: resource allocation control
 Synchronization: race condition prevention
 Waiting Mechanism: wait for resources
 Monitor:
 condition variable:
 Monitor:
A monitor is a concurrency construct containing data
and procedures required to perform allocation of a
particular serially reusable shared resource or group of
resource. More than one process may want to enter in
the monitor but the mutual exclusion is implemented at
monitor boundary. Only process at a time can enter a
monitor. It also solved the problem of critical section.
 Syntex of Monitor:

Monitor Demo // Name of the Monitor

Variables;

Conditional variable;

Procedure p1{ ……..}

Procedure p2{………}
}
 Advantages of monitors:

 A process calling a monitor procedure or method can ignore


actual implementation.

 Once a monitor is correctly programmed , it remains correct,


despite the number of process executing.

 The implementation of a monitor can be changed without


affecting the application or the users view of the monitor
resources.

 Monitor provide mutual exclusion.


Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy