int pipe_fd;int res;int open_mode = O_RDONLY;char buf[100];int bytes_read = 0;memset (buf,'#!/bin/bashpipe=/tmp/pipeif [[ ! -p $pipe ]]; then echo "Reader not running" exit 1fiif [[ "" ]]; then echo "some string" >$pipeelse echo "q" >$pipefi',sizeof(buf));pipe_fd = open(FIFO_name,open_mode);if (access(FIFO_name,F_OK) == -1){ res = mkfifo(FIFO_name,0777); if (res != 0) { fprintf (stderr,"Could not create fifo %s\n",FIFO_name); exit (EXIT_FAILURE); }}for(;;){ do { res = read(pipe_fd,buf,sizeof(buf)); bytes_read += res; }while (res > 0); // process data then go back and block ............}
它通过一些代码在一个bash脚本中发送一个简单的缓冲区,像这样’./test 1′
$mkfifo ./tmp1$cat < input > ./tmp1 &$cat < ./tmp1 > /dev/null
我在gdb中运行C代码程序,最初它会读取阻塞,但是一旦我调用bash脚本,C代码就不再阻塞,它会成功读取数据
缓冲区然后每次读取时都有0个字节读取,所以不知道为什么它不再阻塞. “一些字符串”数据在另一边正确接收.
我只需要坐在那里等待数据处理,然后回去等待更多
解决方法I run the C code program in gdb and initially it does block on the read but as soon as i call the bash script the C code no longer blocks,it does successfully read the data from the buffer and then each time it reads there are 0 bytes read so not sure why its no longer blocking. The ‘some string’ data is correctly received at the other sIDe.
0表示EOF.只有当连接到它们的进程用于读取和写入时,FIFO才能被读取或写入.当没有更多的作者(你的shell脚本被终止)时,通过read()返回EOF就会通知读者.
FIFO的行为方式与shell管道逻辑兼容,例如:
如果read()不会返回EOF,则第二只猫会永久阻止.
I just need it to sit there waiting for data process it and then go back and wait for more
在C程序中,read()返回EOF后,必须重新打开()FIFO.
附:找到quite nice FIFO summary为你的.检查第二页上的表格.
总结以上是内存溢出为你收集整理的在命名管道上读取不阻塞全部内容,希望文章能够帮你解决在命名管道上读取不阻塞所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)