2. Overview
• Each task in Unix starts a process.
• Each process is assigned a unique process identification
number (PID).
• Some processes are started by the system and are used
to control activities, these are usually called daemons
• Processes started by the user from the shell inherit the
shell’s terminal, and interact through it
• Processes started in the GUI don’t usually have a
terminal associated with them
3. Processes and PIDs
• Use the ‘ps’ command to list the processes currently
running on the system.
• The command is:
ps [options]
• Commonest options are -ef: (in some UNIXs aux)
– –e Every process on the system
– –f Full listing
• Use pgrep command to search for a specific process.
This is equivalent to ps –ef | grep
• Without options, ps will just print all processes that share
the terminal with it.
4. Processes and PIDs
• ‘ps’ can also print out resource usage information for all
processes on the system.
Example
$ ps aux | head
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 10348 732 ? Ss 2010 0:04 init [3]
apache 6019 0.0 0.8 241120 9184 ? S Jun26 0:01 /usr/sbin/httpd
root 6108 0.0 0.0 5908 620 ? Ss Mar30 0:16 syslogd -m 0
5. Process Life Line
• There are three stages in the process life cycle:
– Fork – the parent process creates an exact image of
itself in the memory and gets a new PID.
– Exec – the new process rewrites its image in the
memory and executes. The parent (i.e. shell) either
waits for the process to finish (foreground) or
continues to run (background)
– Termination – the child process returns a signal to the
parent, which contains an exit code. The parent
process deletes the child from the process table.
6. Signals
• UNIX systems use signals to communicate different
scenarios between processes
• The kernel manages signal transfers
• When a process receives a signal it stops its current
activity and moves to a signal handling routine
• Different signals have different default routines, which
can be overridden by the programmer
• Among other things signals terminate, suspend and
continue process.
• From a regular user perspective, most signals are
irrelevant – they are used by the kernel
7. Signaling with kill
• Use the command kill to send a signal to a process.
$ kill [-signal] %job-id|process-id
• The name is derived from the fact the default signal kill
sends is the SIGTERM (15) signal, which by default
terminates the process
• For a foreground process, ^C achieves the same result,
but it is actually the terminal sending a SIGINT (2) to the
process
• Both these signals can be blocked by the process (the
BASH, for example, blocks them)
• The signal SIGKILL (9) cannot be blocked, so if all else
fails, this is the way to destroy a stuck process
• ^Z sends the signal SIGTSTP, while SIGSTOP
is a non blocked equivalent
8. Cron
• The cron daemon continually checks the users crontab
files and execute periodic tasks
• Edit the user’s crontab with the command crontab –e
• An editor (usually vi) is opened with the crontab. The
table has six fields:
minute,hour,day,month,weekday,command
59,23,*,*,*, echo Hello > hello.$(date +%s)
• You can list your crontab with crontab –l
• In the system level, there is an additional mechanism of
crontabs for once-a-day, once-a-week, etc.
9. Monitoring processes by top
• ‘top’ - Displays the top processes on the system and
periodically updates the information.
• The first few lines of the display show general
information about the system’s state:
– Load averages in the last 1, 5, and 15 minutes.
– Existing processes no. and the no. of processes in
each state (sleeping, waiting, running, starting,
zombie, and stopped).
– Percentage of time spent in each of the processor
states (user, nice, system, idle, interrupt and
swapper) per processor on the system.
– Average value for each of the processor states (only
on multi-processor systems).
10. Monitoring processes by top – cont.
CPU Processor number on which the process is executing (only on multi-processor systems).
TTY Terminal interface used by the process
PID Process ID number
USERNAME Name of the owner of the process
PRI Current priority of the process
NI Nice value ranging from -20 to +20
SIZE Total size of the process in kilobytes.
RES Resident size of the process in kilobytes
STATE Current state of the process. The various states are sleep, wait, run, idle, zomb, or stop
TIME Number of system and CPU seconds the process has consumed
%WCPU Weighted CPU (central processing unit) percentage
%CPU Raw CPU percentage. This field is used to sort the top processes
COMMAND Name of the command the process is currently running
Hinweis der Redaktion
Each task you perform in the Unix environment starts a process. An example of a process is using vi to edit a text file, or sending file to a printer. Each process is assigned a unique process identification number (PID), which is used by the system to identify the process.The following chapter defines useful commands to handle processes.
Example:$ ps –ef | more UID PID PPID C STIME TTY TIME CMDroot 0 0 0 Dec 19 ? 0:01 sched root 1 0 0 Dec 19 ? 0:11 /etc/init - root 2 0 0 Dec 19 ? 0:00 pageout root 3 0 0 Dec 19 ? 201:03 fsflush root 126 1 0 Dec 19 ? 0:00 /usr/lib/netsvc/yp/ypxfrd root 322 1 0 Dec 19 ? 0:00 /usr/lib/saf/sac -t 300root 104 1 0 Dec 19 ? 2:34 /usr/sbin/rpcbind root 118 116 0 Dec 19 ? 1:01 rpc.nisd_resolv -F -C 8 -p 1073741824 -t udp root 106 1 0 Dec 19 ? 0:00 /usr/sbin/keyservroot 304 1 0 Dec 19 ? 0:00 /usr/sbin/rpld -a root 116 1 0 Dec 19 ? 7:46 /usr/lib/netsvc/yp/ypserv -d root 123 1 0 Dec 19 ? 0:03 /usr/lib/netsvc/yp/ypbind root 133 1 0 Dec 19 ? 0:00 /usr/lib/netsvc/yp/rpc.ypupdated--More—
Example:$ ps –ef | more UID PID PPID C STIME TTY TIME CMDroot 0 0 0 Dec 19 ? 0:01 sched root 1 0 0 Dec 19 ? 0:11 /etc/init - root 2 0 0 Dec 19 ? 0:00 pageout root 3 0 0 Dec 19 ? 201:03 fsflush root 126 1 0 Dec 19 ? 0:00 /usr/lib/netsvc/yp/ypxfrd root 322 1 0 Dec 19 ? 0:00 /usr/lib/saf/sac -t 300root 104 1 0 Dec 19 ? 2:34 /usr/sbin/rpcbind root 118 116 0 Dec 19 ? 1:01 rpc.nisd_resolv -F -C 8 -p 1073741824 -t udp root 106 1 0 Dec 19 ? 0:00 /usr/sbin/keyservroot 304 1 0 Dec 19 ? 0:00 /usr/sbin/rpld -a root 116 1 0 Dec 19 ? 7:46 /usr/lib/netsvc/yp/ypserv -d root 123 1 0 Dec 19 ? 0:03 /usr/lib/netsvc/yp/ypbind root 133 1 0 Dec 19 ? 0:00 /usr/lib/netsvc/yp/rpc.ypupdated--More—
When a new command is issued in the shell, a new process starts. The first stage of a new process is when the parent process (the shell in our example) creates an exact copy of itself in the memory. The new process (the child process) has the exact environment as the parent process, but it has a different PID. This act of creating an exact copy of the parent process in the memory is called fork.In the next stage, the child process rewrites its image in the memory with the image of the command it should run. Once the image of the new process is in the memory – the new command starts to execute.This stage is called exec.After the command finishes, it terminates. Before the command terminates completely, its parent process should acknowledge its termination. The parent process receives from the child process the exit code (or an indication why the child was killed, if the child did not exit voluntarily) and is responsible for deleting the child from the process table.
There are 46 signals in the Unix operating systems. Each signal is associated with a number and a name. Information on the different signals can be found by using the following command:$ man –s 5 signal The kill command sends a signal to a process. By doing this the kill command enables terminating unwanted command processes. It is useful when there is a need to stop a command that takes a long time to run, or when there is a need to terminate a process that you can not quit in the normal way.In order to send a signal to a process, it’s PID has to be known. Use the ps command to get the PID. After finding the process PID, type kill followed by the PID.If you use the kill command without specifying a signal, signal 15 (SIGTERM) is sent to the process with the specified PID number. This usually causes the process to terminate.Example:$ ps -u userd1 PID TTY TIME CMD 25190 pts/0 0:02 ls 25083 pts/0 0:00 ksh$ kill 25190$ jobs[1] + Terminated ls -R / > /dev/null 2>&1 &$ Use the –u option of the ps command to find the processes that the user userd1 is running. Then the ls command is terminated with the kill command. If you need to forcibly terminate a process, you can append the –9 option to the kill command. This option is necessary for killing shells which do not respond to any other signal to terminate.Command format:$ kill –9 pid WARNING – For processes other then shell, use kill –9 command as a last resort because it is an abrupt method and does not allow for proper process termination. To abort a background job, use the specific job number as an argument to the kill command:$ jobs[1] + Running ls -R / > /dev/null 2>&1 &$$$ kill %1[1] + Terminated ls -R / > /dev/null 2>&1 &$ The pkill command works exactly like the pgrep command, except that it terminates the matching process or processes with a kill signal:$ ls -R / > /dev/null 2>&1 &[1] 25387$ $ pkill ls$ jobs[1] + Terminated ls -R / > /dev/null 2>&1 &$