summaryrefslogtreecommitdiff
path: root/user/pingpong.c
diff options
context:
space:
mode:
authorMole Shang <[email protected]>2024-01-17 11:33:39 +0800
committerMole Shang <[email protected]>2024-01-17 11:33:39 +0800
commitdaa90a639a35e9f99747c92bb28946ac414615bd (patch)
treecc119f4e037015b2fdff2b4ed8d989b8db66e3b0 /user/pingpong.c
parentc424f997808269559f7968c812860fd1f1974a13 (diff)
downloadxv6-labs-daa90a639a35e9f99747c92bb28946ac414615bd.tar.gz
xv6-labs-daa90a639a35e9f99747c92bb28946ac414615bd.tar.bz2
xv6-labs-daa90a639a35e9f99747c92bb28946ac414615bd.zip
lab util: finishutil
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);
+}