0% found this document useful (0 votes)
99 views2 pages

C Program To Implement Inter Process Communication (IPC) by Using Pipe

This document summarizes a C program that implements inter-process communication (IPC) using pipes. There is a main parent process (P1) and two child processes (C1 and C2). P1 reads a string and passes it to C1 via a pipe. C1 finds the string length and sends it back to P1, then sends an integer array of that length to C2. C2 receives the array from C1, calculates the sum of the elements, and sends the sum back to P1. The full C source code for this IPC pipe implementation is provided.

Uploaded by

Suresh Karthick
Copyright
© Attribution Non-Commercial (BY-NC)
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)
99 views2 pages

C Program To Implement Inter Process Communication (IPC) by Using Pipe

This document summarizes a C program that implements inter-process communication (IPC) using pipes. There is a main parent process (P1) and two child processes (C1 and C2). P1 reads a string and passes it to C1 via a pipe. C1 finds the string length and sends it back to P1, then sends an integer array of that length to C2. C2 receives the array from C1, calculates the sum of the elements, and sends the sum back to P1. The full C source code for this IPC pipe implementation is provided.

Uploaded by

Suresh Karthick
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 2

C Program to implement Inter Process Communication(IPC) by using Pipe

at 10:38 AM Posted by Techman

Inter Process communication is a technique by which a thread in one process shares some information with threads in other processes.A pipe is a buffer,specifically a FIFO(First in First Out) buffer that has 2 ends,a read end and a write end.A thread can write or read depending on the file reference it has. read() is used to read data and write() is used to write data. The following is an implementation of Pipe in C Language.There is a main process P1 and 2 child processes C1 and C2 , the main process reads a string and passes it to one of the child process ,the child process C1 receive string from P1,find its length and sends the value to P1.C1 also reads an integer array of length equal to the length of the string and sends it to C2.C2 receive integer array from C1,find the sum of the elements and sends the sum to the parent process P1.The sleep() function is used in the program to suspend the processes for a desired interval of time. The complete C language source code of this example program that implements IPC(Interprocess Communication) using PIPE is given below.
#include<stdio.h> #include<unistd.h> #include<string.h> void main() { // www.c-madeeasy.blogspot.com int pid[2],pid1[2],pid2[2],pid3[2],pid4[2]; int a[20],i,l,s=0; char str[20]; pipe(pid); pipe(pid1); pipe(pid2); pipe(pid3); pipe(pid4); if(fork()==0) { sleep(5); close(pid[1]); read(pid[0],str,sizeof(str)); for(i=0,l=0;str[i]!='\0';i++) l=l+1; close(pid3[0]); write(pid3[1],&l,sizeof(l));

sleep(6); printf("Enter %d array elementz:",l); for(i=0;i<l;i++) scanf("%d",&a[i]); close(pid1[0]); write(pid1[1],a,sizeof(a)); close(pid4[0]); write(pid4[1],&l,sizeof(l)); } else if(fork()==0) { sleep(2); close(pid1[1]); close(pid4[1]); read(pid4[0],&l,sizeof(l)); read(pid1[0],a,sizeof(a)); for(i=0;i<l;i++) s=s+a[i]; close(pid2[0]); write(pid2[1],&s,sizeof(s)); } else { printf("\nEnter string:"); scanf("%s",str); close(pid[0]); write(pid[1],str,sizeof(str)); sleep(7); close(pid3[1]); read(pid3[0],&l,sizeof(l)); printf("\nThe string length=%d",l); sleep(8); close(pid2[1]); read(pid2[0],&s,sizeof(s)); printf("\nSum=%d",s); } }

You might also like