Page 438 - DCAP103_Principle of operating system
P. 438
Unit 14: Case Study of Linux Operating System
(1) wake up periodically, typically each 500 msec, to write back to disk very old dirty pages, Notes
or (2) are explicitly awakened by the kernel when available memory levels fall below a certain
threshold, to write back dirty pages from the page cache to disk. In laptop mode, in order to
conserve battery life, dirty pages are written to disk whenever pdflush threads wakeup. Dirty
pages may also be written out to disk on explicit requests for synchronization, via systems calls
such as sync, orfsync, fdatasync. Older Linux versions used two separate daemons: kupdate, for
old page write back, and bdflush, for page write back under low memory conditions. In the 2.4
kernel this functionality was integrated in the pdflush threads. The choice of multiple threads
was made in order to hide long disk latencies.
14.5 Input/Output in Linux
The I/O system in Linux is fairly straightforward. Basically, all I/O devices are made to look
like files and are accessed as such with the same read and write system calls that are used to
access all ordinary files. In some cases, device parameters must be set, and this is done using a
special system call. We will study these issues in the following sections.
14.5.1 Fundamental Concepts of Input/Output in Linux
Like all computers, those running Linux have I/O devices such as disks, printers, and networks
connected to them. Some way is needed to allow programs to access these devices. Although
various solutions are possible, the Linux one is to integrate the devices into the file system
as what are called special files. Each I/O device is assigned a path name, usually in /dev.
For example, a disk might be /dev/hd1, a printer might be /dev/lp, and the network might
be /dev/net. These special files can be accessed the same way as any other files. No special
commands or system calls are needed. The usual open, read, and write system calls will do just
fine. For example, the command cp file /dev/lp copies the file to printer, causing it to be printed
(assuming that the user has permission to access /dev/lp). Programs can open, read, and write
special files the same way as they do regular files. In fact, cp in the above example is not even
aware that it is printing. In this way, no special mechanism is needed for doing Special files
are divided into two categories—block and character. A block special file is one consisting of
a sequence of numbered blocks. The key property of the block special file is that each block can
be individually addressed and accessed. In other words, a program can open a block special
file and read, say, block 124 without first having to read blocks 0 to 123. Block special files are
typically used for disks.
Character special files are normally used for devices that input or output a character stream.
Keyboards, printers, networks, mice, plotters, and most other I/O devices that accept or
produce data for people use character special files. It is not possible (or even meaningful) to
seek to block 124 on a mouse. Associated with each special file is a device driver that handles
the corresponding device. Each driver has what is called a major device number that serves
to identify it. If a driver supports multiple devices, say, two disks of the same type, each disk
have a minor device number that identifies it. Together, the major and minor device numbers
uniquely specify every I/O device. In few cases, a single driver handles two closely related
devices. For example, the driver corresponding to /dev/tty controls both the keyboard and the
screen, which is often thought of as a single device, the terminal.
Although most character special files cannot be randomly accessed, they often need to be
controlled in ways that block special files do not. For example, input typed on the keyboard
and displayed on the screen. When a user makes a typing error and wants to erase the last
character typed, he presses some key. Some user prefer to use backspace, and others prefer
DEL. Similarly, to erase the entire line just typed, many conventions abound. Traditionally @
was used, but with the spread of e-mail (which uses @ within e-mail address), many systems
LOVELY PROFESSIONAL UNIVERSITY 431