IPC – Inter-Process Communication

What is the IPC?

– As the name suggests, Inter-process communication is the OS service used to exchange data or information between processes.
– IPC is used to exchange information and notify events between processes.

Understand Inter Process Communication

Why IPC?

– The process runs on different address spaces in the OS.
– If one process wants to communicate with other processes then IPC is used.
– let’s understand with a programming example.

//vim program_func.c
#include<stdio.h>

int glbl = 10;
void func1()
{
        printf("In func1 glbl=%d\n", ++glbl);
}
void func2()
{
        printf("In func2 glbl=%d\n", ++glbl);
}
int main()
{
        func1();
        func2();
}
o/p:
In func1 glbl=11
In func2 glbl=12

– In the above program program_func.c:
– The initial value of the global variable glbl is 10.
– Incrementing in func1. So, it becomes ’11’ .
– Also incrementing in func2. So, it becomes ’12’.

//vim program_ipc.c
#include<stdio.h>
#include<unistd.h>

int glbl = 10;
void p1() //parent process
{
        printf("In process p1 glbl = %d\n", ++glbl);

}

void p2() //child process
{
        printf("In process p2 glbl = %d\n", ++glbl);
}
int main()
{
        if (fork())
        {
                p1();
        }
        else
        {
                p2();
        }
}

o/p:
In process p1 glbl = 11
In process p2 glbl = 11

– In the above program program_ipc.c,
– There are two processes created using fork. p1() runs in the parent process and p2 runs in the child process.
– Here, also glbl is incremented once in p1() and once in p2().
– But, the output is ’11’ and ’11’.
– Why even though glbl is incremented two times, glbl is printing ’11’?
– Because both processes have separate address spaces.
– And also have a separate copy of glbl to write.
– Parent process has a separate glbl copy which increments once, Child process also has a separate glbl copy which increments once, thus output is ’11’ and ’11’.
– So, the conclusion is that for program.c, There is only one process. global variable glbl is common for both functions func1 and fun2.
– In the case of program_ipc.c, there are two processes, and both processes have separate copies of variable glbl.
– So, we can say that the process runs on different address spaces and does not share the data. And to share we need IPC.

Examples of the IPC or Types of IPC.

Pipe (unnamed pipe)
– FIFO (named pipe)
– Message queue
– Shared memory
– Network Sockets
– Semaphore

  • Some books consider semaphore as IPC and some are not.
  • Because semaphore is used to notify about semaphore variables to another process, Semaphore variable is used for synchronization purposes.
  • Semaphore is not used to transfer data, just to notify another process.
  • Semaphore is commonly considered as a synchronization mechanism.
  • However, you can consider semaphore as an IPC used for process synchronization.

– Some books also consider OS signals as IPC because it is also used to notify across processes.

What is copy-on-write?

– In the above program, When parent and child processes are created. Both share the same page memory initially.
– But, when any process tries to modify any common shared page data then it will create a separate copy of it, and there modification happens.
– For example, global variable glbl is shared by both the processes initially with marking/tag as a copy-on-write.
– As soon as any process tries to modify it, A Separate copy of glbl is created for that specific process and modification can happen.
– This technique is called copy of write.

Where does IPC run?

– IPC is created in kernel space.
– Our processes are running in user space.
– If any user process wants to use IPC then it has to use system calls to get the service from kernel space.

Usage of IPC.

– IPC is useful When processes want to cooperate to achieve some goal.
– IPC is useful when processes want to send any data, notify signal, or status to another process.
– Processes running on different address spaces and different machines can exchange information.

Note: By default data sharing between different processes is not allowed to avoid vulnerability in the system. This is referred to as data protection for the process in the kernel. That’s where IPC comes into consideration to transfer data between processes.

How does IPC work in Unix/Linux?

– Let’s say, There are process-1(producer) and process-2(consumer) are running in user space and communicating through IPC residing in kernel space.
– Most of the IPCs are a special file in kernel space that is used for communication.
– The producer writes data to an IPC special file and the Consumer can read it.
– IPC files can not store data forever. Once the producer writes and the consumer reads the data. Data is removed from IPC file.
– If the IPC file is full and the producer tries to write then it can block the producer as there is no space to write. (There are ways to call API with a non-block option as well).
– If the IPC file is empty and the consumer tries to read then it can block the consumer as there is no data to read.

  • A producer is not writing to file and the file is empty resulting in the consumer waiting.
  • The consumer is not reading and the file is full, resulting in the producer waiting to write.

– So, We can say that producer and consumer depend on each other which is also referred to as synchronous communication.

Pipe IPC

Leave a Comment