Write/Read to/from FIFO files - linux

为君一笑 提交于 2019-12-05 11:01:53

when I ran this at background with ./server.out& it's blocked at the read call and waiting for anyone to write to clientToServer.fifo

Actually it blocks at the open. This is the way FIFOs work. The open (in blocking mode) is going to block until something opens the FIFO on the other end.

the while loop in the server.out breaks as well. meaning, the read no longer blocks the server.out process and awaits other clients, instead, the server program ends

Again, this is normal behavior. Only one client process is connected to the FIFO so when it closes its end then EOF is sent and the server quits. If multiple clients are attached to the FIFO at the same time you won't see EOF until the last client closes it. If you want a long running server to serve multiple clients continuously the easiest way to accomplish it is to open the server's FIFO as read/write. This way there is always a reader/writer - the server itself - and you won't see EOF when even the last client exits. When it is time to shut down the server then close the appropriate end in the server and let nature take its course as the real clients quit.

When the client exits it closes the pipe connection, which causes the read function in the server to return 0 which exits the loop.

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