Linux File System
- Kernel Space v. User Space
- File system is a shared name space useful for
- Accessing shared system resources such as devices and shared memory
- Performing inter-process communication (named pipes)
- A file is an interface that exports:
- open: convert public name space to local
name, established "proper" access to the file
- proper does not necessarily mean
exclusive, just that read, write operations perform properly
- read: returns bytes from the file/device
(advances file position)
- write: appends bytes to the file/device (at current
position)
- close: relinquish access to the device (invalidates
the local file descriptor)
- lseek: move the position in the file
- The system opens three files for each user process at load time: stdin,
stdout, stder. By default these files are the user's console.
Example: cat
cat does this (not showing
alot of stuff, like error checking, etc).
main(argc, argv) {
while
(1) {
s = read(stdin, buf, 1);
s = write(stdout, buf, 1);
}
'cat < /dev/console >/dev/console' is equivalent to 'cat'
both will simply echo what you type. /dev/console is special file in the
file system that has a "device driver" associated with it. When you
write to this device, you are really calling a function in the device driver
package. In this case, the device driver, running in kernel space, can send
characters to the user's display and get them from the keyboard.
Example
cat < dog > eat
- in this case, the system opens disc files 'dog' and 'eat' instead of
'/dev/console' for standard input and standard output. Of course, cat needs
to check for end of file to terminate. You never get and end-of-file from a
terminal or console device.
Example
cat < dog | cat | cat | cat
- for each pipe '|' the system opens a temporary file, say /tmp/pipe. The
system opens this file as stdin for the user process on the left, and as
stdout for the user process on the right. Eventually the contents of
the file 'dog' will appear on the user's console as standard output of the
last 'cat'.
Device Drivers
- To create a device driver you must:
- Create a special file on the disc and tell the OS which device driver
should be used to access it. .
- Write the device driver, which is really just a collection of
functions that are allowed to run in Kernel Space so that direct access
to the HW resources is possible. The key point is that you must define
open, read, write, close, etc. so that a user can treat your device like
a file. Not all operations must be defined for all devices. Some devices
are "write" only and some are "read" only. In this
case, open should fail (return -1) in the case that the user opens the
device in an unsupported mode.
- Install the device driver. This can be done either when you compile
the operating system or you can install a device driver when the OS is
already running (in Linux).
Example
With the proper device driver, you should be able to send
music to your 466 format player by using the following command:
'cat song.466 > /dev/player466'
In class activity
write psuedo-code for a sonar device driver: The sonar device has one input,
INIT and one output ECHO. The device performs a single measurement when INIT is
asserted by the user. The time between the user's assertion of INIT and the
devices assertion of ECHO corresponds distance. The challenge:
Define the functions: open, read, write, close so that the
device will look like a device.
You must
Example