read not blocking on named pipe

自作多情 提交于 2019-12-04 01:18:27

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 means EOF. FIFO can be read or written only when there are processes connected to it for both reading and writing. When there are no more writers (your shell scripts terminated) readers are notified about that through read() returning the EOF.

FIFOs behave that way to be compatible with shell pipe logic e.g.:

$ mkfifo ./tmp1
$ cat < input > ./tmp1 &
$ cat < ./tmp1 > /dev/null

If read() will not return EOF, the second cat would block forever.

I just need it to sit there waiting for data process it and then go back and wait for more

In your C program you have to reopen() the FIFO after read() returned the EOF first time.

P.S. Found quite nice FIFO summary for your. Check the table on the second page.

your bash script closes the pipe so the C is getting an "eof" condition

I think write side shell script close the pipe every time when echo something.

So, the write script need to open the pipe and repeatedly use the opended descriptor to write something and close the opended descripter finally.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!