From: ptkwt@... (Phil Tomson) Date: 2004-06-18T02:23:27+09:00 Subject: Re: Bidirectional named pipes on Linux In article <40d0f617$1@buckaroo.cs.rit.edu>, Scott Rubin wrote: >Phil Tomson wrote: >> I've got two programs which need to communicate (which eliminates >> un-named pipes). Information needs to be sent in both directions such >> that one program (say it's called the master) sends a couple of bytes >> to and then waits for a response from the other program (say it's called the >> slave) and so on. It looks like if you open the pipe (or FIFO file) from >> both sides in read/write mode that the pipe is opened non-blocking so what >> ends up happening is that the side which sends first just keeps sending and >> doesn't wait for a response. >> >> ...yeah, I should probably be using sockets, but this is a sort of >> 'legacy' system. >> >> phil > >Phil, > >I'm doing this exact same thing on a linux system so that a ruby process >and a C process can communicate. It works perfectly, here is generally >how I did it. First, pipes are one way. You can have one end reading >and one end writing at the same time, but both sides may not write >simultaneously and both ends may not read simultaneously. Pipes are >strictly fifos, that's why the command to make named ones is mkfifo. >One way in, one way out. So you can either make two pipes, one for each >direction or switch who is reading and who is writing, this is difficult >if you ask me. > >Next, just write to it as if it is a file. The data will stay in the >pipe until the other end opens it and then it spills out all over if you >don't catch it. It can only be read out once. So here is how I think you >should do it.. > >Make two fifos, one for each direction. The process that sends first >sends data down the out fifo and calls select on the in fifo. Select >will cause it to block until it is ready to read. The other process >starts out selecting on the incoming fifo and when the data finally >comes in it can send the response on the outgoing fifo. > >Try to think of it like two guys with those vacuum tubes sending >messages in canisters. There is just one canister and the tubes are one >way. The guy puts the canister in the pipe that goes out and then sits >looking at the other pipe waiting for a response. > You wouldn't happen to have the code which shows the select call, would you? Phil