• Open Control Blocks

    The open control block (OCB) contains active information about the open resource.

    For example, the filesystem keeps the current seek point within the file here. Each open() creates a new OCB. Therefore, if a process opens the same file twice, any calls to lseek() using one FD will not affect the seek point of the other FD. The same is true for different processes opening the same file.

    The following diagram shows two processes, in which one opens the same file twice, and the other opens it once. There are no shared FDs.

    Figure showing two processes opening the same file.

    FDs are a process resource, not a thread resource.

    Several file descriptors in one or more processes can refer to the same OCB. This is accomplished by two means:

    • A process may use the dup() , dup2() , or fcntl() functions to create a duplicate file descriptor that refers to the same OCB.
    • When a new process is created via vfork() , fork() , posix_spawn() , or spawn() , all open file descriptors are by default inherited by the new process; these inherited descriptors refer to the same OCBs as the corresponding file descriptors in the parent process.

    When several FDs refer to the same OCB, then any change in the state of the OCB is immediately seen by all processes that have file descriptors linked to the same OCB.

    For example, if one process uses the lseek() function to change the position of the seek point, then reading or writing takes place from the new position no matter which linked file descriptor is used.

    The following diagram shows two processes in which one opens a file twice, then does a dup() to get a third FD. The process then creates a child that inherits all open files.

    Figure showing a process opening a file twice.You can prevent a file descriptor from being inherited when you posix_spawn(), spawn(), or exec*() by calling the fcntl() function and setting the FD_CLOEXEC flag.

One Responseso far.

  1. Mr WordPress says:

    Hi, this is a comment.
    To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.