0

As the argument of accept() for new client socket, the listener socket is in shared memory area and is shared by all forked server processes.

but each server processesaccept()returns the same socket descriptor afteraccept()` is called by all different forked processes.

Does the fork() also makes separate area for socket descriptors and each forked process manage the area separately? Is that why they produce duplicate socket descriptors?

I intended to use select() to detect changes on all socket descriptors, but because they produce all same descriptors, I couldn't make it out..

alk
  • 69,737
  • 10
  • 105
  • 255
Swen
  • 79
  • 2
  • 3
  • 8
  • This setup sounds strange, you are calling `accept()` on the same socket from multiple servers that you've `fork()`ed? Eh? Normally, you have a single server calling `accept()` on the socket, and `fork()` a separate process to handle the client socket... – Nim Dec 06 '13 at 11:13
  • Possible dup: http://stackoverflow.com/questions/17061961/unix-accept-function-returns-the-same-file-descriptor-twice – MeNa Dec 06 '13 at 11:14
  • @MeNa: The linked question refers to threads not processes. – alk Dec 06 '13 at 11:17
  • 2
    @alk You right... this is about processes: http://stackoverflow.com/questions/5915144/c-same-file-descriptors-of-all-client-connections-client-server-programming – MeNa Dec 06 '13 at 11:23
  • 2
    The listening socket is shared because it was a open file descriptor when you forked, not because you are using shared memory (which isn't required). Each established new connection exists only in one process, and can't be accessed from the others (that were forked before the `accept`) – Douglas Leeder Dec 06 '13 at 11:38
  • Douglas, it was really useful answer. Then even if I had different number of socket descriptor from each other processes, they also can`t be accessed from each other processes, right? – Swen Dec 06 '13 at 11:41

1 Answers1

4

Yes, socket descriptors' (as well as file descriptors) values are managed on a per-process base.

alk
  • 69,737
  • 10
  • 105
  • 255
  • Then all file descriptors are also managed by per process base? Then the shared file descriptors are the file descriptors that were created before forking? – Swen Dec 06 '13 at 11:53
  • 2
    Just for completeness... it's not true that inheriting over fork is the *only* way to share a file descriptor with another process. There is also file descriptor passing. See for example http://stackoverflow.com/questions/2358684/can-i-share-a-file-descriptor-to-another-process-on-linux-or-are-they-local-to-t –  Dec 06 '13 at 13:12
  • Wumpus! If I could see your answer a bit earlier, That would have changed a lot for the work I`m doing. Thanks anyway really much. – Swen Dec 07 '13 at 22:56