FIFO - Restore communication in C ++

寵の児 提交于 2020-01-15 03:47:47

问题


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?

来源:https://stackoverflow.com/questions/40676486/fifo-restore-communication-in-c

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