Unit V: Task Communication
Unit V: Task Communication
Task Communication:
Shared Memory, Message Passing, Remote Procedure Call and Sockets, Task
Synchronization: Task Communication/Synchronization Issues, Task Synchronization
Techniques, Device Drivers, How to Choose an RTOS.
SYNCHRONIZATION TECHNIQUES
SEMAPHORE
Semaphores are integer variables that are used to solve the critical section problem by using
two atomic operations, wait and signal that are used for process synchronization.
The definitions of wait and signal are as follows −
Wait
The wait operation decrements the value of its argument S, if it is positive. If S is
negative or zero, then no operation is performed.
wait(S)
{
while (S<=0);
S--;
}
Signal
The signal operation increments the value of its argument S.
signal(S)
{
S++;
}
Types of Semaphores
There are two main types of semaphores i.e. counting semaphores and binary semaphores.
Details about these are given as follows −
Counting Semaphores
These are integer value semaphores and have an unrestricted value domain. These
semaphores are used to coordinate the resource access, where the semaphore count is
the number of available resources. If the resources are added, semaphore count
automatically incremented and if the resources are removed, the count is decremented.
Binary Semaphores
The binary semaphores are like counting semaphores but their value is restricted to 0
and 1. The wait operation only works when the semaphore is 1 and the signal operation
succeeds when semaphore is 0. It is sometimes easier to implement binary semaphores
than counting semaphores.
Advantages of Semaphores
Some of the advantages of semaphores are as follows −
Semaphores allow only one process into the critical section. They follow the mutual
exclusion principle strictly and are much more efficient than some other methods of
synchronization.
There is no resource wastage because of busy waiting in semaphores as processor
time is not wasted unnecessarily to check if a condition is fulfilled to allow a process
to access the critical section.
Semaphores are implemented in the machine independent code of the microkernel. So
they are machine independent.
Disadvantages of Semaphores
Some of the disadvantages of semaphores are as follows −
Semaphores are complicated so the wait and signal operations must be implemented
in the correct order to prevent deadlocks.
Semaphores are impractical for last scale use as their use leads to loss of modularity.
This happens because the wait and signal operations prevent the creation of a
structured layout for the system.
Semaphores may lead to a priority inversion where low priority processes may access
the critical section first and high priority processes later.
What is Mutex?
Mutex is a mutual exclusion object that synchronizes access to a resource. It is created with a
unique name at the start of a program. The mutex locking mechanism ensures only one thread
can acquire the mutex and enter the critical section. This thread only releases the mutex when
it exits in the critical section.
It is a special type of binary semaphore used for controlling access to the shared resource. It
includes a priority inheritance mechanism to avoid extended priority inversion problems. It
allows current higher priority tasks to be kept in the blocked state for the shortest time possible.
However, priority inheritance does not correct priority inversion but only minimizes its effect.
Example
Skip Ad
This is shown with the help of the following example,
1. wait (mutex);
2. .....
3. Critical Section
4. .....
5. signal (mutex);
Use of Mutex
A mutex provides mutual exclusion, either producer or consumer who can have the key (mutex)
and proceed with their work. As long as the producer fills the buffer, the user needs to wait,
and vice versa. In Mutex lock, all the time, only a single thread can work with the entire buffer.
When a program starts, it requests the system to create a mutex object for a given resource.
The system creates the mutex object with a unique name or ID. Whenever the program thread
wants to use the resource, it occupies lock on mutex object, utilizes the resource and after use,
it releases the lock on mutex object. Then the next process is allowed to acquire the lock on the
mutex object.
Meanwhile, a process has acquired the lock on the mutex object, and no other thread or process
can access that resource. If the mutex object is already locked, the process desiring to acquire
the lock on the mutex object has to wait and is queued up by the system till the mutex object is
unlocked.
Advantages of Mutex
o Mutex is just simple locks obtained before entering its critical section and then releasing
it.
o Since only one thread is in its critical section at any given time, there are no race
conditions, and data always remain consistent.
Disadvantages of Mutex
o If a thread obtains a lock and goes to sleep or is preempted, then the other thread may
not move forward. This may lead to starvation.
o It can't be locked or unlocked from a different context than the one that acquired it.
o Only one thread should be allowed in the critical section at a time.
o The normal implementation may lead to a busy waiting state, which wastes CPU time.
o
Spinlock
It is a locking mechanism. It enables a thread to wait for the lock to become ready, i.e., the
thread can wait in a loop or spin until the lock is ready. It is only held for a short time, and it is
useful in a multiprocessor system. The thread holds the spinlock until it is released after
acquiring the lock. In some implementations, the spinlock is automatically released if the thread
holding the lock is blocked or goes to sleep state.
A spinlock also avoids the overhead caused by OS process rescheduling or context switching.
Furthermore, the spinlock is an effective method to block the threads temporarily. As a result,
spinlocks are used in most of the operating system kernels. However, if a thread keeps a
spinlock for an extended period of time, it may prevent other threads from executing. In this
case, the other threads repeatedly try to acquire the lock, while the thread holding the lock
doesn't begin to release it. Generally, it may mainly occur in single-processor systems.
There are various advantages and disadvantages of a spinlock. Some of the advantages and
disadvantages of the spinlock are as follows:
Skip Ad
Advantages
1. It does not require a context switch because it is busy waiting, and the thread is not
sleeping.
2. If the critical section (CS) is smaller, it is helpful.
Disadvantages
Remote Procedure Call (RPC) is a powerful technique for constructing distributed, client-
server based applications. It is based on extending the conventional local procedure calling
so that the called procedure need not exist in the same address space as the calling
procedure. The two processes may be on the same system, or they may be on different systems
with a network connecting them.
When making a Remote Procedure Call:
1. The calling environment is suspended, procedure parameters are transferred across
the network to the environment where the procedure is to execute, and the procedure
is executed there.
2. When the procedure finishes and produces its results, its results are transferred back
to the calling environment, where execution resumes as if returning from a regular
procedure call.