Reference: Molay, Understanding Unix/Linux Programming, Chapter 5
Similarities between files and devices
- Devices have file names
- Files that represent devices are located in the /dev directory.
$ ls -C /dev | head -5
autofs loop-control sda7 tty19 tty39 tty59 vcsa
bsg mapper sda8 tty2 tty4 tty6 vcsa1
core mqueue sda9 tty20 tty40 tty60 vcsa2
cpu net sh tty21 tty41 tty61 vcsa3
cpu_dma_latency null stderr tty22 tty42 tty62 vcsa4
- Devices support system calls
- Devices support file-related system calls such as open, read, write, lseek, close, and stat. Unix provides no other means to communicate with devices.
- Some devices can be read-only or write-only and may not support all system calls. Terminals support read and write, but not lseek
- Properties of device files
- Device files have most of the properties that disk files have. Device files represent connections, not data containers. The inode of a device file stores a pointer to a device driver, a subroutine in the kernel that gets data into and out of a device.
$ stat /dev/tty
File: "/dev/tty"
Size: 0
FileType: Character Device
Mode: (0666/crw-rw-rw-)
Uid: ( 0/ root)
Gid: ( 0/ wheel)
Device: 42,7610600 Inode: 303 Links: 1
Access: Wed Oct 29 11:29:57 2014
Modify: Mon Oct 20 21:03:14 2014
Change: Mon Oct 20 21:03:14 2014
Differences between files and devices
- File connection attributes
- Disk buffering and auto-append mode can be controlled when the file connection is created using the open system call or using the fcntl system call at a later moment.
- int fcntl(int fd, int cmd [, long arg | struct *flock *lockp ]);
- Controls the attributes of a file descriptor fd. Operation cmd is performed with parameters arg.
- Device connection attributes
- Connection to a terminal has attributes setting the echo, baud rate, editing, newline conversion parameters.