I have two processes, a server and a client, that should communicate via pipes (C++, Linux).
The server opens the pipe with the O_RDONLY
flag, and the client with O_WRONLY
.
However, the server blocks at the open
function, while the client seems to run successfully (the open
function returns success and so do the write
functions).
I have read that if the O_NONBLOCK
flag is set, the read function will continue, but I don't want it to continue if no client is connected - it is ok to block until a client is connected, but in my case it remains blocked even after the client finishes running...
Can you plese tell me what I'm doing wrong...?
Here is the code:
// Server side
int pipe;
int status, nr_read = 0;
status = mkfifo(FIFO_NAME, 0666);
if (status < 0)
{
// If the file already exists, delete it
unlink(FIFO_NAME);
// Try again
status = mkfifo(FIFO_NAME, 0666);
if(status < 0)
{
printf("mkfifo error: %d\n", status);
return status;
}
}
pipe = open(FIFO_NAME, O_RDONLY);
printf("Never gets here...\n");
[...]
nr_read = read(pipe, my_char_array, CHAR_ARRAY_SIZE);
[...]
close(pipe);
unlink(FIFO_NAME);
It never gets to the "printf" line...
// Client side:
int pipe, nr_sent = 0;
int status = 0;
pipe = open(FIFO_NAME, O_WRONLY);
if (pipe < 0)
{
printf("open fifo error: %d\n", status);
return pipe;
}
[...]
nr_sent = write(pipe, my_char_array, CHAR_ARRAY_LENGTH);
[...]
close(pipe);
EDIT
I didn't mention the line
#define FIFO_NAME "MYFIFO"
... and here was the problem: as Jody Hagins said, the path being a relative one and the processes being started from different folders, they were trying to open different files.