summaryrefslogtreecommitdiff
path: root/user/pingpong.c
diff options
context:
space:
mode:
Diffstat (limited to 'user/pingpong.c')
-rw-r--r--user/pingpong.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/user/pingpong.c b/user/pingpong.c
new file mode 100644
index 0000000..7b03a76
--- /dev/null
+++ b/user/pingpong.c
@@ -0,0 +1,44 @@
+#include "kernel/types.h"
+#include "user/user.h"
+
+int
+main(int argc, char* argv[])
+{
+ int p[2];
+
+ if (argc > 1) {
+ fprintf(2, "usage: pingpong\n");
+ exit(1);
+ }
+
+ pipe(p);
+
+ int pid = fork();
+
+ if (pid == 0) {
+ short n;
+ read(p[0], &n, sizeof(n));
+ if (n == 42) {
+ fprintf(1, "%d: received ping\n", getpid());
+ }
+ n++;
+ write(p[1], &n, sizeof(n));
+ close(p[0]);
+ close(p[1]);
+ exit(0);
+ }
+
+ short n = 42;
+
+ write(p[1], &n, sizeof(n));
+ read(p[0], &n, sizeof(n));
+ if (n == 43) {
+ fprintf(1, "%d: received pong\n", getpid());
+ }
+ close(p[0]);
+ close(p[1]);
+
+ wait(0);
+
+ exit(0);
+}