Skip to content

Commit

Permalink
Lab util: pingpong
Browse files Browse the repository at this point in the history
  • Loading branch information
foyoodo committed Mar 7, 2021
1 parent aa8d91d commit f5693b0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ UPROGS=\
$U/_ln\
$U/_ls\
$U/_mkdir\
$U/_pingpong\
$U/_rm\
$U/_sh\
$U/_sleep\
Expand Down
31 changes: 31 additions & 0 deletions user/pingpong.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"

int
main(int argc, char *argv[])
{
if (argc > 1) {
fprintf(2, "Usage: pingpong\n");
exit(1);
}

int p[2];
char buf[4];

pipe(p);

if (fork() == 0) {
read(p[0], buf, 4);
fprintf(1, "%d: received %s\n", getpid(), buf);
write(p[1], "pong", 4);
exit(0);
} else {
write(p[1], "ping", 4);
wait(0);
read(p[0], buf, 4);
fprintf(1, "%d: received %s\n", getpid(), buf);
}

exit(0);
}

0 comments on commit f5693b0

Please sign in to comment.