Chapter 3 OS
Chapter 3 OS
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.
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
• 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.
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);
• 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