-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.c
56 lines (47 loc) · 791 Bytes
/
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
#include"pipe.h"
/* return the position of '|' */
int is_pipe(char **args)
{
for (int i=0; args[i]; i++)
{
if (strcmp(args[i],"|") == 0)
{
return i;
}
}
return 0;
}
/* to run when there is a pipe*/
int make_pipe(char **args1, char **args2)
{
int pid;
int p[2];
if (pipe(p) == -1)
error_occur("pipe");
pid=fork();
if (pid == -1)
error_occur("fork");
if (pid == 0)
{
close(p[0]) ;
if (dup2(p[1],1) == -1)
error_occur("dup2");
close(p[1]);
//execvp(args1[0],args1);
execute(args1);
exit(1);// exit directly
}
else
{
if (wait(NULL) == -1)
error_occur("while waiting");
close(p[1]);
if (dup2(p[0],0) == -1)
error_occur("dup2");
close(p[0]);
//execvp(args2[0],args2);
execute(args2);
exit(1);// exit directly
}
return 0;
}