Lab: Xv6 and Unix utilities

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.

这是实验要求。用下面一张图来描述
在这里插入图片描述
关键有两点

  1. 如何搞出两个进程
  2. 如何保证顺序

然后看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中没有数据,会一直等待。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值