pingpong
Write a program that uses UNIX system calls to ‘‘ping-pong’’ a byte between two processes over a pair of pipes, one for each direction.
The parent should send a byte to the child;
the child should print “: received ping”, where is its process ID, write the byte on the pipe to the parent, and exit;
the parent should read the byte from the child, print “: received pong”, and exit. Your solution should be in the file user/pingpong.c.
这是实验要求。用下面一张图来描述
关键有两点
- 如何搞出两个进程
- 如何保证顺序
然后看hints
Some hints:
Use pipe to create a pipe.
Use fork to create a child.
Use read to read from the pipe, and write to write to the pipe.
那就可以明白,用fork来弄出子进程,这就有了两个进程
用read和write来保证顺序
tips
fork,可以理解为,复制一份一模一样的进程,两个进程的数据,执行进度一模一样。
read系统调用,如果pipe中没有数据,会一直等待。