-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIPC using PIPE.c
84 lines (67 loc) · 1.16 KB
/
IPC using PIPE.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
int main()
{
int fd[2],i,j,n;
pid_t pid;
if(pipe(fd)==-1)
{
perror("pipe error");
return 0;
}
pid=fork();
if(pid<0)
{
printf("Child creation failed");
return 0;
}
else if(pid>0)
{
close(fd[0]);
printf("P: Enter the total numbers:\n");
scanf("%d",&n);
int numbers[n];
printf("P: Enter the numbers:\n");
for(i=0;i<n;i++)
{
scanf("%d",&numbers[i]);
}
write(fd[1],&n,sizeof(int));
write(fd[1],numbers,n*sizeof(int));
close(fd[1]);
//wait(NULL);
}
else if(pid==0)
{
close(fd[1]);
read(fd[0],&n,sizeof(int));
int r_numbers[n];
read(fd[0],r_numbers,n*sizeof(int));
close(fd[0]);
printf("c: Numbers Read from the pipe:\n");
for(i=0;i<n;i++)
{
printf("%d\t",r_numbers[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(r_numbers[j]>r_numbers[j+1])
{
int temp=r_numbers[j];
r_numbers[j]=r_numbers[j+1];
r_numbers[j+1]=temp;
}
}
}
printf("\nC: Sorted numbers are:\n");
for(i=0;i<n;i++)
printf("%d\t",r_numbers[i]);
exit(EXIT_FAILURE);
}
return 0;
}