-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q4.cpp
96 lines (70 loc) · 1.73 KB
/
Q4.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
#include<iostream>
#include<fstream>
#include<unistd.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>
using namespace std;
int main(int argc, char *argv[])
{
pid_t pid1,pid2,pid3;
int pipe1[2],pipe2[2];
pipe(pipe1);
pipe(pipe2);
pid=fork();
if (pid1 > 0) //Parent 1
{
close(pipe1[1]); //Writing end of pipe 1
wait(NULL);
pid2 = fork();
if (pid2 > 0) //Parent 2
{
close(pipe1[0]); //Reading end of pipe 1
close(pipe2[1]); //Writing end of pipe 2
wait(NULL);
pid3 = fork();
if(pid3 > 0) //Parent 3
{
close(pipe2[0]); //Reading end of pipe 2
wait(NULL);
}
else if(pid3 == 0) //Child 3
{
close(pipe1[0]); //Reading end of pipe 1
close(pipe1[1]); //Writing end of pipe 2
close(pipe2[1]); //Writing end of pipe 2
dup2(pipe2[0],0);
execl("/usr/bin/wc","wc",NULL,NULL);
}
else
{
cout<<"Fork3 failed"<<endl;
}
}
else if(pid2 == 0)
{
close(pipe1[1]); //Writing end of pipe 1
close(pipe2[0]); //Reading end of pipe 2
dup2(pipe1[0],0);
dup2(pipe2[1],1);
execl("/usr/bin/sort -r","sort",NULL); // sorting
}
else
{
cout<<"Fork3 failed"<<endl;
}
}
else if (pid1 == 0)
{
close(pipe1[0]); //Reading end of pipe 1
close(pipe2[0]); //Reading end of pipe 1
close(pipe2[1]); //Reading end of pipe 2
dup2(pipe1[1],1);
execl("/bin/ls","ls",NULL,NULL);
}
else
{
cout<<"Fork1 Failed"<<endl;
}
}