Creating and removing files

This section describes the internal procedures performed by the operating system when creating, opening, closing, or removing files.

Creating a file

Different subroutines create specific types of files, as follows:

Subroutine Type of File Created
creat Regular
open Regular (when the O_CREAT flag is set)
mknod Regular, first-in-first-out (FIFO), or special
mkfifo Named pipe (FIFO)
pipe Unnamed pipe
socket Sockets
mkdir Directories
symlink Symbolic link

Creating a regular file (creat, open, or mknod subroutines)

You use the creat subroutine to create a file according to the values set in the Pathname and Mode parameters. If the file named in the Pathname parameter exists and the process has write permission to the file, the creat subroutine truncates the file. Truncation releases all data blocks and sets the file size to 0. You can also create new, regular files using the open subroutine with the O_CREAT flag.

Files created with the creat, mkfifo, or mknod subroutine take the access permissions set in the Mode parameter. Regular files created with the open subroutine take their access modes from the O_CREAT flag Mode parameter. The umask subroutine sets a file-mode creation mask (set of access modes) for new files created by processes and returns the previous value of the mask.

The permission bits on a newly created file are a result of the reverse of the umask bits ANDed with the file-creation mode bits set by the creating process. When a new file is created by a process, the operating system performs the following actions:

  • Determines the permissions of the creating process
  • Retrieves the appropriate umask value
  • Reverses the umask value
  • Uses the AND operation to combine the permissions of the creating process with the reverse of the umask value

Creating a special file (mknod or mkfifo subroutine)

You can use the mknod and mkfifo subroutines to create new special files. The mknod subroutine handles named pipes (FIFO), ordinary, and device files. It creates an i-node for a file identical to that created by the creat subroutine. When you use the mknod subroutine, the file-type field is set to indicate the type of file being created. If the file is a block or character-type device file, the names of the major and minor devices are written into the i-node.

The mkfifo subroutine is an interface for the mknod subroutine and is used to create named pipes.

Opening a file

The open subroutine is the first step required for a process to access an existing file. The open subroutine returns a file descriptor. Reading, writing, seeking, duplicating, setting I/O parameters, determining file status, and closing the file all use the file descriptor returned by the open call. The open subroutine creates entries for a file in the file descriptor table when assigning file descriptors.

The open subroutine does the following:

  • Checks for appropriate permissions that allow the process access to the file.
  • Assigns a entry in the file descriptor table for the open file. The open subroutine sets the initial read/write byte offset to 0, the beginning of the file.

The ioctl or ioctlx subroutines perform control operations on opened special device files.

Closing a file

When a process no longer needs access to the open file, the close subroutine removes the entry for the file from the table. If more than one file descriptor references the file table entry for the file, the reference count for the file is decreased by 1, and the close completes. If a file has only 1 reference to it, the file table entry is freed. Attempts by the process to use the disconnected file descriptor result in errors until another open subroutine reassigns a value for that file descriptor value. When a process exits, the kernel examines its active user file descriptors and internally closes each one. This action ensures that all files close before the process ends.

Removing a file

When a file is no longer needed, you can use the unlink subroutine to remove the specified file from the directory containing it. If there are multiple hard links to the same file, the unlink subroutine removes the specified link. If there is only one link, the unlink subroutine removes the file itself. For more information, see the unlink subroutine.