* same with xv6
[mascara-docs.git] / i386 / ucla / src / lab5 / user / testpipe.c
blob6cc0b65406e5f2cedd450cdb9ffa0976c623dd96
1 #include <inc/lib.h>
3 const char *msg = "Now is the time for all good men to come to the aid of their party.";
5 asmlinkage void
6 umain(int argc, char **argv)
8 char buf[100];
9 int i, pid, p[2];
11 argv0 = "pipereadeof";
13 if ((i = pipe(p)) < 0)
14 panic("pipe: %e", i);
16 if ((pid = fork()) < 0)
17 panic("fork: %e", i);
19 if (pid == 0) {
20 cprintf("[%08x] pipereadeof close %d\n", thisenv->env_id, p[1]);
21 close(p[1]);
22 cprintf("[%08x] pipereadeof readn %d\n", thisenv->env_id, p[0]);
23 i = readn(p[0], buf, sizeof buf-1);
24 if (i < 0)
25 panic("read: %e", i);
26 buf[i] = 0;
27 if (strcmp(buf, msg) == 0)
28 cprintf("\npipe read closed properly\n");
29 else
30 cprintf("\ngot %d bytes: %s\n", i, buf);
31 exit();
32 } else {
33 cprintf("[%08x] pipereadeof close %d\n", thisenv->env_id, p[0]);
34 close(p[0]);
35 cprintf("[%08x] pipereadeof write %d\n", thisenv->env_id, p[1]);
36 if ((i = write(p[1], msg, strlen(msg))) != strlen(msg))
37 panic("write: %e", i);
38 close(p[1]);
40 wait(pid);
42 argv0 = "pipewriteeof";
43 if ((i = pipe(p)) < 0)
44 panic("pipe: %e", i);
46 if ((pid = fork()) < 0)
47 panic("fork: %e", i);
49 if (pid == 0) {
50 close(p[0]);
51 while (1) {
52 cprintf(".");
53 if (write(p[1], "x", 1) != 1)
54 break;
56 cprintf("\npipe write closed properly\n");
57 exit();
59 close(p[0]);
60 close(p[1]);
61 wait(pid);
63 cprintf("pipe tests passed\n");