From 0d6a64fa06ce6aae729fa05a539eadd88fa59007 Mon Sep 17 00:00:00 2001 From: Mole Shang <135e2@135e2.dev> Date: Wed, 17 Jan 2024 11:33:39 +0800 Subject: lab util: finish --- user/pingpong.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 user/pingpong.c (limited to 'user/pingpong.c') 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); +} -- cgit v1.2.3