Inter Process Communication
Inter Process Communication
Definition
"Inter-process communication is used for exchanging useful information
between numerous threads in one or more processes (or programs)."
These are the following methods that used to provide the synchronization:
1. Mutual Exclusion
2. Semaphore
3. Barrier
4. Spinlock
Mutual Exclusion:-
It is generally required that only one process thread can enter the critical
section at a time. This also helps in synchronization and creates a stable
state to avoid the race condition.
Semaphore:-
1. Binary Semaphore
2. Counting 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.
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
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
Barrier:-
A barrier typically not allows an individual process to proceed unless all the
processes does not reach it. It is used by many parallel languages, and
collective routines impose barriers.
Spinlock:-
Spinlock is a type of lock as its name implies. The processes are trying to
acquire the spinlock waits or stays in a loop while checking that the lock is
available or not. It is known as busy waiting because even though the
process active, the process does not perform any functional operation (or
task).
1. Pipes
2. Shared Memory
3. Message Queue
4. Direct Communication
5. Indirect communication
6. Message Passing
7. FIFO
Pipe:-
Shared Memory:-
Message Queue:-
In general, several different messages are allowed to read and write the data
to the message queue. In the message queue, the messages are stored or
stay in the queue unless their recipients retrieve them. In short, we can also
say that the message queue is very helpful in inter-process communication
and used by all operating systems.
o send (message)
o received (message)
Direct Communication:-
Indirect Communication
FIFO:-
o Socket:-
o File:-
A file is a type of data record or a document stored on the disk and can be
acquired on demand by the file server. Another most important thing is that
several processes can access that file as required or needed.
o Signal:-
As its name implies, they are a type of signal used in inter process
communication in a minimal way. Typically, they are the massages of
systems that are sent by one process to another. Therefore, they are not
used for sending data but for remote commands between multiple
processes.
Usually, they are not used to send the data but to remote commands in
between several processes.
#define mod %
struct item{
// or consumed data
---------
int full_index = 0;
C
item nextProduced;
while(1){
// for production.
// if so keep waiting.
shared_buff[free_index] = nextProduced;
item nextConsumed;
while(1){
while((free_index == full_index);
nextConsumed = shared_buff[full_index];
In the above code, the Producer will start producing again when the
(free_index+1) mod buff max will be free because if it is not free, this implies
that there are still items that can be consumed by the Consumer so there is
no need to produce more. Similarly, if free index and full index point to the
same index, this implies that there are no items to consume.
C++
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#define buff_max 25
#define mod %
struct item {
// or consumed data
// ---------
};
// item shared_buff[buff_max];
// Two variables which will keep track of
std::atomic<int> free_index(0);
std::atomic<int> full_index(0);
std::mutex mtx;
void producer() {
item new_item;
while (true) {
// ...
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
mtx.lock();
// shared_buff[free_index] = new_item;
mtx.unlock();
void consumer() {
item consumed_item;
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
mtx.lock();
// consumed_item = shared_buff[full_index];
mtx.unlock();
// Consume the item
// ...
std::this_thread::sleep_for(std::chrono::milliseconds(100));
int main() {
std::vector<std::thread> threads;
threads.emplace_back(producer);
threads.emplace_back(consumer);
thread.join();
return 0;
}
Note that the atomic class is used to make sure that the shared variables
free_index and full_index are updated atomically. The mutex is used to
protect the critical section where the shared buffer is accessed. The
sleep_for function is used to simulate the production and consumption of
items.
ii) Messaging Passing Method
Now, We will start our discussion of the communication between processes
via message passing. In this method, processes communicate with each
other without using any kind of shared memory. If two processes p1 and p2
want to communicate with each other, they proceed as follows:
The message size can be of fixed size or of variable size. If it is of fixed size,
it is easy for an OS designer but complicated for a programmer and if it is of
variable size then it is easy for a programmer but complicated for the OS
designer. A standard message can have two parts: header and body.
The header part is used for storing message type, destination id, source id,
message length, and control information. The control information contains
information like what to do if runs out of buffer space, sequence number,
priority. Generally, message is sent using FIFO style.
C
void Producer(void){
int item;
Message m;
while(1){
receive(Consumer, &m);
item = produce();
build_message(&m , item ) ;
send(Consumer, &m);
Consumer Code
C
void Consumer(void){
int item;
Message m;
while(1){
receive(Producer, &m);
item = extracted_item();
send(Producer, &m);
consume_item(item);
Pipe
Socket
Remote Procedural calls (RPCs)
The above three methods will be discussed in later articles as all of them are
quite conceptual and deserve their own separate articles.
References:
Advantages of IPC:
Disadvantages of IPC: