A device special file corresponts to a device on the system.
A device driver is a unit of kernel code that implements a set of operations that correspond to input and output actions on an associated device.
Devices can be divided in 2 types:
Character devices - data on character-by-character basis. Terminals and keyboards.
Block devices - data as block at a time. Disks and tape drives.
File System (disks and partitions)
File System (structure)
A file system is an organized collection of regular files and directories. Some examples of filesystem are:
ext2, ext3, ext4, brtfs
Microsoft's FAT, FAT32, NTFS
Apple's HFS
ext2
File System (File descriptors)
When openning files for write, you'll create or discard previous contents. The system requires to do some validations to verify that you have the proper rigthts to do so.
A File descriptor is a non-negative integer that is used as a reference to the file you're working with.
All information about the file is maintained by the operating system. User will only access it through the file descriptor.
Input / Output
All system calls for performing I/O refer to open files using a file descriptor, a (usually
small) nonnegative integer.
The following are the key required system calls for I/O operations.
Same open(), read(), write() and close() are used to perform I/O in all types of files, including devices.
$ ./copy test test.old # Copy a regular file
$ ./copy a.txt /dev/tty # Copy a regular file to this terminal
$ ./copy /dev/tty b.txt # Copy input from this terminal to a regular file
$ ./copy /dev/pts/16 /dev/tty # Copy input from another terminal
System Calls
The UNIX-based operating systems provide their services through a set of system calls,
which will be function within the Operating System where the programs are running.
A program is a file containing a range of information that describes how to contruct a process at run time. Information includes:
- Machine language instructions
- Programing entry-point address
- Data
- Symbol and relocation tables
- Shared-library and dynamic-linking information
- Other information
Typical Program's memory layout
Processes in Linux
htop
Errors Handling
The manual page for each system call documents the possible return values of the call, showing which value(s) indicate an error.
fd = open(pathname, flags, mode); /* system call to open a file */
if (fd == -1) {
/* Code to handle the error */
}
...
if (close(fd) == -1) {
/* Code to handle the error */
}