Introduction
Processes How to list them How to terminate them Process priorities Scheduling jobs
Signals
Processes
Every time you run a command under UNIX it creates a process
A process is an instance of a running program
A program and process are different things. wc is a program; each time you run the program wc, that creates a new process.
A process runs until it has finished or is killed
Processes (2)
Every process has a number assigned to it called a process–ID or PID.
All the processes running on a UNIX machine are scheduled a certain amount of time on the CPU. The more that are running, the longer it will take for any one to finish
Listing Processes
ps
PID TTY TIME CMD 4073 pts/23 00:00:00 bash16007 pts/23 00:00:00 ps
ps can be given different options to, e.g. list all processes on system, show the user that owns the process, etc.
Child and Parent Processes
Any process can create another process
That process is then a parent process of the newly created child process
Use the pstree command to view relationships between parent and child processes (the process tree)
pstree[zlizmj@unnc-cslinux ~]$ pstree -p zuczpdgconfd-2(10782)
kdeinit(10733)-+-artsd(10752) |-emacs(10779)---aspell(27099) |-kdeinit(10738) |-kdeinit(10771)---bash(10783)---emacs(23700)-+-aspell(32542) | `-emacsserver(23754) |-kdeinit(10773) |-kdeinit(10774)---bash(10802) |-pam-panel-icon(10768)---pam_timestamp_c(10770) `-xload(10778)
Child and Parent Processes (2)
A parent process forks to create a child process
This allows multiprocessing:
$
$ pwd
$$ pwd &$$ ls &
$ ls
Prompt
Running Job 1
Running Job 2
Parent Process
Child Process
Child Process
Children run separately and simultaneously with each other and with their parents (although the parent might choose to wait for its child to finish)
Children inherit from their parents When a process spawns to make a
child process, the child is initially an exact copy of the parent, except for some differences (see next)
Child and Parent Processes (3)
Differences between Child and Parent Processes
They have different Process IDs (PIDs)The have different PPIDs (parent PIDs)Accounting information is reset for the
childFor details of all the differences look up
the manual page on the system call forkAll other things are initially the same, but
each process has its own copy (they can modify their own copy)
Being “nice” to other people!If you are running a program on a
shared machine and you know it will take some time - “nice” it
nice will run a program at a lower priority so that it doesn’t clog up the CPU
Priorities range from 19 to -20 -20 is the highest priority, but you are
not allowed to set priorities below 0 unless you are root
$ nice –n 20 find . -name unix.ps –print
Job Control: top
A program which shows you information about the top CPU processes
Updates this information at regular intervals
Type q to quitType k to kill a process (to kill means to
send a signal)Type u (return) followed by a username
(return) to just see the processes belonging to that user
Killing ProcessesUse topAlternatively, use kill
kill <signal number> <PID>E.g. kill -15 25718
“-15” is the signal number – here, it means “stop the process cleanly” (i.e. close any files it is using)
More about signals later… “-9” means “kill the process whatever”
Useful if all else fails!killall <signal number> <process
name> will send the signal to every process with that name.
Process States
Processes can have one of a number of states: 0 - running on a processor S - sleeping (waiting for an event to
complete) R - runnable (process is on run queue) Z – zombie
Running a Process in the Background
Some commands may take a while to complete
Some may run until the user Exits (e.g. emacs)
You may want to use your command line in the meantime: & puts a process in the background
(detaches it from the terminal) E.g. emacs &
Suspending ProcessesProcesses can be temporarily suspended
Use Ctrl-Z
To restart a process type one of: fg (puts process back in the foreground) bg (will restart process, but in the
background)
Suspended processes can also be killed: Do a process listing Use the kill command with the process ID
Job Control The jobs command produces a numbered list
of background and suspended processes You can use these job numbers to access your
jobs:
Note that the job number is not the process ID!
[zlizmj@unnc-cslinux ~]$ jobs[1] - Running xclock -d[2] + Suspended more temp.txt[zlizmj@unnc-cslinux ~]$ kill %2[zlizmj@unnc-cslinux ~]$ jobs[1] - Running xclock -d[3] Terminated more temp.txt
Job Control (2)
jobs allows you to: Bring a job to the foreground
fg %<job number> Run a job in the background
bg %<job number> Suspend a job
stop %<job number> Terminate a job
kill %<job number>
Control Key Sequences for Processes
Some control sequences affect processes: Ctrl-C - kill a process Ctrl-D - exit a shell (send EOF) Ctrl-S - suspend or pause the display of
output Ctrl-Q - resume or continue output
from Ctrl-S
Daemons
UNIX daemons are processes which lie dormant until they are needed for a particular service (services in windows-speak)
Commonly, their names end with a ‘d’Examples are:
Printer daemons (lpd) Web server daemons (httpd) Scheduling daemons (atd, crond)
Scheduling Processes - cron
Processes can be scheduled to run at a periodic intervals: Use the cron daemon With this, users can schedule
processes to run periodically, or at specified times
Create a text file called crontab.cron which contains lines with a date/time and command line
Scheduling Processes - cron (2)
Cron jobs are allowed or denied by system administrators using the cron.allow and cron.deny files in either /var/spool/cron or /etc/crond.d
You have to register your crontab using the command crontab crontab.cron in order for the cron daemon to activate your crontab
Scheduling Processes - cron (3)
Each line in crontab.cron has five fields: Minute - (0-59) Hour - (0-23) Day of the month - (1-31) Month of the year - (1-12) Day of the week - (0-6) (Sunday is 0) Command line - the command to be
executed
Using cronEdit your crontab.cron file to
contain what you want it to do:
This cron job will record the date it was run every 30 minutes from Monday to Friday, in the file datelog
Register your crontab:
$ crontab crontab.cron
0,30 * * * 1-5 date >> datelog
Scheduling Processes - at
You can schedule something to happen once using at
at TIME will execute at given TIME the commands given in STDIN.
It’s often more comfortable to useat TIME < filenameat TIME –f filename
Scheduling Processes - at (2)
$ at now + 1 min$ at> who | logged$ at> ls myDir | listing.txt$ at> <EOT>job 1171280502.a at Mon Feb 12 11:41:42 2007$ at 3am < commandsjob 8 at 2007-03-21 03:00
Scheduling Processes - batchThe batch command can be used to
queue up jobs:
These jobs will be run as soon as the system has the resources to do so
$ batchat> ls myDir > listing.txtat> <EOT>$
UNIX Signals
Signals are a UNIX mechanism for controlling processes
A signal is a message to a process that requires immediate attention
Signals are generated by exceptions, e.g.: Attempts to use illegal instructions The user pressing an interrupt key Window resize events A child process calling exit or terminating
abnormally
Signal NumbersEach signal has a default action
associated with itMost signals can be caught from within a
program. A programmer can then: Ignore signal Perform the default action Execute a program specified function
The default action can be TermTerminate the process. Ign Ignore the signal. Core Terminate the process and dump core. Stop Stop the process.
Signal NumbersSignal Name
Number Default Action
Meaning
SIGHUP 1 Term Hangup (sent to a process when a modem or network connection is lost, terminal is closed, etc)
SIGINT 2 Term Interrupt (generated by Ctrl-C)
SIGTRAP 5 Core Trace trap
SIGKILL 9 Term Kill
SIGBUS 10 Core Bus error (invalid memory reference)
SIGSEGV 11 Core Segmentation violation
SIGTERM 15 Term Software termination signal (default kill signal)
For a complete reference see the section 7 of the manual on signal
$ man 7 signal
Summary
UNIX processes How to list them How to prioritise them How to schedule them Parent and Child processes
Signals What are they for? Types of signal