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

Sorting Program in C Using Fork and Pipe

This document contains summaries of multiple documents about implementing interprocess communication (IPC) using pipes in C programs: 1. The first document discusses a C program that implements sorting of an integer array using fork and pipes. The main process takes input, forks a child process, and the processes communicate to find the maximum element in the array. 2. The second document discusses a C program that implements summing of elements in an integer array using fork and pipes. The processes communicate to calculate the sum. 3. The third document provides a more complete C program to calculate the sum of integers input by the user using fork and pipes. 4. The fourth document discusses implementing the sum of N numbers using

Uploaded by

Roshni Biju
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)
52 views

Sorting Program in C Using Fork and Pipe

This document contains summaries of multiple documents about implementing interprocess communication (IPC) using pipes in C programs: 1. The first document discusses a C program that implements sorting of an integer array using fork and pipes. The main process takes input, forks a child process, and the processes communicate to find the maximum element in the array. 2. The second document discusses a C program that implements summing of elements in an integer array using fork and pipes. The processes communicate to calculate the sum. 3. The third document provides a more complete C program to calculate the sum of integers input by the user using fork and pipes. 4. The fourth document discusses implementing the sum of N numbers using

Uploaded by

Roshni Biju
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/ 12

SORTING PROGRAM IN C USING FORK AND PIPE

Posted by C Programmer
main()
{
void sort(int *,int);
int n,arr[10],i;
printf("Enter The Total Elements\n");
scanf("%d",&n);
printf("Enter Elements One By One\n");
for(i=0;i
scanf("%d",(arr+i));
sort(arr,n);
}

void sort(int *s,int n)


{
int a[2],i=0,b[2],c[2],temp=0;
int p,sec=0,j,fir=0;
int max=*(s+i);
if (n%2)
*(s+n)=0;
pipe(a);
pipe(b);
p=fork();
{
for(i=0;i
if(p>0)
{
if(i==0)
max=*(s+i);
write(a[1],&max,1);
read(b[0],&max,1);
if(max<*(s+i))
max=*(s+i);
}

else
{
sec=*(s+i+1);
read(a[0],&max,1);
if(max
max=sec;
write(b[1],&max,1);
}
}
printf("\nMAXIMUM IS :%d",max);
}

PIPES IN UNIX IPC


Posted by C Programmer
#include
main()
{
clock_t st,en;
int a[2],i,b[2],c[2],s[]={1,2,3,4,5,6,7,8,9,10},temp=0;
int p,res=0,res1=0,n;
printf("Enter The Total Elements\n");
scanf("%d",&n);
printf("Enter All Elements One By One\n");
for(i=0;i
scanf("%d",(s+i));
//st=clock();
//printf("\nSTART IS :%d",st);
if(n%2==1)
{
n++;
*(s+i)=0;
}
pipe(a);
pipe(b);

p=fork();
for(i=0;i
if(p>0)
{
write(a[1],(s+i),1);
read(b[0],&temp,1);
res=res+temp;
}
else
{
read(a[0],&res1,1);
res=*(s+i+1)+res1;
write(b[1],&res,1);
}
printf("\nRES :%d",res);
en=clock();
printf("\nTOTAL TIME IS : %d ",(en-st));
}

COMPLETE SUM PROGRAM IN UNIX IPC


Posted by C Programmer
main()
{
void sum(int);
int n;
printf("Enter The Total Elements\n");
scanf("%d",&n);
sum(n);
}

void sum(int n)
{
int a[2],i,b[2],c[2],s[1000],temp=0;

int p,res=0,res1=0;
for(i=0;i
*(s+i)=i+1;
if(n%2==1)
{
n++;
*(s+i)=0;
}
pipe(a);
pipe(b);
p=fork();
for(i=0;i
if(p>0)
{
write(a[1],(s+i),1);
read(b[0],&temp,1);
res=res+temp;
}
else
{
read(a[0],&res1,1);
res=*(s+i+1)+res1;
write(b[1],&res,1);
}
printf("\rRES :%d",res);
}

SUM OF N NUMBERS PROGRAM IN C USING FORK AND PIPE IPC


Posted by C Programmer
main()
{
int a[2],i,b[2],c[2],s[]={1,2,3,4,5,6,7,8,9,10},temp=0;
int p,res=0,res1=0,n;
printf("Enter The Total Elements\n");
scanf("%d",&n);

printf("Enter All Elements One By One\n");


for(i=0;i
scanf("%d",(s+i));
//printf("%s",line);
pipe(a);
pipe(b);
p=fork();
if(n%2==1)
{
n++;
*(s+i)=0;
}
for(i=0;i
{
if(p>0)
{
//printf("NO IS %d ",i);
write(a[1],(s+i),1);
read(b[0],&temp,1);
res=res+temp;
}
else
{
//printf("NO IN @ IS: %d ",i);
read(a[0],&res1,1);
res=*(s+i+1)+res1;
write(b[1],&res,1);
//printf("TEMP VAL :%d ",res);
}
//system("sleep 1");
}
printf("RES :%d",res);
exit(1);
}

AIM:
To implement the concept of interprocess communication using pipes
using c program.

ALGORITHM:
1. create the pipe and create the process.
2. get the input in the main process and pass the output to the child process using
pipe.
3. perform the operation given in the child process and print the output.
4. stop the program.

PROGRAM:
#include<stdio.h>
#include<unistd.h>
#include<string.h>
main()
{
int p1[2],p2[2],p3[2],p4[2];
int i,j=0,k=0,l=0;
char r[10],s[10],t[10],u[10];
printf("\t PROCESS 1.ENTER THE STRING");
scanf("%s",r);
pipe(p1);
pipe(p2);
write(p1[1],r,sizeof(r));
write(p2[1],r,sizeof(r));
int a=fork();
if(a==0)
{
printf("\n\t PROCESS 2:it splits the given string\n");

read(p1[0],r,sizeof(r));
int n=strlen(r);
for(i=0;i<n/2;i++)
{
s[i]=r[i];
}
for(i=n/2;i<=n;i++)
{
t[j++]=r[i];
}
pipe(p3);
pipe(p4);
write(p3[1],s,sizeof(s));
write(p4[1],t,sizeof(t));
int b=fork();
if(b==0)
{
printf("p4 %d\t",getpid());
printf("p2 %d\n",getppid());
read(p3[0],s,sizeof(s));
printf("\t PROCESS 4:sub string \t %s \t",s);
printf("no of char=%d \n",strlen(s));
}
else
{
int c=fork();
if(c==0)
{
printf("p5 %d\t",getpid());
printf("p2 %d\n",getppid());
read(p4[0],t,sizeof(t));
printf("\t PROCESS 5:sub string \t %s \t",t);
printf("no of char=%d \n",strlen(t));
}

else
{
wait();
printf("p2 %d\t",getpid());
printf("p1 %d\n",getppid());
} }}
else
{
wait();
int d=fork();
if(d==0)
{
printf("p3 %d\t",getpid());
printf("p1 %d\n",getppid());
read(p2[0],r,sizeof(r));
for(i=strlen(r)-1;i>=0;i--)
{
u[l++]=r[i];
}
for(i=0;i<strlen(r);i++)
{
if(u[i]==r[i])
k++;
else
continue;
}
if(k==strlen(r))
printf("\t PROCESS 3: the given string is palindrome\n");
else
printf("\t PROCESS 3: the given string is not palindrome\n");
}
else
{
printf("p1 %d\t",getpid());

printf("kernal %d\t\n",getppid());
}
}}

OUTPUT:
Process 1: enter the string ARUN
Process 2: it splits the string
P4 8137 p2 8136
Process 3: sub string a r no of char=2
P5 8138 p2 8136
Process 4; substring u n no of char=2
P2 8136 p1 8132
P3 8139 p1 8132
Process 3: the given string is not palindrome
Process 1: enter the string MADAM
Process 2: it splits the string
P4 8137 p2 8136
Process 4: sub string m a no of char=2
P5 8138 p2 8136
Process 5: sub string dam no of char=3
P2 8136 p1 8132
P3 8139 p1 8132
Process 3: the given string is palindrom

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