Final PPT of Os
Final PPT of Os
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:
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
nextc Consumer
Producer nextp
Bounded- Buffer Problem (Producer consumer
Problem)
Semaphore full=0, empty=n , Mutex=1;
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
//think
} while (true);
Dining philosophers Problem 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.
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.
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:
Variables;
Conditional variable;
Procedure p2{………}
}
Advantages of monitors: