From 32169869e8a58c6f9d89165b0fb1cffe47f66622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sat, 28 Jun 2003 04:17:31 +0000 Subject: [PATCH] Added a test for the pipe implementation. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3705 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kernel/apps/Jamfile | 1 + src/kernel/apps/pipe_test.c | 93 +++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 src/kernel/apps/pipe_test.c diff --git a/src/kernel/apps/Jamfile b/src/kernel/apps/Jamfile index 3ec2473eb5..3c0bada0bd 100644 --- a/src/kernel/apps/Jamfile +++ b/src/kernel/apps/Jamfile @@ -5,6 +5,7 @@ KernelObjects fibo_main.c init.c monitor_test.c + pipe_test.c true_main.c sig_test.c select_test.c diff --git a/src/kernel/apps/pipe_test.c b/src/kernel/apps/pipe_test.c new file mode 100644 index 0000000000..2e5cfbba9d --- /dev/null +++ b/src/kernel/apps/pipe_test.c @@ -0,0 +1,93 @@ +/* tests basic pipes functionality */ + +/* +** Copyright 2003, Axel Dörfler, axeld@pinc-software.de. All rights reserved. +** Distributed under the terms of the OpenBeOS License. +*/ + + +#include + +#include +#include +#include + + +static int32 +reader_func(void *data) +{ + char buffer[1024]; + int input = (int)data; + int bytes; + + while ((bytes = read(input, buffer, sizeof(buffer) - 1)) > 0) { + buffer[bytes] = '\0'; + // null-terminate, just in case + printf("reader: (%d) %s\n", bytes, buffer); + + if (!strcmp(buffer, "QUIT")) + break; + } + puts("reader quits"); + + return 0; +} + + +static int32 +writer_func(void *data) +{ + int output = (int)data; + int i; + + const char *strings[] = { + "1. If you read this", + "2. the pipe implementation", + "3. seems to work", + "4. at least a bit", + "QUIT", + NULL}; + + for (i = 0; strings[i] != NULL; i++) { + snooze(100000); + // make sure the reader is waiting for us... + // (needed by the current pipefs implementation :) + + printf("writer: (%ld)\n", write(output, strings[i], strlen(strings[i]) + 1)); + } + puts("writer quits"); + + return 0; +} + + +int +main(int argc, char **argv) +{ + thread_id reader, writer; + int stream[2]; + status_t returnCode; + + if (pipe(stream) < 0) { + fprintf(stderr, "pipe creation failed!\n"); + return -1; + } + + reader = spawn_thread(reader_func, "Reader" , B_NORMAL_PRIORITY, (void *)stream[0]); + resume_thread(reader); + + writer = spawn_thread(writer_func, "Writer" , B_NORMAL_PRIORITY, (void *)stream[1]); + resume_thread(writer); + + puts("reader & writer started."); + + // wait until they quit + wait_for_thread(reader, &returnCode); + wait_for_thread(writer, &returnCode); + + close(stream[0]); + close(stream[1]); + + return 0; +} +