Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket descriptor vs file descriptor

Tags:

linux

read(2) and write(2) works both on socket descriptor as well as on file descriptor. In case of file descriptor, User file descriptor table->file table and finally to inode table where it checks for the file type(regular file/char/block), and reads accordingly. In case of char spl file, it gets the function pointers based on the major number of the file from the char device switch and calls the appropriate read/write routines registered for the device. Similarly appropriate read/write routine is called for block special file by getting the function pointers from the block device switch.

Could you please let me know what exatly happens when read/write called on socket descriptor. If read/write works on socket descriptor, we cant we use open instead of socket to get the descriptor?

like image 307
Ganesh Kundapur Avatar asked Aug 13 '10 08:08

Ganesh Kundapur


2 Answers

As i know in memory, the file descriptor will contains flag to identify the file-system type of this fd. The kernel will invoke corresponding handler function depends on the file-system type. You can see the source read_write.c in linux kernel.

To be speak in brief, the kernel did:

  1. In read-write.c, there is a file_system_wrapper function, that call corresponding handler function depends on fd's file type (ext2/ ext3/ socket/ ..)
  2. In socket.c, there is a socket_type_wrapper function; that calls corresponding socket handler function depends on socket's type (ipv4, ipv6, atm others)
  3. In socket_ipv4.c, there is a protocol_type wrapper function; that calls corresponding protocol handler function depends on protocol tpye (udp/ tcp)
  4. In tcp_ip4.c; there is tcp_sendmsg and this function would be called when write to FD of tcp ipv4 type.

Hope this clearly, thanks, Houcheng

like image 76
Houcheng Avatar answered Oct 17 '22 01:10

Houcheng


Socket descriptors are associated with file structures too, but a set of file_operations functions for that structures differs from the usual. Initialization and use of those descriptors are therefore different. Read and write part of kernel-level interface just happened to be exactly equivalent.

like image 28
Basilevs Avatar answered Oct 16 '22 23:10

Basilevs