next up previous

Controlling Processes

This contains links to information used in the lectures.

The entity which executes programs is called a process. Resources such as memory, CPU, and i/o devices are allocated to processes. In Unix, the SysAdm can monitor the status of processes, control how much CPU time they get, send signals to them, and suspend or halt them.

Components of a Process

Life Cycle of a Process

Unix creates new process by spawing a copy of an existing process using fork and then substituting the text of another program using exec. In UNIX, all processes other than some the kernel creates, are children spawned by init, PID 1.
Init plays another important role. Exiting processes call _exit and return an exit status. This is stored by the kernel until requested by the process's parent (using the wait system call). The address space of the process is released and it uses no further CPU time. However it still retains its identity (PID). This uses a slot in the process table but no other resources.
Problems arise if

Signals

Process States

In Unix systems there are at least 5 execution states, although more may exist on some systems:

When runnable processes make system calls (such as requesting i/o), which cannot be completed immediately, they are put to sleep.

Interactive processes and daemons spend most time sleeping waiting for input or connections. They are not elegible to be selected by the CPU scheduler, until unblocked by a signal. Most sleeps last for tens of milliseconds. A process sleeping longer is presumed interactive, and get to run immediately it is unblocked. This biases the scheduling in favor of interactive programs and makes them more responsive.

Processes enter the stopped state by receiving a STOP or TSTP signal and can only be restarted by receiving a CONT signal from another process. Most processes are stopped by one of:

The state of the process on SunOS is:
  R       Runnable processes.
  T       Stopped processes.
  P       Processes in page wait. 
  D       Processes in non-interruptible waits;
          typically  short-term  waits for disk
          or NFS I/O.
  S       Processes  sleeping  for  less   than
          about 20 seconds.
  I       Processes  that  are  idle  (sleeping
          longer than about 20 seconds).
  Z       Processes that  have  terminated  and
        that  are  waiting  for  their parent
        process to do  a  wait(2V)  ("zombie"
        processes).
and on HP-UX is:
  0    Nonexistent
  S    Sleeping
  W    Waiting
  R    Running
  I    Intermediate
  Z    Terminated
  T    Stopped
  X    Growing

Nice and Renice

The nice command executes command at a nondefault CPU scheduling priority. On BSD (SunOS) the value can be -19 to 19, and on ATT (HP-UX) 0 to 39. Lower numbers are higher priority.

All processes have an associated system nice value which is used to compute the instantaneous-priority of the process when it is scheduled to run. Normally, all processes inherit the system nice value of their parent process when they are spawned. The shell (sh, csh, ksh, etc.) can create a child process with a different priority from the current shell process by spawning the child process via the nice command. If the priority_change value is unsigned (positive), the child process is nicer (lower in priority) relative to the parent for example

nice -12 command &
runs the child process at a priority 12 worse than the default for background processes. If the priority_change value is negative, the child process runs at a higher priority with a greater share of available system resources.
nice --14 ./prog prog_args
To spawn a higher priority child process, the parent process must be owned by a user who has the appropriate privileges. Some versions on Unix nice up background jobs by default. On HP-UX ksh executes all background processes via nice -4 (the csh does not !!). BSD systems allow resetting the nice values during execution using renice. If a process goes wild and drives the load to 65, you may need to use nice to run a high priority shell to investigate the problem. On HP-UX the superuser or other authorized users can run processes with real-time priority using the rtprio executes command with a real-time priority, or changes the real-time priority of currently executing process pid. Real-time priorities range from zero (highest) to 127 (lowest). Real-time processes are not subject to priority degradation, and are all of greater (scheduling) importance than non-real-time processes.

Monitoring Processes

ps

The main tool for monitoring processes is ps. On BSD good overwviews can be had with ps -aux and ps -axl, which is also faster to run. ps is generally expensive to run. On ATT systems ps -ef and ps -elf are the equivalents.

top

top displays the top processes on the system and periodically updates the information (by default every 10 seconds). Raw CPU percentage is used to rank the processes. It comes with HP-UX 10.XX and is also freely available. Examples:

NOHUP: Protect background processes

Primitive shells such as sh send signals to all descendants, including background processes, when terminated. To prevent the background process from exiting use nohup:
nohup command &
This also increases niceness by 5. Output is put in nohup.out if stdout and stderr are not redirected.

Errant Processes



next up previous


Paul A. Farrell
Thu Feb 1 04:34:17 EST 1996