0% found this document useful (0 votes)
534 views

Inter Process Communication

This document discusses inter-process communication (IPC) methods in operating systems. It describes several common IPC approaches including pipes, message passing, message queues, shared memory, and FIFOs. It provides examples of using shared memory for IPC by having one process write a message to shared memory and another process read the message from shared memory. The document explains that IPC allows processes to coordinate activities and communicate concurrently in an operating system.

Uploaded by

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

Inter Process Communication

This document discusses inter-process communication (IPC) methods in operating systems. It describes several common IPC approaches including pipes, message passing, message queues, shared memory, and FIFOs. It provides examples of using shared memory for IPC by having one process write a message to shared memory and another process read the message from shared memory. The document explains that IPC allows processes to coordinate activities and communicate concurrently in an operating system.

Uploaded by

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

Session 12 & 13

Inter Process Communication (IPC) in OS


Inter process communication (IPC) is used for exchanging data between multiple threads
in one or more processes or programs. The Processes may be running on single or multiple
computers connected by a network. The full form of IPC is Inter-process communication.

It is a set of programming interface which allow a programmer to coordinate activities


among various program processes which can run concurrently in an operating system. This
allows a specific program to handle many user requests at the same time.
Since every single user request may result in multiple processes running in the operating
system, the process may require to communicate with each other. Each IPC protocol
approach has its own advantage and limitation, so it is not unusual for a single program to
use all of the IPC methods.

Approaches for Inter-Process Communication


Here, are few important methods for interprocess communication:

Pipes
Pipe is widely used for communication between two related processes. This is a half-duplex
method, so the first process communicates with the second process. However, in order to
achieve a full-duplex, another pipe is needed.

Message Passing:
It is a mechanism for a process to communicate and synchronize. Using message passing,
the process communicates with each other without resorting to shared variables.

IPC mechanism provides two operations:

 Send (message)- message size fixed or variable


 Received (message)

Message Queues:
A message queue is a linked list of messages stored within the kernel. It is identified by a
message queue identifier. This method offers communication between single or multiple
processes with full-duplex capacity.

Direct Communication:
In this type of inter-process communication process, should name each other explicitly. In
this method, a link is established between one pair of communicating processes, and
between each pair, only one link exists.
Indirect Communication:
Indirect communication establishes like only when processes share a common mailbox each
pair of processes sharing several communication links. A link can communicate with many
processes. The link may be bi-directional or unidirectional.

Shared Memory:
Shared memory is a memory shared between two or more processes that are established
using shared memory between all the processes. This type of memory requires to protected
from each other by synchronizing access across all the processes.

FIFO:
Communication between two unrelated processes. It is a full-duplex method, which means
that the first process can communicate with the second process, and the opposite can also
happen.

Why IPC?
Here, are the reasons for using the interprocess communication protocol for information
sharing:

 It helps to speedup modularity


 Computational
 Privilege separation
 Convenience
 Helps operating system to communicate with each other and synchronize their
actions.

Terms Used in IPC


The following are a few important terms used in IPC:

Semaphores: A semaphore is a signaling mechanism technique. This OS method either


allows or disallows access to the resource, which depends on how it is set up.

Signals: It is a method to communicate between multiple processes by way of signaling. The


source process will send a signal which is recognized by number, and the destination process
will handle it.

Implement IPC using Shared Memory


Inter Process Communication  through shared memory is a concept where two or more
process can access the common memory. And communication is done via this shared
memory where changes made by one process can be viewed by another process.
The problem with pipes, fifo and message queue – is that for two process to exchange
information. The information has to go through the kernel.
A process creates a shared memory segment using shmget(). The original owner of a
shared memory segment can assign ownership to another user with shmctl(). It can also
revoke this assignment. Other processes with proper permission can perform various
control functions on the shared memory segment using shmctl().

Once created, a shared segment can be attached to a process address space


using shmat(). It can be detached using shmdt(). The attaching process must have the
appropriate permissions for shmat(). Once attached, the process can read or write to the
segment, as the permission requested in the attach operation allows. A shared segment
can be attached multiple times by the same process.

A shared memory segment is described by a control structure with a unique ID that points
to an area of physical memory. The identifier of the segment is called the shmid. The
structure definition for the shared memory segment control structures and prototypes can
be found in <sys/shm.h>.

Examples
We will write two programs for IPC using shared memory as an example. Program 1 will
create the shared segment, attach it, and then write some content in it. Then Program 2
will attach itself to the shared segment and read the value written by Program 1.

Program 1: This program creates a shared memory segment, attaches itself to it, and
then writes some content into the shared memory segment.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666|IPC_CREAT);
//creates shared memory segment with key 2345, having size 1024 bytes.
IPC_CREAT is used to create the shared segment if it does not exist. 0666 are
the permissions on the shared segment
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0);
//process attached to shared memory segment
printf("Process attached at %p\n",shared_memory);
//this prints the address where the segment is attached with this process
printf("Enter some data to write to shared memory\n");
read(0,buff,100); //get some input from user
strcpy(shared_memory,buff); //data written to shared memory
printf("You wrote : %s\n",(char *)shared_memory);
}

Output

Key of shared memory is 0


Process attached at 0x7ffe040fb000
Enter some data to write to shared memory
Hello World
You wrote: Hello World

How does it work?

In the above program, the shmget() function creates a segment with key 2345,
size 1024 bytes, and reads and writes permissions for all users. It returns the
identifier of the segment, which gets stored in shmid. This identifier is used in
shmat() to attach the shared segment to the process's address space.

NULL in shmat() means that the OS will itself attach the shared segment at a
suitable address of this process. Then some data is read from the user using the
read() system call, and it is finally written to the shared segment using the
strcpy() function.

Program 2: This program attaches itself to the shared memory segment


created in Program 1, and it reads the content of the shared memory.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/shm.h>
#include<string.h>
int main()
{
int i;
void *shared_memory;
char buff[100];
int shmid;
shmid=shmget((key_t)2345, 1024, 0666);
printf("Key of shared memory is %d\n",shmid);
shared_memory=shmat(shmid,NULL,0); //process attached to shared memory
segment
printf("Process attached at %p\n",shared_memory);
printf("Data read from shared memory is : %s\n",(char *)shared_memory);
}
Output

Key of shared memory is 0


Process attached at 0x7f76b4292000
Data read from shared memory is: Hello World

How does it work?

In this program, shmget() here generates the identifier of the same segment as
created in Program 1. Remember to give the same key value. The only change is,
do not write IPC_CREAT as the shared memory segment is already created.
Next, shmat() attaches the shared segment to the current process.

After that, the data is printed from the shared segment. In the output, you will
see the same data that you have written while executing Program 1.

Inter Process Communication - Pipes


Pipe is a communication medium between two or more related or interrelated processes.
It can be either within one process or a communication between the child and the parent
processes. Communication can also be multi-level such as communication between the
parent, the child and the grand-child, etc. Communication is achieved by one process
writing into the pipe and other reading from the pipe. To achieve the pipe system call,
create two files, one to write into the file and another to read from the file.
Pipe mechanism can be viewed with a real-time scenario such as filling water with the
pipe into some container, say a bucket, and someone retrieving it, say with a mug. The
filling process is nothing but writing into the pipe and the reading process is nothing but
retrieving from the pipe. This implies that one output (water) is input for the other
(bucket).
Example Programs
Following are some example programs.

Example program 1 − Program to write and read two messages using pipe.

Algorithm
Step 1 − Create a pipe.

Step 2 − Send a message to the pipe.

Step 3 − Retrieve the message from the pipe and write it to the standard output.

Step 4 − Send another message to the pipe.

Step 5 − Retrieve the message from the pipe and write it to the standard output.

Note − Retrieving messages can also be done after sending all messages.

Source Code: simplepipe.c

#include<stdio.h>
#include<unistd.h>

int main() {
int pipefds[2];
int returnstatus;
char writemessages[2][20]={"Hi", "Hello"};
char readmessage[20];
returnstatus = pipe(pipefds);

if (returnstatus == -1) {
printf("Unable to create pipe\n");
return 1;
}

printf("Writing to pipe - Message 1 is %s\n", writemessages[0]);


write(pipefds[1], writemessages[0], sizeof(writemessages[0]));
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Reading from pipe – Message 1 is %s\n", readmessage);
printf("Writing to pipe - Message 2 is %s\n", writemessages[0]);
write(pipefds[1], writemessages[1], sizeof(writemessages[0]));
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Reading from pipe – Message 2 is %s\n", readmessage);
return 0;
}
Execution Steps
Compilation
gcc -o simplepipe simplepipe.c
Execution/Output
Writing to pipe - Message 1 is Hi
Reading from pipe – Message 1 is Hi
Writing to pipe - Message 2 is Hi
Reading from pipe – Message 2 is Hell

Example program 2 − Program to write and read two messages through the pipe
using the parent and the child processes.

Algorithm
Step 1 − Create a pipe.

Step 2 − Create a child process.

Step 3 − Parent process writes to the pipe.

Step 4 − Child process retrieves the message from the pipe and writes it to the
standard output.

Step 5 − Repeat step 3 and step 4 once again.

Source Code: pipewithprocesses.c

#include<stdio.h>
#include<unistd.h>

int main() {
int pipefds[2];
int returnstatus;
int pid;
char writemessages[2][20]={"Hi", "Hello"};
char readmessage[20];
returnstatus = pipe(pipefds);
if (returnstatus == -1) {
printf("Unable to create pipe\n");
return 1;
}
pid = fork();

// Child process
if (pid == 0) {
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Child Process - Reading from pipe – Message 1 is %s\n",
readmessage);
read(pipefds[0], readmessage, sizeof(readmessage));
printf("Child Process - Reading from pipe – Message 2 is %s\n",
readmessage);
} else { //Parent process
printf("Parent Process - Writing to pipe - Message 1 is %s\n",
writemessages[0]);
write(pipefds[1], writemessages[0], sizeof(writemessages[0]));
printf("Parent Process - Writing to pipe - Message 2 is %s\n",
writemessages[1]);
write(pipefds[1], writemessages[1], sizeof(writemessages[1]));
}
return 0;

Execution Steps
Compilation
gcc pipewithprocesses.c –o pipewithprocesses
Execution
Parent Process - Writing to pipe - Message 1 is Hi
Parent Process - Writing to pipe - Message 2 is Hello
Child Process - Reading from pipe – Message 1 is Hi
Child Process - Reading from pipe – Message 2 is Hello

You might also like