FIFO 两个进程同时写同一个FIFO

将“IPC-FIFO ,open and read are blocked”那篇
的test1.c( line25)的sleep(10)的注释取消,
便是本文的test1.c
目的是在10秒之内(从test1开始运行算起),把test2和test3都运行起来
即在test1去读FIFO之前,test1和test3要把各自的数据都写到同一个FIFO中
将test2.c (line 103)的 hello from改为hihi from ,
便是test3.c
目的是测试test1是否会将test2和test3发过来的数据弄混掉
test2.c不变

[root@localhost ch13]# ./test1&
[2] 4751
[root@localhost ch13]# 
 DBG(test1.c, main(), 13): print from test1:will mkfifo SERVER_FIFO_NAME
 DBG(test1.c, main(), 15): print from test1:already mkfifo SERVER_FIFO_NAME
 DBG(test1.c, main(), 17): print from test1:will open SERVER_FIFO_NAME
./test2&
[3] 4752
[root@localhost ch13]# 
 DBG(test2.c, main(), 12): print from test3:will open SERVER_FIFO_NAME
 DBG(test1.c, main(), 23): print from test1:already open SERVER_FIFO_NAME
 DBG(test2.c, main(), 14): print from test3:already open SERVER_FIFO_NAME
 DBG(test2.c, main(), 26): print from test3:will mkfifo client_fifo
 DBG(test2.c, main(), 31): print from test3:already mkfifo client_fifo
 DBG(test2.c, main(), 38): 4752 sent:"hello from 4752" 
 DBG(test2.c, main(), 39): print from test3:will write server_fifo_fd
 DBG(test2.c, main(), 41): print from test3:already write server_fifo_fd
 DBG(test2.c, main(), 42): print from test3:will open client_fifo
./test3

 DBG(test3.c, main(), 12): print from test3:will open SERVER_FIFO_NAME
 DBG(test3.c, main(), 14): print from test3:already open SERVER_FIFO_NAME
 DBG(test3.c, main(), 26): print from test3:will mkfifo client_fifo
 DBG(test3.c, main(), 31): print from test3:already mkfifo client_fifo
 DBG(test3.c, main(), 38): 4753 sent:"hihi from 4753" 
 DBG(test3.c, main(), 39): print from test3:will write server_fifo_fd
 DBG(test3.c, main(), 41): print from test3:already write server_fifo_fd
 DBG(test3.c, main(), 42): print from test3:will open client_fifo
 DBG(test1.c, main(), 27): print from test1:will read server_fifo_fd
 DBG(test1.c, main(), 29): print from test1:hello from 4752
 DBG(test1.c, main(), 30): print from test1:already read server_fifo_fd
 DBG(test1.c, main(), 49): print from test1:will open client_fifo
 DBG(test1.c, main(), 51): print from test1:already open client_fifo
 DBG(test1.c, main(), 53): print from test1:will write client_fifo_fd
 DBG(test1.c, main(), 55): print from test1:already write client_fifo_fd
 DBG(test1.c, main(), 27): print from test1:will read server_fifo_fd
 DBG(test1.c, main(), 29): print from test1:hihi from 4753
 DBG(test1.c, main(), 30): print from test1:already read server_fifo_fd
 DBG(test1.c, main(), 49): print from test1:will open client_fifo
 DBG(test1.c, main(), 51): print from test1:already open client_fifo
 DBG(test1.c, main(), 53): print from test1:will write client_fifo_fd
 DBG(test1.c, main(), 55): print from test1:already write client_fifo_fd
 DBG(test1.c, main(), 27): print from test1:will read server_fifo_fd
 DBG(test2.c, main(), 44): print from test3:already open client_fifo
 DBG(test2.c, main(), 46): print from test3:will read client_fifo_fd
 DBG(test2.c, main(), 48): 4752 received: "HELLO FROM 4752"
 DBG(test2.c, main(), 50): print from test3:already read client_fifo_fd
 DBG(test3.c, main(), 44): print from test3:already open client_fifo
 DBG(test3.c, main(), 46): print from test3:will read client_fifo_fd
 DBG(test3.c, main(), 48): 4753 received: "HIHI FROM 4753"
 DBG(test3.c, main(), 50): print from test3:already read client_fifo_fd
 DBG(test1.c, main(), 29): print from test1:HIHI FROM 4753
 DBG(test1.c, main(), 30): print from test1:already read server_fifo_fd
[3]+  Done                    ./test2
[2]+  Done                    ./test1
[root@localhost ch13]# 


从line29-line35和line36-line42的打印信息可以看出
test1从FIFO里面读出了一些数据
第一次读出”hello from 4752“
第一次读出“hihi from 4753”
这表明test2和test3向同一个FIFO里面写数据的时候是公用一个读写位置指针的,所以不会覆盖
即两个进程公用一个文件表
貌似在apue  p73讲到一点,走着瞧....

然后test1根据读出的data再建立相应的FIFO ,2个,将处理过的数据写进对应FIFO,
然后test2和test3再去对应的FIFO读取

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值