Mat.S
Mat.S

Reputation: 1854

Why close pipe in c?

What could go wrong if the reader of a pipe forgets to close fd[1] or if the writer of a pipe forgets to close fd[0]?

Upvotes: 2

Views: 1039

Answers (2)

Prabhu
Prabhu

Reputation: 3541

No resource runs infinite for a given process. So is the number of files , sockets that a process can create. Failing to close the FDs after use can cause something akin to memory leak if your processes once again requests new FDs.

Check ulimit for the number of open files allowed. You can try creating new descriptors without close. You should soon run out of it.

Upvotes: 0

DarkDust
DarkDust

Reputation: 92424

You'll have a file handle leak (as long as the process that has the file descriptor open is running). Worst thing that can happen is that you run out of file descriptor handles if you have lot of pipes.

There's usually a soft and a hard limit (see ulimit) per user, and also a system wide limit (although you're unlikely to hit that if your system has a useful per-user limit). Once you run out of file descriptor handles, strange things happen like you won't be able to start new processes or other running processes might stop working correctly.

Most of the time this isn't something to worry about as most of the time there's just two processes and one pipe, so the leak won't be a big deal. Still, you usually really want to close any filehandle you don't need any more to free up resources.

Upvotes: 1

Related Questions