Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Synchronization and Messaging

• The synchronization and messaging provides the communication between the task of one
system to another system and the messaging services are following. To synchronize the
internal activities the event flag is used and to send the text messages we can use in the
mailbox, pipes and message queues. In the common data areas, the semaphores are used.

• Semaphores
• Event flags
• Mailboxes
• Pipes
• Message queues
• Semaphore in OS is an integer value that indicates whether the
resource required by the process is available or not. The value of a
semaphore is modified by wait() or signal() operation where the
wait() operation decrements the value of semaphore and the signal()
operation increments the value of the semaphore.
• The wait() operation is performed when the process wants to access
the resources and the signal() operation is performed when the
process want to release the resources. The semaphore can be binary
semaphore or the counting semaphore.
• Semaphore is a process synchronization tool that prevents race
condition that may occur when multiple cooperative processes try to
access the same resources. Two or more process can synchronise by
means of the signal. This means the process, trying to access the
same shared resource can be forced to stop at a specific place until it
is signalled to access the resource.
• Actually, the semaphore is an integer variable whose value signals that if the process must
access the resource or wait or wake up to access the shared resources. Apart from the
initialization, the semaphore value can be modified using two operations wait() and signal().

• The wait() operation decrements the value of semaphore and if the semaphore value
becomes negative then the process executing wait() would be blocked. The signal()
operation increments the value of semaphore and if the semaphore value is less than or
equal to 0 then a process blocked by wait() is unblocked.

• The wait() and signal() operation must be performed indivisibly i.e. if a process is modifying
the semaphore value no other process must simultaneously modify the semaphore value.
Along with that the execution of wait() and signal() must not be interrupted in between.
• You can also observe the P(), down() as a synonym to wait() operation. Now, let us see the working of
wait() operation.

• Wait(Semaphore S)
•{
• S.vlaue= S.value -1;
• If (S.value≤0)
•{
• block the process Sleep();
•}
• else
• return;
•}
• As a synonym to signal(), v(), up(), post() and release(). Now let us see the working of
signal() operation.

• Signal(Semaphore S)
•{
• S.value= S.value + 1;
• If(S.value≤0)
•{
• Unblock the process wake up();
•}
•}
• Any processes that want to access the shared resources have to first
execute the entry section where the wait() operation decrements the
value of the semaphore. If the value of the semaphore is 0 that
means all the resources are in use and now onwards the processes
trying to execute the entry section would be blocked.

• Implementation of Semaphore
• Semaphore in Operating System
• 11th January 2021 by Neha T Leave a Comment

• Semaphore in OS is an integer value that indicates whether the resource required by the process is available or not. The value of a semaphore is modified by wait() or signal() operation where the wait() operation decrements the value of semaphore and the signal() operation increments the value of the semaphore.

• The wait() operation is performed when the process wants to access the resources and the signal() operation is performed when the process want to release the resources. The semaphore can be binary semaphore or the counting semaphore. In this section, we will discuss the concept of semaphore in brief along with the types, advantages and disadvantages.

• Content: Semaphore in OS
• What is Semaphore?
• Implementation of Semaphores
• Types of Semaphores
• Advantages and Disadvantages
• Key Takeaways

• What is Semaphore in OS?


• Semaphore is a process synchronization tool that prevents race condition that may occur when multiple cooperative processes try to access the same resources. Two or more process can synchronise by means of the signal. This means the process, trying to access the same shared resource can be forced to stop at a specific place until it is signalled to access the resource.

• Now you will be thinking how the semaphore signals the process in order to achieve synchronization?

• Actually, the semaphore is an integer variable whose value signals that if the process must access the resource or wait or wake up to access the shared resources. Apart from the initialization, the semaphore value can be modified using two operations wait() and signal().

• The wait() operation decrements the value of semaphore and if the semaphore value becomes negative then the process executing wait() would be blocked. The signal() operation increments the value of semaphore and if the semaphore value is less than or equal to 0 then a process blocked by wait() is unblocked.

• The wait() and signal() operation must be performed indivisibly i.e. if a process is modifying the semaphore value no other process must simultaneously modify the semaphore value. Along with that the execution of wait() and signal() must not be interrupted in between.

• You can also observe the P(), down() as a synonym to wait() operation. Now, let us see the working of wait() operation.

• Wait(Semaphore S)
•{
• S.vlaue= S.value -1;
• If (S.value≤0)
•{
• block the process Sleep();
•}
• else
• return;
•}
• As a synonym to signal(), v(), up(), post() and release(). Now let us see the working of signal() operation.

• Signal(Semaphore S)
•{
• S.value= S.value + 1;
• If(S.value≤0)
•{
• Unblock the process wake up();
•}
•}

• Implementation of Semaphore
• Any processes that want to access the shared resources have to first execute the entry section where the wait() operation decrements the value of the semaphore. If the value of the semaphore is 0 that means all the resources are in use and now onwards the processes trying to execute the entry section would be blocked.

• Implementation of Semaphore

• Now when a process wants to release the resource then it has to execute the exit code before releasing the resource. Executing the exit code the process increments the value of semaphore with the signal() operation, unblocks a blocked process and thereby release the resource.

• The unblocked process can now try to execute the entry section code to access the resource. There is always a waiting queue or blocked queue linked with each semaphore. The processes are unblocked or removed from the waiting queue in the First-in-First-out manner. That means the processes that have been waiting for a long is released first.
• There are several types of semaphores (the basic idea behind each type is the same):

• Binary

• Counting

• Mutex

• Semaphores are typically used in one of two ways:

• To control access to a shared device between tasks. A printer is a good example. You don't want 2 tasks sending to
the printer at once, so you create a binary semaphore to control printer access. When a device wishes to print, it
attempts to "take" the semaphore. If the semaphore is available, the task gets to print. If the semaphore is not
available, the task will have to wait for the printer.

• Task synchronization. By tasks taking and giving the same semaphore, you can force them to perform operations in
a desired order.

• Counting semaphores are used when you might have multiple devices (like 3 printers or multiple memory buffers).
• Binary semaphores are used to gain exclusive access to a single
resource (like the serial port, a non-reentrant library routine, or a hard
disk drive). A counting semaphore that has a maximum value of 1 is
equivalent to a binary semaphore (because the semaphore's value
can only be 0 or 1).

• Mutex semaphores are optimized for use in controlling mutually


exclusive access to a resource. There are several implementations of
this type of semaphore.
• Types of Semaphores
• The operating system classifies
semaphores into two types as
discussed below:
• 1. Counting Semaphore
• The counting semaphores controls access to resources that have finite
instances. Therefore the value of counting semaphore is not restricted
to a certain domain.

• The counting semaphore is initialized with a value equivalent to the


number of resources available. When a process wants to access the
resource it executes the entry section code where the wait()
operation decrements the value of semaphore and allot the resource
to the process.
• When the counting semaphore value decreases to 0 it means no resources
are available now and the process that executes the entry section code
further would be blocked. Now when a process releases a resource it
executes its exit section code where the signal() operation would increment
the value of counting semaphore and release a blocked process which can
further try to execute the entry section code.

• 2. Binary Semaphores
• Binary semaphores are generally used to access the critical section. As we
know that only one process can enter a critical section at a time therefore
the value of binary semaphores ranges over 0 to 1.
• Consider that the binary semaphore value is initialized to 1. Now if a process P1
want to enter the critical section then it executes the entry section code where
the wait() operation decrements the semaphore value to 0 and enters the critical
section. Now when second process P2 tries to enter the critical section then it
executes the entry section code where the wait() operation verifies the binary
semaphore value is already 0 which means no process further can enter the
critical section and blocks the second process P2.

• When process P1 leaves the critical section it executes the signal() operation of
exit section which increments the semaphore value to 1 and releases a blocked
process P2 which can further try to execute entry section code to enter the
critical section.
• Advantages and Disadvantages
• Advantages of Semaphore

• Semaphore resolves the process synchronization issues.


• Waiting list associated with each semaphore avoids busy waiting and lets CPU perform other
productively.
• Disadvantages of Semaphore

• Implementing semaphore can lead to priority inversion where the two processes get into
spinlock condition.
• If not implemented properly then semaphore can cause deadlock condition.
• Semaphore requires busy waiting and it wastes CPU cycles.

You might also like