0% found this document useful (0 votes)
9 views4 pages

Interprocess Communication in C

The document outlines an experiment aimed at implementing interprocess communication using a C program. It details the requirements, approaches, and a sample code for using pipes to facilitate communication between processes. The experiment successfully demonstrates how processes can exchange messages through a virtual file system using pipes.

Uploaded by

hackeronekamil
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)
9 views4 pages

Interprocess Communication in C

The document outlines an experiment aimed at implementing interprocess communication using a C program. It details the requirements, approaches, and a sample code for using pipes to facilitate communication between processes. The experiment successfully demonstrates how processes can exchange messages through a virtual file system using pipes.

Uploaded by

hackeronekamil
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

EXPERIMENT 04

NUMBER
IMPLEMENTATION OF INTER PROCESS
DATE DAY MONTH YEA
COMMUNICATION
R

Aim

To write a C program to Implementation of Library Function Using System Call.

Components Requirement

APPLICATION SOFTWARE REQUIREMENT


[Link] SOFTWARE QUANTITY
1 Code Blocks 1 No
SYSTEM SOFTWARE REQUIREMENT
[Link] OPERATING SYSTEM QUANTITY
1 Ubuntu 1 No

Description

Interprocess communication is the mechanism provided by the operating system that


allows processes to communicate with each other. This communication could involve a process
letting another process know that some event has occurred or the transferring of data from one
process to another.

A diagram that illustrates interprocess communication is as follows:


Approaches to Interprocess Communication

The different approaches to implement interprocess communication are given as follows:


• Pipe
It is a data Channel that is unidirectional. Two pipes can be used to create a two-way
data channel between two processes. This uses standard input and output methods.
Pipes are used in all POSIX systems as well as Windows operating systems.

• Socket
The socket is the endpoint for sending or receiving data in a network. This is true for
data sent between processes on the same computer or data sent between different
computers on the same network. Most of the operating systems use sockets for
interprocess communication.

• File
A file is a data record that may be stored on a disk or acquired on demand by a file
server. Multiple processes can access a file as required. All operating systems use files
for data storage.

• Signal
Signals are useful in interprocess communication in a limited way. They are system
messages that are sent from one process to another. Normally, signals are not used to
transfer data but are used for remote Commands between processes.

• Shared Memory
Shared memory is the memory that can be simultaneously accessed by multiple
processes. This is done so that the processes can communicate with each other. All
POSIX systems, as well as Windows operating systems use shared memory.

• Message Queue
Multiple processes can read and write data to the message queue without being
connected to each other. Messages are stored in the queue until their recipient
retrieves them. Message queues are quite useful for interprocess communication and
are used by most operating systems.

Pipe:

Conceptually, a pipe is a connection between two processes, such that the standard
output from one process becomes the standard input of the other process. In U“NIX
Operating System, Pipes are useful for communication between related processes
(inter-process communication).
• Pipe is one-way communication only i.e we can use a pipe such that One process
write to the pipe, and the other process reads from the pipe. It opens a pipe,
which is an area of main memory that is treated as a "virtual file".
• The pipe can be used by the creating process, as well as all its child processes, for
reading and writing. One process can write to this "virtual file" or pipe and another
related process can read from it.
• If a process tries to read before something is written to the pipe, the process is
suspended until something is written.
• The pipe system call finds the first two available positions in the process’s open file
table and allocates them for the read and write ends of the pipe

Syntax:
int pipe(int Ids[2]);

Parameters:
fd[0] will be the fd(file descriptor) for the read end of pipe.
fd[1] will be the fd for the write end of pipe.

Algorithm

Step 1: Create pipe1 for the parent process to write and the child process to read.
Step 2: Create pipe2 for the child process to write and the parent process to read.
Step 3: Close the unwanted ends of the pipe from the parent and child side.
Step 4: Parent process to write a message and child process to read and display on the
screen.
Step 5: Child process to write a message and parent process to read and display on the
screen.

Program
#include<stdio.h>
#include<unistd.h>
int main()
{
int pipefds1[2],pipefds2[2];
int returnstatus1,returnstatus2;
int pid;
char pipe1writemessage[20]="Hi";
char pipe2writemessage[20]="Hello";
char readmessage[20];
returnstatus1=pipe(pipefds1);
if(returnstatus1==-1)
{
printf("unable to create connection\n");
return 1;
}
returnstatus2=pipe(pipefds2);
if(returnstatus2==-1)
{
printf("unable to create connection\n");
return 1;
}
pid=fork(); //creates the copy of process
//parentprocess
if(pid!=0)
{
close(pipefds1[0]); //close the unwanted pipe1 read side
close(pipefds2[1]); //close the unwanted pipe2 write side
printf("\nparent:writing to pipe1,message is %s\n",pipe1writemessage);
write(pipefds1[1],pipe1writemessage,sizeof(pipe1writemessage));
read(pipefds2[0],readmessage,sizeof(readmessage));
printf("\n parent:reading message from pipe2 %s\n",readmessage);
}
//childprocess
else
{
close(pipefds2[0]); //close the unwanted pipe1 read side
close(pipefds1[1]); //close the unwanted pipe2 write side
read(pipefds1[0],readmessage,sizeof(readmessage));
printf("\n child:reading message from pipe1 %s\n",readmessage);
printf("\nchild:writing to pipe2,message is %s\n",pipe2writemessage);
write(pipefds2[1],pipe2writemessage,sizeof(pipe2writemessage));
}
}

Output:

Result:
Thus we implemented a program for inter process communication successfully.

You might also like