How to open a private file descriptor

I have a scenario where I created a channel for communication between two child and parent. The parent writes (using the write function) the data to the channel and closes the corresponding file descriptor. The problem is that when I want to write data to the channel again, the write function returns an error code of -1. I think this is because the end of the record was closed at the previous iteration. Then how to open the corresponding file descriptor after it has been closed once.

I tried to use the open () function, which requires specifying the path to some file as an argument. But I do not use files in my application. I have simple file descriptors (int arr [2]).

Is it possible to achieve the above scenario with pipes ????

+6
source share
2 answers

Once the pipe is closed, it closes. You cannot return it.

If you want to write more, don’t close it first - it’s so simple.

+10
source

To know about anything related to files (channels are also files of some type) under Unix: the file name is used only when opening a file. Later, until the file is opened, it will be available forever before closing, and the name will never be used again. When someone deletes a file in another window while it is open, the name just leaves, not the file. It means:

  • The file is still on disk.
  • He has no name
  • He is still open
  • When it is closed, the core removes it forever

Knowing this, perhaps, helps to understand why it would be almost impossible to “reopen” a file, pipe, or something like that again. The file name and descriptor have different lifetimes.

The only exceptions are stdout and stderr, whose handle is always known as 1 and 2.

+1
source

Source: https://habr.com/ru/post/912017/


All Articles