CIT 140: Introduction to IT Slide #1
CSC 140: Introduction to IT
Processes
CIT 140: Introduction to IT Slide #2
Topics
1. What is a process?
2. Process states and multitasking
3. How does the shell execute processes?
4. Foreground and background processes
5. Job control
6. Process tree
7. Process commands
CIT 140: Introduction to IT Slide #3
Introduction
A process is a program in execution.
A process is created every time you run an external command and is removed after the command finishes its execution.
CIT 140: Introduction to IT Slide #4
Multitasking
Only one process is using a CPU at a time.
A process uses the CPU until:1. It makes an I/O request, or
2. It reaches the end of its time slice, or
3. It is terminated.
The technique used to choose the process that gets to use the CPU next is called Process scheduling.
CIT 140: Introduction to IT Slide #5
Process State
As a process executes, it changes statenew: The process is being created.
running: Instructions are being executed.
waiting: The process is waiting for some event to occur.
ready: The process is waiting to be assigned to a CPU.
terminated: The process has finished execution.
zombie: Process has been terminated and is waiting for its resources to be recovered.
CIT 140: Introduction to IT Slide #6
Process States
A UNIX process can be in one of many states, typically ready, running, or waiting.
CIT 140: Introduction to IT Slide #7
Process Scheduling
First come, First serve(FCFS) – Process that enters system first is assigned highest
priority.
Priority Scheduling– Assign priorities based on accumulated CPU time.– New and I/O bound processes have highest priorities.
Round Robin (RR)– A process gets to use the CPU for one time slice, then
the CPU is given to another process, the next process in the queue of processes waiting to use the CPU.
CIT 140: Introduction to IT Slide #8
Process Scheduling
Modern UNIX scheduler uses mixture of strategies.– Priority values constantly recalculcated.– Smallest priority numbers have highest priority.
Priority value = Threshold priority + Nice value + (Recent CPU usage/2)– Threshold priority is an integer from 40 or 60.– CPU usage is the number of clock ticks for which the
process has used the CPU– Nice value is a positive integer with a default of 20.
CIT 140: Introduction to IT Slide #9
Execution of shell Commands
A shell command can be external or internal.
Internal (built-in) command: is part of the shell:ex: bg, cd, continue, echo, exec
External command: separate program or scriptex: grep, more, cat, mkdir, rmdir, ls
CIT 140: Introduction to IT Slide #10
Execution of shell CommandsA UNIX process is created by the fork() system call,
which creates an exact copy of the original process.
The forking processis known as the parent process.
The created (forked) process is called the child process.
CIT 140: Introduction to IT Slide #11
Execution of shell CommandsTo execute an external command, the exec() system call is used after fork.exec() replaces the current process in memory with the specified file and begins running it.
CIT 140: Introduction to IT Slide #12
Execution of shell Commands
CIT 140: Introduction to IT Slide #13
Execution of Shell ScriptsShell script: a series of shell commands in a file
Execution different from binary programs.– Current shell creates a child shell and lets the child
shell execute commands in the shell script, one by one.– Child shell creates a child process for every command
it executes.– While the child shell is executing commands in the
script file, the parent shell waits for the child to terminate, after which it comes out of waiting state and resumes execution.
– Only purpose of child shell is to execute commands and eof means no more commands.
CIT 140: Introduction to IT Slide #14
Execution of shell Commands
CIT 140: Introduction to IT Slide #15
Execution of shell Commands% psPID TTY TIME CMD15672 pts/0 :03 -csh% sh$ echo This is Bourne shell.This is Bourne shell.$ ksh$ echo This is Korn shell.This is Korn shell.$ psPID TTY TIME CMD15672 pts/0 0:03 -csh15772 pts/0 0:00 sh15773 pts/0 0:00 ksh$ ^D$ ^D%
CIT 140: Introduction to IT Slide #16
Execution of shell Commands (contd.)
CIT 140: Introduction to IT Slide #17
Process AttributesProcesses have a variety of attributes:
1. User owner2. Group owner3. Process name4. Process ID (PID)5. Parent process ID (PPID)6. Process state7. Length of time running8. Amount of memory used
CIT 140: Introduction to IT Slide #18
Process Statusps [options] (System V version)
Purpose Report process statusOutput Attributes of process running on the system
Commonly used options/features:
-a Display Information about the processes executing on your terminal except the session header (your login shell)
-e Display information about all the processes running on the system
-f Display full list (8 columns) of status report.-l Display long list (14 columns) of status report.-u uidlist Display information about processes belonging to the
users with UIDs in the ‘uidlist’ (UIDs separated by commas)
CIT 140: Introduction to IT Slide #19
Examples of ps> ps PID TTY TIME CMD 24621 pts/13 0:00 bash 24740 pts/13 0:00 ps> ps -f UID PID PPID C STIME TTY TIME CMD waldenj 24621 24615 0 14:51:55 pts/13 0:00 -bash waldenj 24745 24621 0 14:55:06 pts/13 0:00 /usr/bin/ps -f> ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 8 S 1249 24621 24615 0 50 20 ? 331 ? pts/13 0:00 bash 8 O 1249 24746 24621 0 50 20 ? 123 pts/13 0:00 ps> ps -ef|head UID PID PPID C STIME TTY TIME CMD root 0 0 0 Jun 16 ? 0:01 sched root 1 0 0 Jun 16 ? 1:39 /etc/init - root 2 0 0 Jun 16 ? 0:01 pageout root 3 0 1 Jun 16 ? 970:56 fsflush root 332 1 0 Jun 16 ? 0:00 /usr/lib/saf/sac -t 300 root 312 1 0 Jun 16 ? 0:00 /usr/lib/snmp/snmpdx -y -c /etc/snmp/conf root 325 1 0 Jun 16 ? 0:05 /usr/sbin/vold root 50 1 0 Jun 16 ? 0:00 /usr/lib/sysevent/syseventd root 60 1 0 Jun 16 ? 0:01 /usr/lib/picl/picld
CIT 140: Introduction to IT Slide #20
Top
Top shows process activity in real time.By default, top shows
– Summary of CPU/memory usage.– Highest 15 CPU using processes.– Updated every 5 seconds.
Top commandsh Helpm Show top processes by memory
usageq Quit
CIT 140: Introduction to IT Slide #21
Example top Outputload averages: 0.12, 0.13, 0.14 zappa 14:57:00102 processes: 99 sleeping, 2 stopped, 1 on cpuCPU states: 83.3% idle, 0.6% user, 16.1% kernel, 0.0% iowait, 0.0% swapMemory: 512M real, 253M free, 238M swap in use, 1.1G swap free
PID USERNAME THR PR NCE SIZE RES STATE TIME FLTS CPU COMMAND 24793 waldenj 1 49 0 1896K 1152K cpu00 0:00 0 0.34% top 20561 mysql 9 59 0 40.3M 5272K sleep 133:28 0 0.07% mysqld 24621 waldenj 1 59 0 2648K 2056K sleep 0:00 0 0.05% bash 232 root 1 59 0 4416K 1504K sleep 7:14 0 0.02% sendmail 349 root 7 59 0 2872K 1728K sleep 47:20 0 0.00% mibiisa 191 root 13 59 0 3488K 1616K sleep 9:07 0 0.00% syslogd 343 root 1 59 0 13.9M 4784K sleep 7:48 0 0.00% Xsun 361 root 1 59 0 7904K 2896K sleep 2:24 0 0.00% dtgreet 209 root 23 59 0 3696K 2400K sleep 1:47 0 0.00% nscd 1 root 1 59 0 1224K 248K sleep 1:38 0 0.00% init 5069 nobody 1 59 0 20.9M 14.0M sleep 0:56 0 0.00% httpd 5675 glasnova 1 59 0 960K 560K sleep 0:49 0 0.00% generate 5683 glasnova 1 59 0 960K 560K sleep 0:48 0 0.00% generate 348 root 1 59 0 2400K 912K sleep 0:44 0 0.00% fbconsole 13323 nobody 1 59 0 20.9M 12.1M sleep 0:35 0 0.00% httpd
CIT 140: Introduction to IT Slide #22
Process and Job controlShell manages your processes for you:
– Process creation– Process termination– Suspending processes– Running foreground processes.– Running background processes. – Switching processes to foreground/background.
CIT 140: Introduction to IT Slide #23
Foreground and Background
Default is to run processes in foreground> find / -name foo –print >foo.out 2>/dev/null
> bzip2 foo.out
User must wait for foreground process to finish to have shell prompt.
Run processes in background with &> find / -name foo –print >foo.out 2>/dev/null &
> bzip2 foo.out &
CIT 140: Introduction to IT Slide #24
Example of Background Processes> find / -name foo 2>/dev/null &[1] 25299> bzip2 bash.man &[2] 25304> ps PID TTY TIME CMD 24621 pts/13 0:00 bash 25299 pts/13 0:03 find 25305 pts/13 0:00 ps>[2]+ Done bzip2 bash.man
CIT 140: Introduction to IT Slide #25
Suspending a Process
What if you forgot to background a process?– Suspend the process with <Ctrl-z>– Use fg or bg to unsuspend process.
Use stop to suspend background processes.– If you don’t have a stop command, use
alias stop=“kill –STOP”– Example
CIT 140: Introduction to IT Slide #26
Managing Background Processes: fg
fg[%jobid]Purpose: Resume execution of the process with job
number ‘jobid’ in the foreground or move background processes to the foreground.
Commonly used values for ‘%jobid’% or %+ Curent job%- Previous job%N Job Number N%Name Job beginning with ‘Name’%?Name Command containing ‘Name’
CIT 140: Introduction to IT Slide #27
Managing Background Processes: bg
bg[%jobid-list]Purpose: Resume execution of suspended processes/jobs with job numbers in ‘jobid-list’ in the background.Commonly used values for ‘%jobid’:
% or %+ Curent job%- Previous job%N Job Number N%Name Job beginning with ‘Name’%?Name Command containing ‘Name’
CIT 140: Introduction to IT Slide #28
Managing Background Processes: jobs
jobs [option] [%jobid-list]Purpose: Display the status of the suspended and background processes specified in ‘jobid-list’; with no list, display the status of the current job.Commonly used options/features:
-l Also display PID of jobs
> find / -name foo 2>/dev/null &
[1] 26629
> bzip2 bigFile &
[2] 26630
> jobs
[1]- Running find / -name foo 2>/dev/null &
[2]+ Running bzip2 bigFile &
> fg %2
>
CIT 140: Introduction to IT Slide #29
Which job is the current job?
The current job is either1) The most recently stopped job, or
2) If no stopped jobs exist, the most recent background job.
The following fg will return you to vim, not sleep.> vim smallFile
Ctrl-z
[2]+ Stopped vim smallFile
> sleep 10000 &
[3] 28137
> fg
CIT 140: Introduction to IT Slide #30
UNIX Daemons
A daemon is a system process running in the background.
Most system services are daemons:
Printing
Ssh
Web
CIT 140: Introduction to IT Slide #31
Sequential and Parallel Execution
cmd1; cmd2; … ; cmdN
Purpose: Execute the ‘cmd1’, ‘cmd2’, ‘cmd3’,…,’cmdN’ commands in sequence.
cmd1 & cmd2 & … & cmdN &
Purpose: Execute commands ‘cmd1’,cmd2’,…’cmdN’ in parallel as separate processes.
CIT 140: Introduction to IT Slide #32
Sequential and Parallel Execution> date; bzip2 -v bigFile; date
Sat Nov 5 15:28:19 EST 2005
bigFile: 11.350:1, 0.705 bits/byte, 91.19% saved, 2940850 in, 259096 out.
Sat Nov 5 15:28:28 EST 2005
> date & bzip2 -v bigFile & echo Hello, World & uname -a & date
[1] 25658
Sat Nov 5 15:30:29 EST 2005
[2] 25659
Hello, World
bigFile: [3] 25660
SunOS zappa 5.9 Generic_112233-07 sun4u sparc SUNW,Ultra-250
[4] 25661
Sat Nov 5 15:30:29 EST 2005
[1] Done date
[3] Done echo Hello, World
[4] Done uname -a
> 11.350:1, 0.705 bits/byte, 91.19% saved, 2940850 in, 259096 out.
[2]+ Done bzip2 -v bigFile
CIT 140: Introduction to IT Slide #33
Combining Sequential and Parallel
CIT 140: Introduction to IT Slide #34
Subshells
Shell scripts run in a subshell.
You can start a subshell without a script– Group commands using ()’s– Use ; to separate sequence of commands.
Advantages of subshells– Redirect I/O of a group of commands at once.– Put group of commands in background at once.– Manage group of commands as a single
background process.
CIT 140: Introduction to IT Slide #35
Subshells> (date; ps -ef; ls -l) | bzip2 -c >subshell.bz2> bzcat subshell.bz2 |headSat Nov 5 15:33:15 EST 2005 UID PID PPID C STIME TTY TIME CMD root 0 0 0 Jun 16 ? 0:01 sched root 1 0 0 Jun 16 ? 1:39 /etc/init - root 2 0 0 Jun 16 ? 0:01 pageout root 3 0 1 Jun 16 ? 971:09 fsflush root 332 1 0 Jun 16 ? 0:00 /usr/lib/saf/sac -t 300 root 312 1 0 Jun 16 ? 0:00 /usr/lib/snmp/snmpdx -y -c /etc/snmp/conf root 325 1 0 Jun 16 ? 0:05 /usr/sbin/vold root 50 1 0 Jun 16 ? 0:00 /usr/lib/sysevent/syseventd
CIT 140: Introduction to IT Slide #36
Subshells> (date; bzip2 -v bigFile; date) >bzPerf.txt 2>&1 &[1] 25858> ps PID TTY TIME CMD 24621 pts/13 0:00 bash 25858 pts/13 0:00 bash 25861 pts/13 0:00 ps 25860 pts/13 0:02 bzip2> fg( date; bzip2 -v bigFile; date ) >bzPerf.txt 2>&1^Z[1]+ Stopped ( date; bzip2 -v bigFile; date ) >bzPerf.txt 2>&1> bg[1]+ ( date; bzip2 -v bigFile; date ) >bzPerf.txt 2>&1 &> whonewell pts/2 Nov 1 13:09 (10.19.39.154)walden pts/13 Nov 5 14:51 (10.19.10.199)[1]+ Done ( date; bzip2 -v bigFile; date ) >bzPerf.txt 2>&1
CIT 140: Introduction to IT Slide #37
Terminating a ProcessCan terminate a foreground process by <Ctrl-C>
Can terminate a background process:
1) Find the PID with ps, then use kill PID.
2) Bring process into foreground with fg, then use <Ctrl-C>
CIT 140: Introduction to IT Slide #38
Terminating a Process> find / -name foo 2>/dev/null >foo.txt &[4] 28344> sort bigFile | bzip2 -c >sortFile.bz2 &[5] 28350> jobs[2]+ Stopped vim smallFile[3] Running sleep 10000 &[4] Running find / -name foo 2>/dev/null >foo.txt &[5]- Running sort bigFile | bzip2 -c >sortFile.bz2 &> kill %5[5]- Terminated sort bigFile | bzip2 -c >sortFile.bz2> ps PID TT S TIME COMMAND 28022 pts/4 S 0:00 -bash 28132 pts/4 T 0:00 vim smallFile 28137 pts/4 S 0:00 sleep 10000 28344 pts/4 R 0:05 find / -name foo> kill 28344> jobs[2]+ Stopped vim smallFile[3] Running sleep 10000 &[4]- Terminated find / -name foo 2>/dev/null >foo.txt> ps PID TT S TIME COMMAND 28022 pts/4 S 0:00 -bash 28132 pts/4 T 0:00 vim smallFile 28137 pts/4 S 0:00 sleep 10000
CIT 140: Introduction to IT Slide #39
Job Numbers vs PIDs
PIDs– Every process has a PID.– PIDs are global (shared by every user.)
Job Numbers– Every job has a job number.– Jobs may contain multiple processes.– Job numbers are local (every user has own %1...)
CIT 140: Introduction to IT Slide #40
Changing Process Priorities> nice -10 bzip2 biggerFile &[4] 28713> /usr/bin/ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 8 R 1249 28713 28022 21 99 30 ? 1054 pts/4 0:05 bzip2 8 O 1249 28719 28022 0 50 20 ? 123 pts/4 0:00 ps8 T 1249 28132 28022 0 40 20 ? 1115 pts/4 0:00 vim 8 S 1249 28022 28020 0 50 20 ? 334 ? pts/4 0:01 bash> bzip2 biggerFile &[4] 28643> renice +10 28643> /usr/bin/ps -l F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD8 O 1249 28656 28022 0 50 20 ? 123 pts/4 0:00 ps 8 T 1249 28132 28022 0 40 20 ? 1115 pts/4 0:00 vim 8 S 1249 28022 28020 0 50 20 ? 334 ? pts/4 0:01 bash 8 R 1249 28643 28022 68 99 30 ? 1054 pts/4 0:24 bzip2> renice -10 28643renice: 28643: Cannot lower nice value.
CIT 140: Introduction to IT Slide #41
UNIX Process Hierarchy
1. System starts init process on boot.– Init is PID 1.
2. Init starts system processes– System daemons.– Graphical login: xdm, kdm, gdm– Text login: getty -> login– Network login: sshd
3. Login runs– User shell
CIT 140: Introduction to IT Slide #42
Process Hierarchy in UNIX