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-02-05 18:03:17 +0800
commit0d6a64fa06ce6aae729fa05a539eadd88fa59007 (patch)
treeae42de2d64325fc09285fd843851f017b1ef6a6b /user/pingpong.c
parentb9af3ad894701e8805dd9f63e646487a7f09f9f3 (diff)
downloadxv6-labs-0d6a64fa06ce6aae729fa05a539eadd88fa59007.tar.gz
xv6-labs-0d6a64fa06ce6aae729fa05a539eadd88fa59007.tar.bz2
xv6-labs-0d6a64fa06ce6aae729fa05a539eadd88fa59007.zip
lab util: finish
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);
+}