-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathtestpipe1.c
60 lines (57 loc) · 1.24 KB
/
testpipe1.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 <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
int main()
{
pid_t pid;
int cnt = 0;
int pipefd[2];
char buf;
char received[20];
int receivep = 0;
char w[12];
char r[12];
if (pipe(pipefd) == -1)
{
printf("pipe");
exit(-1);
}
write(pipefd[1], "test", strlen("test"));
close(pipefd[1]);
while (read(pipefd[0], &buf, 1) > 0)
received[receivep++] = buf;
received[receivep] = 0;
receivep = 0;
assert(strcmp(received, "test") == 0);
close(pipefd[0]);
if (pipe(pipefd) == -1)
{
printf("pipe");
exit(-1);
}
sprintf(w, "%d", pipefd[1]);
sprintf(r, "%d", pipefd[0]);
pid = vfork();
if (pid < 0)
printf("error in fork!\n");
else if (pid == 0)
{
execl("/bin/testpipe2", "/bin/testpipe2", r, w, NULL);
exit(0);
}
else if (pid > 0)
{
close(pipefd[1]);
while (read(pipefd[0], &buf, 1) > 0)
received[receivep++] = buf;
received[receivep] = 0;
assert(strcmp(received, "hello pipe") == 0);
close(pipefd[0]);
}
return 0;
}