Added a test for the pipe implementation.
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3705 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
1792510380
commit
32169869e8
@ -5,6 +5,7 @@ KernelObjects
|
|||||||
fibo_main.c
|
fibo_main.c
|
||||||
init.c
|
init.c
|
||||||
monitor_test.c
|
monitor_test.c
|
||||||
|
pipe_test.c
|
||||||
true_main.c
|
true_main.c
|
||||||
sig_test.c
|
sig_test.c
|
||||||
select_test.c
|
select_test.c
|
||||||
|
93
src/kernel/apps/pipe_test.c
Normal file
93
src/kernel/apps/pipe_test.c
Normal file
@ -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 <OS.h>
|
||||||
|
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user