0% found this document useful (0 votes)
90 views21 pages

Unit V: Task Communication

The document discusses various techniques for task synchronization in operating systems, including semaphores, mutexes, and spinlocks. It provides: - Definitions and examples of counting and binary semaphores for coordinating access to shared resources. Semaphores use wait and signal operations. - An explanation of mutexes as a type of binary semaphore that includes a priority inheritance mechanism to minimize priority inversion issues. - A description of spinlocks that enable a thread to wait in a busy loop for a lock to become available, avoiding context switches but wasting CPU cycles if held for long periods. - An overview of remote procedure calls (RPC) where a procedure call is made across

Uploaded by

sirisha akula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views21 pages

Unit V: Task Communication

The document discusses various techniques for task synchronization in operating systems, including semaphores, mutexes, and spinlocks. It provides: - Definitions and examples of counting and binary semaphores for coordinating access to shared resources. Semaphores use wait and signal operations. - An explanation of mutexes as a type of binary semaphore that includes a priority inheritance mechanism to minimize priority inversion issues. - A description of spinlocks that enable a thread to wait in a busy loop for a lock to become available, avoiding context switches but wasting CPU cycles if held for long periods. - An overview of remote procedure calls (RPC) where a procedure call is made across

Uploaded by

sirisha akula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

UNIT V:

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

Here are the following advantages of the mutex, such as:

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

Mutex also has some disadvantages, such as:

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.

Advantages and disadvantages of spinlock

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

1. Spinlock needs busy waiting.


2. When the lock is unavailable, it wastes a CPU cycle and repeatedly checks for it to be
accessible.

Remote Procedure Call (RPC) in Operating System

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.

RPC is especially well suited for client-server (e.g. query-response) interaction in


which the flow of control alternates between the caller and callee. Conceptually, the
client and server do not both execute at the same time. Instead, the thread of execution
jumps from the caller to the callee and then back again.
Working of RPC

The following steps take place during a RPC :


1. A client invokes a client stub procedure, passing parameters in the usual way. The client
stub resides within the client’s own address space.
2. The client stub marshalls(pack) the parameters into a message. Marshalling includes
converting the representation of the parameters into a standard format, and copying each
parameter into the message.
3. The client stub passes the message to the transport layer, which sends it to the remote
server machine.
4. On the server, the transport layer passes the message to a server stub,
which demarshalls(unpack) the parameters and calls the desired server routine using the
regular procedure call mechanism.
5. When the server procedure completes, it returns to the server stub (e.g., via a normal
procedure call return), which marshalls the return values into a message. The server
stub then hands the message to the transport layer.
6. The transport layer sends the result message back to the client transport layer, which
hands the message back to the client stub.
7. The client stub demarshalls the return parameters and execution returns to the caller.
RPC ISSUES :
Issues that must be addressed:
1. RPC Runtime:
RPC run-time system is a library of routines and a set of services that handle the network
communications that underlie the RPC mechanism. In the course of an RPC call, client-side
and server-side run-time systems’ code handle binding, establish communications over an
appropriate protocol, pass call data between the client and server, and handle
communications errors.
2. Stub:
The function of the stub is to provide transparency to the programmer-written
application code.
 On the client side, the stub handles the interface between the client’s local procedure call
and the run-time system, marshaling and unmarshalling data, invoking the RPC run-time
protocol, and if requested, carrying out some of the binding steps.
 On the server side, the stub provides a similar interface between the run-time system and
the local manager procedures that are executed by the server.
3. Binding: How does the client know who to call, and where the service resides?
The most flexible solution is to use dynamic binding and find the server at run time when the
RPC is first made. The first time the client stub is invoked, it contacts a name server to
determine the transport address at which the server resides.
Binding consists of two parts:
 Naming:
 Locating:
1. A Server having a service to offer exports an interface for it. Exporting an interface
registers it with the system so that clients can use it.
2. A Client must import an (exported) interface before communication can begin.
4. The call semantics associated with RPC :
It is mainly classified into following choices-
 Retry request message –
Whether to retry sending a request message when a server has failed or the receiver didn’t
receive the message.
 Duplicate filtering –
Remove the duplicate server requests.
 Retransmission of results –
To resend lost messages without re-executing the operations at the server side.
ADVANTAGES :
1. RPC provides ABSTRACTION i.e message-passing nature of network communication is
hidden from the user.
2. RPC often omits many of the protocol layers to improve performance. Even a small
performance improvement is important because a program may invoke RPCs often.
3. RPC enables the usage of the applications in the distributed environment, not only in the
local environment.
4. With RPC code re-writing / re-developing effort is minimized.
5. Process-oriented and thread oriented models supported by RPC.

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