Professional Documents
Culture Documents
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);
}
else
{
sec=*(s+i+1);
read(a[0],&max,1);
if(max
max=sec;
write(b[1],&max,1);
}
}
printf("\nMAXIMUM IS :%d",max);
}
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));
}
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);
}
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.
}
}