overhaul of the testapp, started making menu of tests, broke existing tests out into
[newos.git] / apps / testapp / pipetests.cpp
blob4e35dc4ecc7c3d188bec75c68a6a7635fab32996
1 /*
2 ** Copyright 2003, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 #include <string.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <sys/syscalls.h>
10 static int pipe_read_thread(void *args)
12 int fd = *(int *)args;
13 int err;
14 char buf[1024];
15 int i;
17 for(;;) {
18 err = read(fd, buf, sizeof(buf));
19 printf("pipe_read_thread: read returns %d\n", err);
20 if(err < 0)
21 break;
23 printf("'");
24 for(i=0; i<err; i++)
25 printf("%c", buf[i]);
26 printf("'\n");
29 return err;
32 static int pipe_test(void)
34 int fds[2];
35 int err;
36 char buf[1024];
37 thread_id id;
39 err = pipe(fds);
40 printf("pipe returns %d\n", err);
41 printf("%d %d\n", fds[0], fds[1]);
43 #if 1
44 id = sys_thread_create_thread("pipe read thread", &pipe_read_thread, &fds[1]);
45 sys_thread_resume_thread(id);
47 sys_snooze(2000000);
49 err = write(fds[0], "this is a test", sizeof("this is a test"));
50 printf("write returns %d\n", err);
51 if(err < 0)
52 return err;
54 sys_snooze(2000000);
56 close(fds[0]);
57 close(fds[1]);
58 #endif
59 #if 0
60 // close the reader end and write to it
61 close(fds[1]);
62 err = write(fds[0], "this is a test", sizeof("this is a test"));
63 printf("write returns %d\n", err);
65 sys_snooze(2000000);
66 #endif
67 return 0;