UNIX Lecture Notes – Chapter 02

Categories:

Recommended

2.1 Introduction

This chapter introduces the two primary methods of I/O possible in a UNIX: buffered and unbuffered. By trying to write the who and cp commands, we will learn explore how to create, open, read, write, and close arbitrary les. “Arbitrary” in this context means that they are not necessarily text files. We will write several different versions of the who command, simply to illustrate different approaches to the problem of reading from a file. They will differ in their performance characteristics and their portability. The chapter uses this exercise to introduce the UNIX concept of time, and the first of several important databases provided by the kernel, as well as the kernel’s interface to those databases. We also write two different versions of a simplified cp command, one using read() and write(), and the other using memory-mapped I/O.

2.2 Commands Are (Usually) Programs

In UNIX, most commands are programs, almost always written in C. Some commands are not programs; they are built into the shell and therefore are called shell builtins. Exactly which commands are builtins varies from one shell to another1, but there are some that are common to almost all shells, such as cd and exit. When you type cd, for example, the shell does not run the cd program; it jumps to the internal code that implements the cd command itself. You can think of the shell as containing a C switch statement inside a loop. When it sees that the command is a built in, it jumps to the code to execute it. Some commands, such as pwd, are both shell builtins and programs. By default the shell built in will be executed if the user types pwd; to get the program version, one can either precede the command with a backslash “\”, as in \pwd, or type the full path name, /bin/pwd.

Command programs are located in one of several directories, the most common being /bin, /usr/bin, and /usr/local/bin. The /usr/local/bin directory is traditionally used as a repository for commands that do not come with the UNIX distribution and have been added as local extras. Many packages that are installed after the operating system installation are placed in subdirectories of /usr/local. Administrative commands, such as those for creating and modifying user accounts, are found in /usr/sbin. Many UNIX systems still retain the old /usr/ucb directory. (The “ucb” in /usr/ucb stands for the University of California at Berkeley. The /usr/ucb directory, if it exists, contains commands that are part of the BSD distributions. Some of the commands in /usr/ucb are also in /usr/bin and have different semantics. If the same command exists in both /usr/bin and in some other directory such as /usr/ucb, the PATH environment variable just like the one used in Windows and DOS, determines which command will be run. The PATH variable contains a list of the directories to search when the command is typed without a leading path. Whichever directory is earliest in the list is the one whose version of the command is used. Thus, if more exists in both /usr/ucb and /usr/bin, as well as in your working directory, and /usr/bin precedes /usr/ucb which precedes . in your PATH variable, and if you type

$ more myfile

then /usr/bin/more will run. If instead you type

$ ./more myfile

then your PATH is not searched and your private more program will run. If you type

$ /usr/ucb/more myfile

then your PATH is not searched and /usr/ucb/more will run.

Attribution

Stewart Weiss (2019), UNIX Application and System Programming, URL: http://www.compsci.hunter.cuny.edu/~sweiss/course_materials/unix_lecture_notes.php

This work is licensed under Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) :  (https://creativecommons.org/licenses/by-sa/4.0/).

VP Flipbook Maker

Unlock your creativity with Visual Paradigm Online Flipbook Maker and transform your content into captivating flipbooks! With easy-to-use features and intuitive design tools, you can create interactive publications that are sure to impress. Try it now!