use the -newos toolchain even if -elf is present.
[newos.git] / apps / testapp / pipetests.cpp
blob244b2dbe77cdf96913b8dfe33ae266d74998b030
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 int pipe_test(void)
34 int fds[2];
35 int err;
36 thread_id id;
38 err = pipe(fds);
39 printf("pipe returns %d\n", err);
40 printf("%d %d\n", fds[0], fds[1]);
42 #if 1
43 id = _kern_thread_create_thread("pipe read thread", &pipe_read_thread, &fds[1]);
44 _kern_thread_resume_thread(id);
46 usleep(2000000);
48 err = write(fds[0], "this is a test", sizeof("this is a test"));
49 printf("write returns %d\n", err);
50 if(err < 0)
51 return err;
53 usleep(2000000);
55 close(fds[0]);
56 close(fds[1]);
57 #endif
58 #if 0
59 // close the reader end and write to it
60 close(fds[1]);
61 err = write(fds[0], "this is a test", sizeof("this is a test"));
62 printf("write returns %d\n", err);
64 usleep(2000000);
65 #endif
66 return 0;