command in the POSIX:XSI Extension.

Team-FLY

3.2 Process State

The state of a process indicates its status at a particular time. Most operating systems allow some form of the states listed in Table 3.1. A state diagram is a graphical representation of the allowed states of a process and the allowed transitions between states. Figure 3.1 shows such a diagram. The nodes of the graph in the diagram represent the possible states, and the edges represent possible transitions. A directed arc from state A to state B means that a process can go directly from state A to state B . The labels on the arcs specify the conditions that cause the transitions between states to occur.

Figure 3.1. State diagram for a simple operating system.

graphics/03fig01.gif

While a program is undergoing the transformation into an active process, it is said to be in the new state. When the transformation completes, the operating system puts the process in a queue of processes that are ready to run. The process is then in the ready or runnable state. Eventually the component of the operating system called the process scheduler selects a process to run. The process is in the running state when it is actually executing on the CPU.

Table 3.1. Common process states.

state

meaning

new

being created

running

instructions are being executed

blocked

waiting for an event such as I/O

ready

waiting to be assigned to a processor

done

finished

A process in the blocked state is waiting for an event and is not eligible to be picked for execution. A process can voluntarily move to the blocked state by executing a call such as sleep . More commonly, a process moves to the blocked state when it performs an I/O request. As explained in Section 1.2, input and output can be thousands of times slower than ordinary instructions. A process performs I/O by requesting the service through a library function that is sometimes called a system call . During the execution of a system call, the operating system regains control of the processor and can move the process to the blocked state until the operation completes.

A context switch is the act of removing one process from the running state and replacing it with another. The process context is the information that the operating systems needs about the process and its environment to restart it after a context switch. Clearly, the executable code, stack, registers and program counter are part of the context, as is the memory used for static and dynamic variables . To be able to transparently restart a process, the operating system also keeps track of the process state, the status of program I/O, user and process identification, privileges, scheduling parameters, accounting information and memory management information. If a process is waiting for an event or has caught a signal, that information is also part of the context. The context also contains information about other resources such as locks held by the process.

The ps utility displays information about processes. By default, ps displays information about processes associated with the user. The -a option displays information for processes associated with terminals. The -A option displays information for all processes. The -o option specifies the format of the output.

  SYNOPSIS  ps [-aA] [-G grouplist] [-o format]...[-p proclist]      [-t termlist] [-U userlist]  POSIX Shells and Utilities  
Example 3.3

The following is sample output from the ps -a command.

 >% ps -a   PID TTY      TIME CMD 20825 pts/11   0:00 pine 20205 pts/11   0:01 bash 20258 pts/16   0:01 telnet 20829 pts/2    0:00 ps 20728 pts/4    0:00 pine 19086 pts/12   0:00 vi 

The POSIX:XSI Extension provides additional arguments for the ps command. Among the most useful are the full ( -f ) and the long ( -l ) options. Table 3.2 lists the fields that are printed for each option. An (all) in the option column means that the field appears in all forms of ps .

Example 3.4

The execution of the ps -la command on the same system as for Example 3.3 produced the following output.

 F S  UID   PID  PPID C PRI NI ADDR  SZ WCHAN TTY    TIME CMD 8 S 4228 20825 20205 0  40 20    ? 859     ? pts/11 0:00 pine 8 S 4228 20205 19974 0  40 20    ? 321     ? pts/11 0:01 bash 8 S 2852 20258 20248 0  40 20    ? 328     ? pts/16 0:01 telnet 8 O  512 20838 18178 0  50 20    ? 134       pts/2  0:00 ps 8 S 3060 20728 20719 0  40 20    ? 845     ? pts/4  0:00 pine 8 S 1614 19086 18875 0  40 20    ? 236     ? pts/12 0:00 vi 
Table 3.2. Fields reported for various options of the ps command in the POSIX:XSI Extension.

header

option

meaning

F

-l

flags (octal and additive) associated with the process

S

-l

process state

UID

-f , -l

user ID of the process owner

PID

(all)

process ID

PPID

-f , -l

parent process ID

C

-f , -l

processor utilization used for scheduling

PRI

-l

process priority

NI

-l

nice value

ADDR

-l

process memory address

SZ

-l

size in blocks of the process image

WCHAN

-l

event on which the process is waiting

TTY

(all)

controlling terminal

TIME

(all)

cumulative execution time

CMD

(all)

command name (arguments with -f option)

Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net