#include "stat.h"
#include "user.h"
-int
-main(int argc, char *argv[])
+#define STDOUT_FD 1
+#define READ_END 0
+#define WRITE_END 1
+
+int main(int argc, char *argv[])
{
- // Student code goes here
+ // Student code goes here
+ /* child read parent write, parent read child write */
+ /* having 2 pipes allows duplex communication */
+ int crpw[2], prcw[2];
+ int pid;
+ char mypid;
+
+ pipe(crpw); pipe(prcw);
+
+ /* create the two processes */
+ pid = fork();
+
+ /* get the process' PID */
+ mypid = (char) getpid();
+
+ if (pid == 0) {
+ /* child stuff */
+ char parent_pid;
+ /* close ends of pipes that we aren't using */
+ close(prcw[READ_END]); close(crpw[WRITE_END]);
+ /* read data */
+ read(crpw[READ_END], &parent_pid, 1);
+ printf(STDOUT_FD, "parent: received ping from %d\n", (int) parent_pid);
+ /* write data */
+ write(prcw[WRITE_END], &mypid, 1);
+ /* close my write pipe so that my friend doesn't block */
+ close(prcw[WRITE_END]);
+ } else {
+ /* parent stuff */
+ char child_pid;
+ /* close ends of pipes that we aren't using */
+ close(crpw[READ_END]); close(prcw[WRITE_END]);
+ /* write data */
+ write(crpw[WRITE_END], &mypid, 1);
+ wait();
+ /* read data */
+ read(prcw[READ_END], &child_pid, 1);
+ printf(STDOUT_FD, "parent: received pong from %d\n", (int) child_pid);
+ /* close my write pipe so that my friend doesn't block */
+ close(crpw[WRITE_END]);
+ }
- exit();
-}
\ No newline at end of file
+ exit();
+}