IPC: Using of named pipes in c++ between two programs

戏子无情 提交于 2019-12-05 08:58:44

"First of all if no reading process is attached to the fifo pipe all messages except of the last one written to the pipe get lost".

No, they don't. Use cat instead of your (lousily written :D) reading process and you'll get all of the messages.

You are right. Pipes are byte-oriented, not message oriented. If you want messages, there's other IPC for that (e.g., SysV message queues).

Beej's Guide to Unix IPC is an excellent intro to Unix ipc, if you want something more advanced, then Advanced Programming in the Unix Environment by Richard Stevens is very good.

As far as user authentication is concerned, check out Unix sockets.

Looks like you're trying to use pipes for what they were not designed for. First of all, there have to be reading process "attached" for the other side of the pipe. If you try to open pipe for writing and there is no reading process, open will hang waiting for it or return -1 with errno set to ENXIO (when O_NONBLOCK flag is used). So named pipe is a kind of randez-vous point in the system, where two processes met to exchange data ;)

Second - do not treat writing to a pipe as sending a message. Pipe is rather a "stream of bytes". You can write 10 bytes once, and the reader can do 5 reads of 2 bytes each, or vice-versa: you can write 5 times 2 bytes, and the reader can read them at once. So if you plan to send some kind of "messages" over the pipe you should develop some kind of minimal transport protocol (for example use '\0' byte as a delimiter, as you mentioned above).

So, maybe you should look at System V message queues (see msgget, msgctl, msgrcv, msgsnd, or POSIX message queues (see mq_open, mq_unlink, mq_send, mq_receive etc.), which gives you possibility to send and receive 'atomic' messages.

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