FIFO - Restore communication in C ++

2019-07-21 06:08发布

I have a main program written in C ++. It triggers child programs using vfork () and execl (). Communication between them works perfectly using FIFO (the parent writes and the child reads).

In the main program the sequence for establishing communication is

if (mkfifo(CONTROLLER_file, 0666) < 0)
    perror("mkfifo");

if((aux_fd = open(CONTROLLER_file, O_RDONLY | O_NONBLOCK) )< 0)
    perror("Opening error");

if((fd = open(CONTROLLER_file, O_WRONLY)) < 0)
    perror("Opening error");

I do this to avoid the locking problem waiting for another process to open the file in the other mode.

Writing is done with

write(fd, write_buffer, sizeof(write_buffer));

On the son, I do

if (mkfifo(CONTROLLER_file, 0666) < 0)
    perror("mkfifo");

if((file_descriptor = open(CONTROLLER_file, O_RDONLY)) < 0)
    perror("Opening error");

And I read with

if (mkfifo(CONTROLLER_file, 0666) < 0)
    perror("mkfifo");

if((file_descriptor = open(CONTROLLER_file, O_RDONLY)) < 0)
    perror("Opening error");

The message is sent successfully.

For testing I kill the main program using kill on the Linux terminal and the children continue to run. I call again the main program and it goes into the part of reestablishing communication with the same procedure presented above, opening the same file. I then try to send a message to the child, but he receives nothing.

Does anyone have any idea how I can reestablish this communication?

0条回答
登录 后发表回答