OS LAB-IPC using PIPE
OS LAB-IPC using PIPE
IPC
Shared Memory
Message Queues
Sockets
Signals
PIPES
Pipe provides the communication between processes on same computer
or different computer or across the network
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 |
pd[0] pd[0]
Process 1 (P1) Process 2
(P2)
pd[1] pd[1]
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
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)
Page
Required Header File
#include<unistd.h>
OPERATIONS OF PIPE
One Way Communication between processes
This method will return the number of bytes written on success case and
will return -1 if the case is failure.
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.
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
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
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