13 process management

10
Process Management

description

Unix / Linux Fundamentals

Transcript of 13 process management

Page 1: 13 process management

Process Management

Page 2: 13 process management

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

Page 3: 13 process management

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.

Page 4: 13 process management

Processes and PIDs

• ‘ps’ can also print out resource usage information for all processes on the system. Example

$ ps aux | headUSER 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

Page 5: 13 process management

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.

Page 6: 13 process management

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

Page 7: 13 process management

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

Page 8: 13 process management

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,command59,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.

Page 9: 13 process management

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).

Page 10: 13 process management

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