-
Notifications
You must be signed in to change notification settings - Fork 0
/
pelinpipe.c
64 lines (52 loc) · 1.15 KB
/
pelinpipe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
void main()
{
int pid,p1[2],p2[2],i,j,n,buffer[100],buffer2[100],a,b,f,n2;
if((a=pipe(p1))==-1)
{ printf("\n Erroe in pipe");exit(0);}
if((b=pipe(p2))==-1)
{ printf("\nError in pipe2");exit(0);}
pid=fork();
switch(pid)
{
case -1 : printf("\nError in fork\n");
exit(0);
case 0 :
close(p1[1]);
close(p2[0]);
printf("\n In child Process\n");
n=read(p1[0],buffer,sizeof(buffer));
n=n/sizeof(int);
n2=0;
for(i=0;i<n;i++)
{
f=0;
if(buffer[i]==1)
f=1;
for(j=2;j<buffer[i]/2;j++)
if(buffer[i]%j==0)
{f=1;
break;}
if(f==0)
buffer2[n2++]=buffer[i];
}
write(p2[1],buffer2,n2*sizeof(int));
break;
default :
printf("\n In parent process \n ");
printf("Enter limit :");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&buffer[i]);
write(p1[1],buffer,n*sizeof(int));
waitpid(pid,NULL,0);
printf("\n now printing prime number \n");
n2=read(p2[0],buffer2,sizeof(buffer2));
n2=n2/sizeof(int);
for(i=0;i<n2;i++)
printf("prime=%d\n",buffer2[i]);
break;
}
}