[ Previous | Next | Contents | Glossary | Home | Search ]
AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs

File Creation and Removal

The internal procedures performed by the operating system when creating, opening, or closing files are described in the following sections.

Creating a File

You use different subroutines to create specific types of files. They are:

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:

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:

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

Closing a File

The open subroutine creates entries for a file in the file descriptor table when assigning file descriptors. 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 ensures that all files close before the process ends.

Related Information

Files, Directories, and File Systems for Programmers.

Linking Files and Directories.

Working with JFS i-nodes.

File Space Allocation.

Using File Descriptors.

The chdir subroutine, chroot subroutine, close subroutine, fcntl, dup, or dup2 subroutine, ioctl or ioctlx subroutine, link subroutine, open, openx, or create subroutine, mknod or mkfifo subroutine, readlink subroutine, remove subroutine, symlink subroutine, umask subroutine, unlink subroutine.


[ Previous | Next | Contents | Glossary | Home | Search ]