tags:

views:

149

answers:

2

I'm seeing an issue using both sockets and pipes in Linux. Specifically, we call pipe(), which allocates the next two available file descriptors... let's say 10 and 11. Then we call accept() on a socket, expecting it to allocate 12. Instead, it allocates 11.

We've tested a bit, and it seems the second FD returned from pipe() is always available for reuse by other syscalls that create file descriptors.

Can anyone explain this?

A: 

Maybe the flag SO_REUSEADDR is used for pipes hence you're not seeing an increment in the file descriptor number?

Edit: Thanks to duck and darron for the heads up on my stupid answer. I was reading this link and if close() is called on a file descriptor, it gets reused...

Hope this helps, Best regards, Tom.

tommieb75
I assume he is talking about unnamed/anonymous pipes. There wouldn't even be an address for SO_REUSEADDR to reuse.
Duck
SO_REUSEADDR has nothing to do with file descriptor numbers. It refers to socket addresses (IP and port).
Darron
@Duck: Pipes and sockets are similar...that was why I suggested that?
tommieb75
@tommieb75 - It's understandable. My guess is he is closing one of the pipe descriptors and getting reused on the accept. Guess time will tell.
Duck
+2  A: 

That would imply that someone is closing the file descriptor in question some time after the call to pipe and before the second syscall. For example, if you fork off some other process to do stuff on one end of the pipe, you might be screwing up your code that closes the end of the pipe used by the other process and closing the wrong end of the pipe. Or just about anything else that might call close on some file descriptor might be closing the wrong thing.

Chris Dodd