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

OS LAB-IPC using PIPE

The document describes interprocess communication (IPC) using pipes in Linux C programming, detailing the types of pipes (unnamed and named) and their functionality. It explains the pipe system call, operations, and built-in methods for reading and writing data between processes. Additionally, it provides example source code for sending and receiving static messages and finding prime numbers using pipes.

Uploaded by

brokenbottle571
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)
10 views

OS LAB-IPC using PIPE

The document describes interprocess communication (IPC) using pipes in Linux C programming, detailing the types of pipes (unnamed and named) and their functionality. It explains the pipe system call, operations, and built-in methods for reading and writing data between processes. Additionally, it provides example source code for sending and receiving static messages and finding prime numbers using pipes.

Uploaded by

brokenbottle571
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/ 9

EX.

NO: 5 INTERPROCESS COMMUNICATION USING PIPE


AIM
To work with interprocess communication using pipe in linux c
programming.
IPC
 IPC stands for Inter Process Communication
 It is one of the mechanism provided by the OS which allows processes
to communicate with each other and synchronize their action
 Examples: Message Queue, Signals, Shared Memory, Pipes, etc,…

Process (P1) Process (P2) … Process


(Pn)

IPC

EXAMPLES TECHNIQUES OF IPC


 Shared Files

 Shared Memory

 Pipes (Named and Unnamed Pipes)

 Message Queues

 Sockets

 Signals

PIPES
 Pipe provides the communication between processes on same computer
or different computer or across the network

 It is classified as two types. They are

1. Unnamed Pipe (PIPE)

2. Named Pipe (FIFO)

Page
Pipe Symbol
 The output of one command (read) can be given as the input of next
command (write) that is called as pipe command. In linux terminal, it is
indicated the symbol |

 Two processes can be joined or communicated with help of the pipe


symbol on the shell terminal

pd[0] pd[0]
Process 1 (P1) Process 2
(P2)
pd[1] pd[1]

 Both processes P1, P2 are executed simultaneously and P1 will pass


data / message to Process P2 as it executes.

Example of Pipe Symbol in Linux Terminal

 Here the output of ls command is given as an input to the wc -l command


using pipe symbol (|).

 Hence, both ls command (Process P1) and wc –l command (Process P2)


can be communicated through pipe symbol (|).

 Using pipe symbol, ‘n’ of processes or commands can be communicated.

Page
I. IPC USING PIPE
PIPE SYSTEM CALL
 In linux c, the pipe system call is created by using the built-in function
called pipe()

Example

int pipe(int pd[2]);

 Here, pipe system call accepts only one argument, which is an integer
array of two pipe descriptors.
 pd[0] is used for reading data / message from the pipe and pd[1] is used
for writing data / message to the pipe.
Pipe (Unnamed Pipe or Anonymous Pipe)
 In linux, if the pipe has no name then it is called as unnamed pipe or
anonymous pipe (gives communication between parent and child
processes)
 It is a communication medium between two or more related or interrelated
processes
 It is mainly used for interprocess communication. It has two ends. They
are:
1. First end is fd[0] which is used for reading mode
2. Second end is fd[1] which is used for writing mode
 It is used for transferring data between two processes / commands /
programs
 It acts like queue data structure. We can write 512 bytes at the same time
but we can read ONLY ONE BYTE at the same time.
 It is an important to note that, pipe is an uni-directional (half duplex or
one-way communication) that is either from left to right or right to left
 It is an important to note that, whatever is written on one end (Ex. write
end) might visible on other end (Ex. read end)

Return Value of Pipe


 It returns 0 if successful otherwise it returns -1
 Return type: int

Page
Required Header File

#include<unistd.h>

OPERATIONS OF PIPE
 One Way Communication between processes

 One pipe system call is required

 Two Way Communication between processes

 Two pipe system calls are required

 By default, it is a half-duplex method. So the first process will


communicate with second process or second process will
communicate with first process. However, in order to achieve a
full-duplex, another pipe is needed.

BUILT-IN METHODS OF PIPE


1. write(pipe-descriptor[1], void * message, size_t count)
 This method is used to write the string message using the pipe end [1]

 It takes three arguments such as descriptor, message and size

 1st argument is pipe descriptor

 2nd argument is message which is string type

 3rd argument is count which is unsigned int type.

 This method will return the number of bytes written on success case and
will return -1 if the case is failure.

 Return type: ssize_t

2. read(pipe-descriptor[0], void * buffer, size_t count)


 This method is used to read the string message using the pipe end [0]

 It takes three arguments such as descriptor, message and size

 1st argument is pipe descriptor

 2nd argument is message which is string type

Page
 3rd argument is count which is unsigned int type.

 This method will return the number of bytes read on success case and
will return -1 if the case is failure.

 Return type: ssize_t

3. close(pipe-descriptor)
 This method is used to close the pipe if pipe is already opened

 It takes only one argument which is the integer value of pipe descriptor

 It will return 0 on success case and -1 on failure case.

 Return type: int

Page
I. SENDING AND RECEIVING STATIC MESSAGES BETWEEN TWO
PROCESSES USING PIPE
(/home/runner/Latest-Shells-21/pipeex1.c)
SOURCE CODE
#include<string.h>
#include<fcntl.h>
#include <unistd.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int r;
// pipe descriptors
int p[2];
printf("---------------------------------------------------------\n");
printf("\tSending and Receiving Static Messages-Pipe\n");
printf("---------------------------------------------------------\n");
// Text Messages (fixed messages)
char *sms1="Good Morning\n";
char *sms2="Hello World\n";
unsigned int s=strlen(sms1);
char buf[1024];
// create unnamed pipe using pipe system call
r=pipe(p);
if(r<0)
{
printf("Failed to created unnamed pipe...\n");
exit(1);
}
// send messages to another process
write(p[1],sms1,strlen(sms1));
write(p[1],sms2,strlen(sms1));
printf("Two Messages are sent successfully...(Process 1)\n");

Page
printf("---------------------------------------------------------\n");
printf("Message from Unnamed Pipe: (Process 2)\n");
// read messages from the pipe (other end) using single buffer
read(p[0],buf,sizeof(buf));
printf("%s",buf);
return 99;
}

OUTPUT

II. FINDING PRIME NUMBER USING PIPE


(Static Input)
(/home/runner/Latest-Shells-21/pipeex3.c)
SOURCE CODE
#include<fcntl.h>
#include <unistd.h>
#include<stdio.h>
#include<stdlib.h>
// function returns string as a result
char * findprime(int n)
{
int c=0;
for(int i=0;i<n;i++)
{

Page
if(n%(i+1)==0)
c++;
}
if(c==2)
return "The Given Number is a prime...\n";
else
return "The Given Number is NOT a prime...\n";
}
// main function
int main()
{
int r,ele;
// pipe descriptors
int p[2];
printf("---------------------------------------------------------\n");
printf("\tFinding Prime Number-Pipe\n");
printf("---------------------------------------------------------\n");
char buf[1024];
// create unnamed pipe using pipe system call
r=pipe(p);
if(r<0)
{
printf("Failed to created unnamed pipe...\n");
exit(1);
}
char *msg="18";
// send single message (string type) to another process (one end)
write(p[1],msg,sizeof(msg));
printf("One Message is sent successfully...(Process 1)\n");
printf("---------------------------------------------------------\n");
printf("Message from Unnamed Pipe: (Process 2)\n");
// read message from pipe (other end)
read(p[0],buf,sizeof(msg));

Page
printf("Received Data: %s\n",buf);
// convert received string data to integer
int num=atoi(buf);
// pass integer number to function for the prime number detection
char *rs=findprime(num);
// print the result
printf("%s\n",rs);
return 99;
}

IF INPUT IS 18

IF INPUT IS 31

Page

You might also like