kadina
kadina

Reputation: 5376

Is it really necessary to close the unused end of the pipe in a process

I am reading about the pipes in UNIX for inter process communication between 2 processes. I have following question

Is it really necessary to close the unused end of the pipe? for example, if my parent process is writing data in to the pipe and child is reading from pipe, is it really necessary to close the read end of the pipe in parent process and close the write end from child process? Are there any side effects if I won't close those ends? Why do we need to close those ends?

Upvotes: 20

Views: 9477

Answers (4)

Pavi
Pavi

Reputation: 61

Refer here

Quoting from above reference:

[...] Each pipe provides one-way communication; information flows from one process to another.

For this reason, the parent and child process should close unused end of the pipe.

There is actually another more important reason for closing unused ends of the pipe.

The process reading from the pipe blocks when making the read system call unless:

  1. The pipe contains enough data to fill the reader's buffer or,
  2. The end-of-file character is sent. The end-of-file character is sent through the pipe when every file descriptor to write end of the pipe is closed. Any process reading from the pipe and forgetting to close the write end of the pipe will never be notified of the "end-of-file" [...]

Upvotes: 1

DoxyLover
DoxyLover

Reputation: 3484

Here's the problem if you don't. In your example, the parent creates a pipe for writing to the child. It then forks the child but does not close its own read descriptor. This means that there are still two read descriptors on the pipe.

If the child had the only one and it closed it (for example, by exiting), the parent would get a SIGPIPE signal or, if that was masked, an error on writing to the pipe.

However, there is a second read descriptor on the pipe (the parent's). Now, if the child exits, the pipe will remain open. The parent can continue to write to the pipe until it fills and then the next write will block (or return without writing if non-blocking).

Thus, by not closing the parent's read descriptor, the parent cannot detect that the child has closed its descriptor.

Upvotes: 23

jaroslawj
jaroslawj

Reputation: 462

Pipes are destined to be used as unidirectional communication channels. Closing them is a good practice allowing to avoid some mess in sent messages. Writer's descriptor should be closed for reader and vice versa.

Upvotes: 2

user3386109
user3386109

Reputation: 34829

According to the man page for getdtablesize

Each process has a fixed size descriptor table, which is guaranteed to have at least 20 slots.

Each pipe uses two entries in the descriptor table. Closing the unneeded end of the pipe frees up one of those descriptors. So, if you were unfortunate enough to be on a system where each process is limited to 20 descriptors, you would be highly motivated free up unneeded file descriptors.

Upvotes: 4

Related Questions