Previous | Table of Contents | Next |
By David B. Horvath, CCP
In this chapter you learn the concepts of processes and jobs. In UNIX every program runs as a process. I explain background and foreground processes and how to start a process. You are introduced to the commands that list and kill processes. Finally, the concept of parent and child processes are explained.
In this chapter you look at the four major topics involving processes provided with the shell:
Whenever you issue a command in UNIX, it creates, or starts, a new process. When you tried out the ls command to list directory contents in Chapter 4, Working with Directories, you started a process (the ls command).
The operating system tracks processes through a five digit ID number known as the pid or process ID. Each process in the system has a unique pid. Pids eventually repeat because all the possible numbers are used up and the next pid rolls or starts over. At any one time, no two processes with the same pid exist in the system because it is the pid that UNIX uses to track each process. You might be interested in the fact that the pid usually rolls over at the 16-bit signed boundary. The highest it gets before rolling over is 32,767.
You can use the ps command to see what processes you are running and all processes on the system. The ps command is described in the Listing Running Processes section of this chapter.
When you start a process (run a command), there are two ways you can run itin the foreground or background. The difference is how the process interacts with you at the terminal.
By default, every process that you start runs in the foreground. It gets its input from the keyboard and sends its output to the screen. You can redirect process input and output (see Chapter 13, Input/Output), but by default, input and output are connected to your terminal.
You can see this happen with the ls command. If I want to list all the files in my current directory (various chapters that start with a zero), I can use the following command:
$ ls ch0*.doc
On my screen, I see the following output:
ch01-1.doc ch010.doc ch02.doc ch03-2.doc ch04-1.doc ⇒ch040.doc ch05.doc ch06-2.doc ch01-2.doc ch02-1.doc ch020.doc ch03.doc ch04-2.doc ⇒ch05-1.doc ch050.doc ch06.doc ch01.doc ch02-2.doc ch03-1.doc ch030.doc ch04.doc ⇒ch05-2.doc ch06-1.doc ch060.doc
The process runs in the foreground, the output is directed to my screen, and if the ls command wants any input (which it does not), it waits for it from the keyboard.
While this command is running, I cannot run any other commands (start any other processes). I can enter commands, but no prompt appears and nothing happens until this one completes because UNIX buffers keystrokes. For the ls command, which usually runs very quickly, this is not a problem. But if I have something that runs for a long timesuch as a large compile, database query, program that calculated pi, or a servermy terminal will be tied up.
Fortunately, I do not have to wait for one process to complete before I can start another. UNIX provides facilities for starting processes in the background, suspending foreground processes, and moving processes between the foreground and background.
Caution:
When you log off or are disconnected from the system by a communication problem, your processes are terminated. If you have a long running process that you do not want terminated, you need to use the nohup command. nohup stands for no HUP (Hang UP). The nohup command is described later, in the section Keeping Background Processes Around (nohup Command).
Previous | Table of Contents | Next |