home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Learning the Korn Shell

Learning the Korn ShellSearch this book
Previous: 7.3 Command-line Processing Chapter 8 Next: 8.2 Job Control

8. Process Handling

The UNIX operating system built its reputation on a small number of concepts, all of which are simple yet powerful. We've seen most of them by now: standard input/output, pipes, text-filtering utilities, the tree-structured file system, and so on. UNIX also gained notoriety as the first small-computer operating system to give each user control over more than one process. We call this capability user-controlled multitasking .

If UNIX is the only operating system that you're familiar with, you might be surprised to learn that several other major operating systems have been sadly lacking in this area. For example, Microsoft's MS-DOS, for IBM PC compatibles, has no multitasking at all, let alone user-controlled multitasking. IBM's own VM/CMS system for large mainframes handles multiple users but gives them only one process each. DEC's VAX/VMS has user-controlled multitasking, but it is limited and difficult to use. The latest generation of small-computer operating systems, such as Apple's Macintosh OS System 7, IBM's OS/2 Version 2, and Microsoft's Windows NT, finally include user-controlled multitasking at the operating system level. [1]

[1] Programs like Apple's Multifinder and Microsoft Windows work on top of the operating system (Mac OS Version 6 and MS-DOS, respectively) to give the user limited multitasking.

But if you've gotten this far in this book, you probably don't think that multitasking is a big deal. You're probably used to the idea of running a process in the background by putting an ampersand ( & ) at the end of the command line. You have also seen the idea of a subshell in Chapter 4, Basic Shell Programming when we showed how shell scripts run.

In this chapter, we will cover most of the Korn shell's features that relate to multitasking and process handling in general. We say "most" because some of these features are, like the file descriptors we saw in the previous chapter, of interest only to low-level systems programmers.

We'll start out by looking at certain important primitives for identifying processes and for controlling them during login sessions and within shell scripts. Then we will move out to a higher-level perspective, looking at ways to get processes to communicate with each other. The Korn shell's coroutine facility is the most sophisticated interprocess communication scheme that we'll examine; we'll also look in more detail at concepts we've already seen, like pipes and subshells.

Don't worry about getting bogged down in low-level technical details about UNIX. We will provide only the technical information that is necessary to explain higher-level features, plus a few other tidbits designed to pique your curiosity. If you are interested in finding out more about these areas, refer to your UNIX Programmer's Manual or a book on UNIX internals that pertains to your version of UNIX.

We strongly recommend that you try out the examples in this chapter. The behavior of code that involves multiple processes is not as easy to understand on paper as most of the other examples in this book.

8.1 Process IDs and Job Numbers

UNIX gives all processes numbers, called process IDs , when they are created. You will notice that, when you run a command in the background by appending & to it, the shell responds with a line that looks like this:

fred &
[1]     2349

In this example, 2349 is the process ID for the fred process. The [1] is a job number assigned by the shell (not the operating system). What's the difference? Job numbers refer to background processes that are currently running under your shell, while process IDs refer to all processes currently running on the entire system, for all users. The term job basically refers to a command line that was invoked from your login shell.

If you start up additional background jobs while the first one is still running, the shell will number them 2, 3, etc. For example:

bob &
[2]     2367
dave &
[3]     2382

Clearly, 1, 2, and 3 are easier to remember than 2349, 2367, and 2382!

The shell includes job numbers in messages it prints when a background job completes, like this:

[1] +  Done                     fred &

We'll explain what the plus sign means soon. If the job exits with non-zero status (see Chapter 5, Flow Control ), the shell will include the exit status in parentheses:

[1] +  Done(1)                  fred &

The shell prints other types of messages when certain abnormal things happen to background jobs; we'll see these later in this chapter.

Previous: 7.3 Command-line Processing Learning the Korn Shell Next: 8.2 Job Control
7.3 Command-line Processing Book Index 8.2 Job Control

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System