The Unix Programming Environment

Brian W. Kernighan, Rob Pike

Reviewers: Arun John Kuruvilla, Srinivas Piskala Ganesh Babu

Helpful links: Course&Book

Chapter 1: UNIX for Beginners

The first chapter goes over introducing the basics of the unix system as well as giving an idea about the structure of the rest of the book. The book assumes that the reader has some familirity with using a terminal. The book also requires a copy of the Programmer's Manual. The objective of the chapter is to get the user started with his/her system as soon as possible.

The chapter is split into three sections:

Chapter 2: The File System

Everything in UNIX is a file. This is the fundamental principle dating from the very beginining of UNIX. This approach was decided as an example of the "Keep It Simple" policy. This chapter dwells into the fundamentals of files and directories, permissions, inodes, and finally on the directory hierarchy.

Files are a sequence of bytes. File types are not determined by the file system and the kernel treats every file as a sequence of bytes. The command file makes an educated guess at the type of file by using magic numbers.

Every file has has permissions, restricting who can read, write or execute the file. However, every system has a Super user, who can read, write, or execute any file on the system. A user can use a program called crypt to encrypt his/her files, which can prevent the superuser cannot modify the file.

All the admnistrative components of a file, such as the name, permissions, and modification times are stored separately as inodes. Inodes themselves are files, and keep the contents of the files separate from the administrative metadata. This increases access times, as many a times the contents of the file are not required, but only the administrative metadata.

The unix system uses a directory hierachy starting from "/". They are as follows:

Each of these directories serve a specific purpose. /bin is the direcotry which contains basic binaries such as who and ed. /dev contains device files. /etc contains device administration files, such as password and configuration files. /lib contains parts of the C compiler. /tmp contains temprory files used by different programs. /usr is the user file system.

Devices are mounted as regular files inside the /dev folder. Instead of coming up with different and special routines for each of the devices, Unix considers all devices as files. Inside the kernel, refernces to that file are converted into hadware commands according to the device. For example, when a file /dev/mnt0 is read by the operating system, the kernel converts the read command to the device specific read command and returns the required bytes. This way, every device is treated like a stream of bytes. /dev/null is a special file to which output from any program can be redirected, causing the output to be thrown away.

Chapter 3: Using the Shell

This chapter goes ahead to explain one of the most frequently used programs and one of the most important program for unix users. The shell helps in automating and carrying out a lot of tasks without resorting to programming languages such as C. The shell takes commands and runs them line by line. Other than characters, the shell recongnises meta characters, such as * . Shell commands are not limited to the ones that already exist. The command "new" command can be used to concatenate multiple commands to create new commands for specific purposes.

Just like any other programming language, the shell too has variables. Argumental variables, such as $1, are used to pass data to programs. The digit indicates the position in the argument list, with $0 being the name of the command. Shell variables can be created, accessed, and modified. Some shell variables, such as PATH and HOME hold special meaning to commands, and may cause other programs to crash if they do not contain proper values. Other that those variables, new variables can be created and used as the user desires.

Chapter 4: Filters

This chapter dives into various filters in unix to perform, input (stdin) ===> processing (pattern) ===> output (stdout) operations based on categories of data tranformation filters vs programmable filters.The chapter goes through different filters specifically grep and techniques for optimal usage by combining different filters for different tasks with some examples.

Chapter 5: Shell Programming

This chapter starts with a definition of shell being more of a programming language than a interactive command line interpreter where each statement runs as a command. The author also argues the content with a lot of shell programming techniques and examples should not be considered a manual page rather a reference or guide with examples. A good example of calendar - cal command was depicted with the steps showing how to customize a command based on your needs.

Chapter 6: Programming with Standard I/O

This chapter migrates from robust usage of existing tools to building new tools with C Programming language. When the problem at hand could not be solved with the existing tools, new programs or tools could be created from a minimal viable solution and incrementally adding features and options on top of it to make it a robust tool that others could also use. As the kernel and other native unix programs are written in C, the default choice of the language remains C. The chapter initially takes the "vis" program which displays text and non text characters, achieves a minimal program and iteratively updates version with additional features.

Chapter 7: UNIX System Calls

System calls provision a way of talking to the kernel. Specifically, lowest level of interaction with the unix operating systems kernel. All the tasks in a operating system boils down to multiple system calls sent to the kernel to request a resources or a service.

Chapter 8: Program Development

Chapter 9: Document Preparation