-->
Previous Table of Contents Next


Understanding Processes

A running program in Linux is called a process. Because Linux is a multitasking system, many processes can run at the same time. To distinguish between processes, Linux assigns each new process a unique ID called a process ID.

The process ID is simply a number that uniquely identifies each running process. To see what process IDs are now associated with your process, use the ps command. To look at most of the process IDs now running on your system, issue the command with the flags -guax, and you see something like the following:


USER  PID %CPU %MEM SIZE  RSS TTY STAT START   TIME COMMAND

jack   53  3.2  7.0  352  468 p 1 S    02:01   0:01 -bash

jack   65  0.0  3.5   80  240 p 1 R    02:01   0:00 ps -guax

root    1  0.8  3.1   44  208 con S    02:00   0:00 init

root    6  0.0  1.8   24  124 con S    02:00   0:00 bdflush (daemon)

root    7  0.0  1.9   24  128 con S    02:01   0:00 update (bdflush)

root   40  1.0  3.5   65  240 con S    02:01   0:00 /usr/sbin/syslogd

root   42  0.2  2.9   36  200 con S    02:01   0:00 /usr/sbin/klogd

root   44  0.5  3.2   68  216 con S    02:01   0:00 /usr/sbin/inetd

root   46  0.2  3.0   64  204 con S    02:01   0:00 /usr/sbin/lpd

root   52  0.1  2.0   32  140 con S    02:01   0:00 selection -t ms

root   58  0.2  2.4   37  164 p 6 S    02:01   0:00 /sbin/agetty 38400 tt

The process ID is identified by the column titled PID. Also note the boldfaced line, which indicates the first process started by the system—init. The init process is also described later in this chapter.

When Linux is told to run a program (that is, to create a process), it does so by making an exact copy of the program making the request. In the simplest case, you request that a program be run by telling your shell; the shell makes a fork request to the Linux kernel.

Fork, init, and the exec Process

A fork is the process of cloning an existing process. Linux creates all new processes through the mechanism of forking. When a process is forked, an almost exact duplicate of an existing process (including its environment and any open files) is created; what keeps the duplicate from being exactly the same as its parent application is a flag that tells the forked process which is the parent and which is the child.

Because all processes are created in this fashion, all processes have a parent process and a parent-process ID. Every process running on a Linux system can trace its lineage back to init, the mother of all processes. init itself, process ID 1, is the only process run directly by the Linux kernel that you as a user have any contact with. Every process you create during a session has your login shell as an ancestor, and your login shell has init as its parent.

After a process successfully forks, the child process calls the exec routine to transform itself into the process you requested. The only thing that changes after an exec function is the identity of the running process; the environment of the new process is an exact copy of the environment of its parent.

Standard Input and Output

Every new process is created with three open “files.” Because Linux treats files and devices exactly the same, an open “file” can be a real file on a disk or a device such as your terminal. The three open files are defined as standard input (stdin), standard output (stdout), and standard error output (stderr). All Linux commands, as well as application programs, accept input from the standard input and place any output on the standard output. Any diagnostic messages are automatically placed on the standard error output.

When you first log in, the standard input, output, and error files are attached to your terminal; any programs you run (processes you create) inherit your terminal as the three open files.

Understanding Shell Command Parsing

Parsing is the act of splitting the command line, or what you type, into its component parts for processing. In Linux, parsing constitutes a lot more than simply splitting the command line. The command string is first split into its component parts: the filenames expanded if you used any wild cards, shell variables expanded, I/O redirection set up, any command groupings or subshells set up, and command substitution performed. Only then can the command line, as you typed it, be executed.

If terms such as wild cards and I/O redirection are new to you, you can find explanations of them, in the order they’re performed, later in this chapter. You must first start, however, with the basic command syntax.

Using Commands, Flags, and Parameters

To execute a Linux command, you merely type the name of the file. The command to list files is ls; you can find a file by that name in the /bin directory. If /bin is listed in your PATH variable (and it should be), your shell finds and executes /bin/ls.

Some Linux commands aren’t independent files. These commands are built into the shells themselves. For example, the cd (change directory) command is built into most shells and executed directly by the shell without looking up a file. Read the man pages for the shell you’re using to determine what commands are executed internally or externally. Some shells have a command file that contains commands executed directly by the shell.


Previous Table of Contents Next