Error[8]: Undefined offset: 3, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述我有一个C代码从管道读取,然后应该阻止,但它从不阻止 int pipe_fd;int res;int open_mode = O_RDONLY;char buf[100];int bytes_read = 0;memset (buf, '\0', sizeof(buf));pipe_fd = open(FIFO_NAME, open_mode);if (access(FIFO_N 我有一个C代码从管道读取,然后应该阻止,但它从不阻止
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为你的.检查第二页上的表格.

总结

以上是内存溢出为你收集整理的在命名管道上读取不阻塞全部内容,希望文章能够帮你解决在命名管道上读取不阻塞所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
在命名管道上读取不阻塞_C_内存溢出

在命名管道上读取不阻塞

在命名管道上读取不阻塞,第1张

概述我有一个C代码从管道读取,然后应该阻止,但它从不阻止 int pipe_fd;int res;int open_mode = O_RDONLY;char buf[100];int bytes_read = 0;memset (buf, '\0', sizeof(buf));pipe_fd = open(FIFO_NAME, open_mode);if (access(FIFO_N 我有一个C代码从管道读取,然后应该阻止,但它从不阻止
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为你的.检查第二页上的表格.

总结

以上是内存溢出为你收集整理的在命名管道上读取不阻塞全部内容,希望文章能够帮你解决在命名管道上读取不阻塞所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1252486.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存