views:

50

answers:

1

I faced few issues while writing server application using TCP on linux system. I have few queries.

  1. Where are socket FDs are stored and what are the attributes associated with socket FDs.
  2. How the kernel differentiates between FDs like socket FDs, file Fds, Message Queue FDs

Socket FDs are received as

int sockFD = socket(..., ..., ...);

what is the difference between
a) close(sockFD);
and
b) int sockCopy = sockFD; //copy the socketfd
    close(sockCopy);

Case b will not close socket why?

+3  A: 
  1. Socket file descriptors are stored in integer variables in your application, just like other file descriptors.

  2. The kernel internally differentiates between different file descriptor types through the different function pointers within the associated struct file.

  3. There is no difference; int sockCopy = sockFD; close(sockCopy); will close the socket. The kernel does not care what you call the variable that you store the file descriptor in - all it cares about is the numerical value.

caf
+1: Moreover, you have no access to the `struct file` from applications other than by doing kernel calls (obviously!) so all you can distinguish them by is how they behave operationally. (Some calls can only work on some kinds of file descriptor.)
Donal Fellows