0% found this document useful (0 votes)
32 views

Chapter 3 OS

operating system mu chp 2

Uploaded by

yashpatilyp2004
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)
32 views

Chapter 3 OS

operating system mu chp 2

Uploaded by

yashpatilyp2004
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/ 23

What is Semaphores

A semaphore is an integer variable that is useful for solving a variety of synchronization problems.
It imposes deliberate constraints that help programmers avoid errors. Moreover, it makes the
solution more organized, mak A semaphore is an integer variable that is useful for
solving a variety of synchronization problems.
It imposes deliberate constraints that help programmers avoid errors.
Moreover, it makes the solution more organized, making the programs portable
and efficient.
It can be accessed only through two standard atomic operations: wait() and
signal().
An entity is the one that tries to access a shared resource.
• A semaphore is a value in a designated place in operating system (or
kernel) storage that each process can check and then change.
• Depending on the value that is found, the process can use the
resource or will find that it is already in use and must wait for some
period before trying again
• Binary Semaphore – This is also known as a mutex lock. It can have
only two values – 0 and 1. Its value is initialized to 1.
• A Binary Semaphore is a semaphore whose integer value range over 0
and 1.
• It is nothing, but similar to a lock, with two values: 0 and 1. Here 0
means busy, while 1 means free.
• The idea behind using a binary semaphore is that, it allows only one
process at a time to enter the critical section(thus allowing it to
access the shared resource).
• Here, 0 represents that a process or a thread is in the critical
section(i.e. it is accessing the shared resource), while the other
process or thread should wait for it to complete. On the other hand, 1
means that no process is accessing the shared resource, and the
critical section is free.
• Counting Semaphore – Its value can range over an unrestricted
domain. It is used to control access to a resource that has multiple
instances.
• A counting semaphore is a semaphore that has multiple values of the
counter. The value can range over an unrestricted domain.
• It is a structure, which comprises a variable, known as a semaphore
variable that can take more than two values and a list of task or entity,
which is nothing but the process or the thread.
• The value of the semaphore variable is the number of process or
thread that is to be allowed inside the critical section.
Criteria Binary Semaphore Counting Semaphore

Definition A Binary Semaphore is a semaphore whose integer value A counting semaphore is a semaphore
range over 0 and 1 that has multiple values of the counter.
The value can range over an
unrestricted domain.

Structure typedef struct { typedef struct


Implementation {
int semaphore_variable; int semaphore_variable;
Queue list;
}binary_semaphore; //A queue to store the list of task
}counting_semaphore;

Representation 0 means that a process or a thread is accessing the critical The value can range from 0 to N, where
section, other process should wait for it to exit the critical N is the number of process or thread
section. 1 represents the critical section is free. that has to enter the critical section.

Mutual Exclusion Yes, it guarantees mutual exclusion, since just one process No, it doesn’t guarantees mutual
or thread can enter the critical section at a time. exclusion, since more than one process
or thread can enter the critical section
at a time.
Critical Section Problem in OS

• Critical Section is the part of a program which tries to access shared


resources. That resource may be any resource in a computer like a
memory location, Data structure, CPU or any IO device.

• The critical section cannot be executed by more than one process at


the same time; operating system faces the difficulties in allowing and
disallowing the processes from entering the critical section.
• The critical section problem is used to design a set of protocols which
can ensure that the Race condition among the processes will never
arise.

• In order to synchronize the cooperative processes, our main task is to


solve the critical section problem. We need to provide a solution in
such a way that the following conditions can be satisfied.
Requirements of Synchronization
mechanisms
Our solution must provide mutual exclusion.
By Mutual Exclusion, we mean that if one
process is executing inside critical section then
the other process must not enter in the critical
section.
• Progress
• Progress means that if one process doesn't need to execute into
critical section then it should not stop other processes to get into the
critical section.
Secondary
• Bounded Waiting
• We should be able to predict the waiting time for every process to get
into the critical section. The process must not be endlessly waiting for
getting into the critical section.

• Architectural Neutrality
• Our mechanism must be architectural natural. It means that if our
solution is working fine on one architecture then it should also run on
the other ones as well.
Producer/Consumer Problem
• A semaphore S is an integer variable that can be accessed only through
two standard operations : wait() and signal().
The wait() operation reduces the value of semaphore by 1 and the
signal() operation increases its value by 1.

• wait(S){
• while(S<=0); // busy waiting
• S--;
•}

• signal(S){
• S++;
•}
• Problem Statement – We have a buffer of fixed size. A producer can
produce an item and can place in the buffer. A consumer can pick
items and can consume them. We need to ensure that when a
producer is placing an item in the buffer, then at the same time
consumer should not consume any item. In this problem, buffer is the
critical section.

• To solve this problem, we need two counting semaphores – Full and


Empty. “Full” keeps track of number of items in the buffer at any given
time and “Empty” keeps track of number of unoccupied slots.
• Initialization of semaphores –
mutex = 1
Full = 0 // Initially, all slots are empty. Thus full slots are 0
Empty = n // All slots are empty initially
Solution for Producer –

do{

//produce an item

wait(empty);
wait(mutex);

//place in buffer

signal(mutex);
signal(full);

}while(true)
• When producer produces an item then the value of “empty” is
reduced by 1 because one slot will be filled now. The value of mutex is
also reduced to prevent consumer to access the buffer. Now, the
producer has placed the item and thus the value of “full” is increased
by 1. The value of mutex is also increased by 1 because the task of
producer has been completed and consumer can access the buffer.
• Solution for Consumer –

• do{

• wait(full);
• wait(mutex);

• // consume item from buffer

• signal(mutex);
• signal(empty);

• }while(true)
• As the consumer is removing an item from buffer, therefore the value
of “full” is reduced by 1 and the value is mutex is also reduced so that
the producer cannot access the buffer at this moment. Now, the
consumer has consumed the item, thus increasing the value of
“empty” by 1. The value of mutex is also increased so that producer
can access the buffer now
Bankers algorithm

• Resource Allocation Graph


• The resource allocation graph (RAG) is used to visualize the system’s
current state as a graph.
• The Graph includes all processes, the resources that are assigned to
them, as well as the resources that each Process requests.
• Sometimes, if there are fewer processes, we can quickly spot a
deadlock in the system by looking at the graph rather than the tables
we use in Banker’s algorithm.
• Deadlock avoidance can also be done with Banker’s Algorithm
Banker’s Algorithm

• Bankers’s Algorithm is a resource allocation and deadlock avoidance


algorithm which test all the request made by processes for resources,
it checks for the safe state, and after granting a request system
remains in the safe state it allows the request, and if there is no safe
state it doesn’t allow the request made by the process.
• Inputs to Banker’s Algorithm
• Max needs of resources by each process.
• Currently, allocated resources by each process.
• Max free available resources in the system.
• The request will only be granted under the below condition
• If the request made by the process is less than equal to the max
needed for that process.
• If the request made by the process is less than equal to the freely
available resource in the system.
• Timeouts: To avoid deadlocks caused by indefinite waiting, a timeout
mechanism can be used to limit the amount of time a process can
wait for a resource. If the help is unavailable within the timeout
period, the process can be forced to release its current resources and
try again later.

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