From 8ea8d4eee537aff131cd7e65cfe3b1b157e40ed4 Mon Sep 17 00:00:00 2001 From: Cyrill Gorcunov Date: Tue, 22 Nov 2011 13:20:05 +0400 Subject: [PATCH] test: pipes -- Add a second pipe Signed-off-by: Cyrill Gorcunov --- test/test-pipe-async.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/test/test-pipe-async.c b/test/test-pipe-async.c index ed37b99f6..87f9a10b8 100644 --- a/test/test-pipe-async.c +++ b/test/test-pipe-async.c @@ -16,13 +16,14 @@ static void *map; int main(int argc, char *argv[]) { - int pipefd[2]; + int pipefd1[2]; + int pipefd2[2]; pid_t pid; printf("%s pid %d\n", argv[0], getpid()); - if (pipe(pipefd)) { - perror("Can't create pipe"); + if (pipe(pipefd1)) { + perror("Can't create pipe1"); return -1; } @@ -32,15 +33,37 @@ int main(int argc, char *argv[]) return 1; } else if (pid == 0) { long buf; - while (read(pipefd[0], &buf, sizeof(buf)) > 0) { - printf("pipe-r: %08lx\n", buf); - sleep(2); + + if (pipe(pipefd2)) { + perror("Can't create pipe2"); + return -1; + } + + pid = fork(); + if (pid == -1) { + printf("fork failed\n"); + return 1; + } else if (pid == 0) { + while (1) { + long buf; + read(pipefd1[0], &buf, sizeof(buf)); + printf("pipe2-r: %08lx\n", buf); + sleep(1); + } + } + + while (1) { + read(pipefd1[0], &buf, sizeof(buf)); + printf("pipe1-r: %08lx\n", buf); + printf("pipe2-w: %08lx\n", buf); + write(pipefd2[1], &buf, sizeof(buf)); + sleep(1); } } else { long buf = 0; while (1) { - printf("pipe-w: %08lx\n", buf); - write(pipefd[1], &buf, sizeof(buf)); + printf("pipe1-w: %08lx\n", buf); + write(pipefd1[1], &buf, sizeof(buf)); sleep(1); buf++; }