File Descriptor

A unique File Descriptor is assigned to an opened file in order for Unix or Unix-like operating system such as #Linux to identify it. There are some default file descriptors reserved for system usage:

  • 0 is used by standard input
  • 1 is used for standard output
  • 2 is used for standard error
Links to this page
  • Unix System Call
    close (close File Descriptor#)
  • TTP3121 Chapter 3: File Manipulation
  • TTP3121 Chapter 2: Introduction to Unix Based OS
  • Socket Programming

    Additionally, we could alter the socket’s default option for more convenient set up using the system call setsockopt(). The parameters that should be passed are the socket File Descriptor, the level (who in the system to interpret this option), the option name, the option’s value, and the value’s length. The usage is shown below:

    We can get the socket’s name (server or client) with the functions getsocketname() and getpeername() by passing socket File Descriptor, socket address, and its length. The former returns the local associated address (server) and the latter returns the foreign associated address (client).

  • POSIX Mandatory Lock

    POSIX Mandatory Lock is a lock checked by Unix-like operating system to verify that the operation such as read and write does not interfere with a lock held by a process. It could be done by lockf() #Unix System Call (imported from unistd.h) which requires File Descriptor#, function used, and the length of the file. The locking starts from the current byte position of the file, and if the length is positive, then it will extend forward, otherwise it will extend backward. If length is 0, then the entire file is locked.

  • POSIX Advisory Lock

    POSIX Advisory Lock is a lock that doesn’t necessary forbid other process from writting the file that is being locked. It is especially useful when working with cooperating processes. It could be done with flock() Unix System Call#, imported from sys/file.h, with File Descriptor# and specified operation as parameters.

  • Linux
  • I/O Multiplexing

    select() system call, receiving either three file descriptor sets (described below) including read, write and exceptional file descriptor sets, allows the user process to instruct the Kernel to wait for either reading, writing, or exceptional I/O events to happen and to wake up the process only when one of these events occur. The first argument for select() should be the highest-numbered file descriptor (numfds) to be tested in any of passed three sets plus 1. With this system call, we can monitor several sockets (up until the limit numfds) via their File Descriptor# at the same time to see which one is ready for reading, which one is ready for writing etc. in a non-blocking manner. Meaning, the process will only be notified when the data is readily available for reading from either socket. An example usage of select() is shown below:

#unix #operating-system #linux