-
Notifications
You must be signed in to change notification settings - Fork 9
/
beos-runpiped.c
60 lines (58 loc) · 1.11 KB
/
beos-runpiped.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
#include <fcntl.h>
#include <unistd.h>
#include <be/kernel/OS.h>
int main(int argc, char **argv)
{
int pid,fd1,fd2;
sem_id sem1,sem2;
sem1=create_sem(1,"piperun");
sem2=create_sem(1,"piperun");
if (sem1<B_NO_ERROR ||sem2<B_NO_ERROR) {
perror("create_sem");
exit(1);
}
acquire_sem(sem1);
acquire_sem(sem2);
pid=fork();
if (pid==0) {
fd1=open("/pipe/1",O_WRONLY|O_CREAT,0666);
if (fd1==-1) {
perror("writer: /pipe/1");
_exit(1);
}
release_sem(sem1);
/* wait for other side to open the pipe 1 */
acquire_sem(sem2);
/* wait for creation of pipe 2 */
acquire_sem(sem1);
fd2=open("/pipe/2",O_RDONLY);
if (fd2==-1) {
perror("/pipe/2");
_exit(1);
}
release_sem(sem2);
dup2(fd2,0);
dup2(fd1,1);
system(argv[2]);
_exit(1);
}
acquire_sem(sem1);
fd1=open("/pipe/1",O_RDONLY);
release_sem(sem2);
if (fd1==-1) {
perror("/pipe/1");
exit(1);
}
fd2=open("/pipe/2",O_WRONLY|O_CREAT,0666);
if (fd2==-1) {
perror("writer: /pipe/2");
exit(1);
}
release_sem(sem1);
/* wait for child to open ... */
acquire_sem(sem2);
dup2(fd1,0);
dup2(fd2,1);
system(argv[1]);
exit(0);
}