COMP 400 Week 2

advertisement
itec 400
Unix Processes
George Vaughan
Franklin University
1
Topics
•
•
•
•
•
•
•
What is a process?
Life cycle of a process
Process States
Monitoring Processes
Signals
Managing Processes
cron Utility
2
Unix Processes
• What is a process?
– an executing program that has its own
address space.
• Each process has its own unique ID
(called the process id).
• Every process (with very few exceptions)
are created by a parent process
3
User Processes
• When you log into a Unix/Linux system, a
shell process is started on your behalf.
• When you execute a command (like ‘ps’),
another process is created for that
command:
>ps
PID TTY
12325 pts/1
12367 pts/1
TIME CMD
00:00:00 bash
00:00:00 ps
4
User Processes
• Assume you write a one-line script named ‘test01’:
sleep 10
#my one-line script
• When you run ‘test01’, 2 processes are created:
– a new shell process is created which executes the script
– a process is created to run sleep. Sleep runs within the context of the
new shell - not the parent shell (login shell).
– when your script ends, the script shell terminates
– this means the parent shell (the login shell) won’t be affected by the
script
• Example (using 2 telnet sessions):
PID TTY
1434 pts/0
12594 pts/2
12663 pts/2
12664 pts/2
12665 pts/0
TIME CMD
00:00:00 bash
00:00:00 bash
00:00:00 bash
00:00:00 sleep
00:00:00 ps
<= Login shell from telnet #1
<= Login shell from telnet #2
<= Script shell from telnet #2
<= sleep from script on telnet #2
<= ps on telnet #1
5
What is a process?
• Book’s definition:
“A process is a single executable program that
is running in its own address space.”
• Note that a command may spawn other
processes.
• All user processes are descendents of init.
• Typical of a single user to have more than
one active process.
6
What is a process?
• Almost all system processes are
descendents of init.
• There are a few processes that are
created ‘by hand’ during system startup.
• In Solaris hand crafted processes are:
– scheduler
– init
– pager
– fsflush
7
What is a process?
• The kernel keeps track of process state
information, such as:
– address space map
– current state of process
– priority
– process owner
– list of open files
– etc.
8
Life cycle of a process
• All processes are created from another
process (i.e. all processes have a parent,
except init).
• Parent process creates a copy of itself
using fork() - see manpage
• In child copy, fork returns 0.
• In parent copy, fork returns PID of child
• This is how parent and child can tell who is
who.
9
Example ex1000.c
0001 #include <stdio.h>
0002 #include <sys/types.h>
0003 #include <unistd.h>
0004
0005 main()
0006 {
0007 printf ("PARENT: process ID is: %d, BEFORE fork()\n",
getpid());
0008 pid_t procID = fork();
0009 if (procID != 0) { /* Am I the PARENT process? */
0010
printf ("PARENT: process ID is: %d. ", getpid());
0011
printf ("Sleep 30 sec...\n");
0012
sleep(30);
0013
printf("PARENT: process just woke up\n");
0014 }
0015 else { /* I must be the CHILD process */
0016
printf (" CHILD: process ID is: %d. ", getpid());
0017
printf ("Sleep 60 sec...\n");
0018
sleep(60);
0019
printf(" CHILD: process just woke up\n");
0020 }
0021 printf ("Process: %d has just terminated\n", getpid());
0022 }
•
•
•
•
•
Line 8: process forks into 2 processes
Line 9: Test to see if we are the parent process
Lines 10-13: This code is executed only by parent
Lines 16-19: This code is executed only by child
Line 21: This line is executed by both the parent
and the child.
•
OUTPUT (telnet session 1):
>ex1000
PARENT: process ID is: 14028, BEFORE fork()
CHILD: process ID is: 14029. Sleep 60 sec...
PARENT: process ID is: 14028. Sleep 30 sec...
PARENT: process just woke up
Process: 14028 has just terminated
CHILD: process just woke up
Process: 14029 has just terminated
•
OUTPUT (telnet session 2);
>ps -u
PID
13295
6704
14032
14029
14028
13281
14001
6687
vaughang
TTY
pts/40
pts/6
pts/40
pts/6
pts/6
pts/9
pts/4
pts/4
TIME
0:00
0:00
0:00
0:00
0:00
0:00
0:00
0:00
CMD
ksh
ksh
ps
ex1000
ex1000
ksh
vi
ksh
 Child Process
 Parent Process
10
Life cycle of a process
• The child process may then overwrite its own
address space with the desired program using
exec() - see manpage.
• Although child runs new program, it does so
within the inherited environment of the parent.
• The child process will continue to run until it exits
or is terminated. exit() - see man page
• When a process ends, it sends a signal to parent
process.
11
ex1010.c and ex1020.c
0001 #include <stdio.h>
0002 #include <sys/types.h>
0003 #include <unistd.h>
0004
0005 main()
0006 {
0007 printf ("ex1010 PARENT: process ID is: %d,
BEFORE fork()\n", getpid());
0008 pid_t procID = fork();
0009 if (procID != 0) { /* Am I the PARENT process? */
0010
printf ("ex1010 PARENT: process ID is: %d\n",
getpid());
0011 }
0012 else { /* I must be the CHILD process */
0013
printf ("ex1010 CHILD: process ID is: %d\n",
getpid());
0014
execl("ex1020", (char *) 0);
0015 }
0016 printf ("ex1010 Process: %d has just
terminated\n", getpid());
0017 }
-----------------------------------------------------------------0001 #include <stdio.h>
0002 #include <unistd.h>
0003
0004 main() {
0005 printf("ex1020 process ID is: %d\n", getpid());
0006 }
ex1010:
•
Line 8: fork() occurs here
•
Line 9: Check to see if I am parent or child
•
Line 8-10: These lines are only executed by
parent
•
Line 12: process must be child
•
Lines13-15: These lines are only executes
by child
•
Line 14: Child executes “execl()” which will
now execute ex1020
ex1020:
•
Line 5: print out that this process is now
executing 1020
•
OUTPUT:
>ex1010
ex1010 PARENT: process ID is: 14410, BEFORE fork()
ex1010 CHILD: process ID is: 14411
ex1010 PARENT: process ID is: 14410
ex1010 Process: 14410 has just terminated
ex1020 process ID is: 14411
How come we don’t see?:
ex1010 Process: 14411 has just terminated
12
Process States
Fork
Exiting
Ready
exit
Scheduled
Parent
Process
Run-able
Running
Deleted
Paused
Waiting on
Resource
Resource
Available
See last slide for References
Blocked
(Sleeping)
13
Process States
• Fork Event:
– A Process makes an exact copy of itself
– New process inherits environment from parent
– Child process has new PID
– Child process has unique address space
– The child overwrites address space with
image of desired program. See exec().
14
Process States
• Run-able State:
– This state represents a queue
– All processes that are run-able are on the
queue.
– Many processes can be in the run-able state.
– Run-able means that the process is not
waiting for any resources
– The process is just waiting its turn to become
the “running process”.
15
Process States
• “Running” State
– Process is executing in CPU
– Only ‘n’ processes can be running on system with ‘n’
CPU's.
– Process will remain in this state until:
• Process exceeds CPU time-slice
– if so, process moves to run-able state
• Process needs resource to continue (I/O)
– if so, process is moved to blocked state
• Process is pre-empted by a higher priority process.
– if so, process moves to run-able state.
16
Process States
• Blocked “Sleeping” State:
– A process moves to this state when it is
waiting for a resource
– Many processes maybe in this state (queue).
– Maybe waiting on I/O, page fault, etc.
17
Process States
• Exiting State:
– A process moves into the ‘Exiting’ state when exit() is
invoked.
– Parent process is notified and parent acknowledges
termination of child (see wait()).
– Abnormal situations:
• If a parent refuses to acknowledge termination of child
process, the child process becomes a zombie process.
• If parent process already terminated, grand-parent process
adopts child process and acknowledges termination (Unix).
• If parent no longer exists, kernel makes child a process of init
- init then adopts and acknowledges termination of child
process (Linux).
18
Monitoring Processes
• uptime
>uptime
11:08pm up 16 day(s), 14:49, 58 users, load average: 3.46, 3.07, 1.77
– fields
•
•
•
•
current time
elapsed time the system has been up
current number of users
average number of job in the run-able state over
the last 1, 5 and 15 minutes
19
Monitoring Processes
• ps - list active processes (Solaris [bsd
version])
>/usr/ucb/ps aux | more
USER
PID
%CPU %MEM
rhodes06 25983
27.2 36.3
rhodes06 25980
27.0 33.7
rhodes06 25982
8.6 0.1
rhodes06 25981
8.4 0.1
rhodes06 25979
8.4 0.1
rhodes06 25984
7.8 0.1
•
SZ
52776
52764
1448
1448
1448
1448
RSS
TT
8365008?
8338936?
904
?
904
?
896
?
728
?
S
R
R
S
O
S
S
START
TIME
23:00:47 14:16
23:00:41 14:27
23:00:47 4:47
23:00:41 4:51
23:00:41 4:32
23:00:47 4:55
COMMAND
a.out
a.out
a.out
a.out
a.out
a.out
Fields:
–
–
–
–
–
–
–
–
USER: logname
PID: Process ID
%CPU, %MEM: percentage of cpu and memory used by processes
SZ: Total virtual memory size in pages
RSS: Resident Set Size (size resident in memory) in bytes
TT: Associated TTY line
S: process status (R=runnable, S=sleeping, O=active, Z=zombie, T=stopped)
TIME: cumulative cpu time
20
Monitoring Processes
• top - show top consuming processes
(Solaris)
>top | more
last pid: 29652; load averages: 3.82, 3.73, 3.60
23:38:00
261 processes: 249 sleeping, 2 running, 8 stopped, 2 on cpu
Memory: 1024M real, 15M free, 2934M swap in use, 341M swap free
PID USERNAME THR PRI NICE SIZE
25983
1961
1 10
0 1027M
25980
1961
1 11
0 1027M
25984
1961
1 32
0 1448K
25982
1961
1 32
0 1448K
25979
1961
1 33
0 1448K
•
RES
83M
78M
720K
896K
888K
STATE
run
cpu/0
sleep
sleep
run
TIME
CPU COMMAND
18:15 28.89% a.out
19:52 23.51% a.out
6:06 8.71% a.out
5:59 8.47% a.out
6:16 7.89% a.out
Fields
–
–
–
–
PID, USERNAME, SIZE, RES, STATE, TIME, CPU, COMMAND: nothing new
THR: # of executing threads in the process
PRI: prioirity
NICE: nice value
21
Monitoring Processes
• pstree - shows parent-child process
relationships
$ pstree
init-+-atd
|-cannaserver
|-cpumemusage_app
|-crond
|-cserver
|-deskguide_apple
|-dhcpcd
|-gconfd-1
|-gdm---gdm-+-X
|
`-gnome-session
|-gnome-name-serv
|-gnome-smproxy
|-gnome-terminal-+-2*[bash]
|
|-bash---pstree
|
`-gnome-pty-helpe
22
Signals
• Process level interrupt
• Can be used for:
– inter-process commutation
– user at terminal can send signals to kill, interrupt,
suspend processes with special key strokes
– signals can be sent with kill command
– kernel can send signals on abnormal events (division
by zero, invalid address, etc.)
• More than 30 types of signals…
23
• Types of signals:
Signals
#
Name
Description
Default
Catch?
Block?
Core
Dump?
Key
Strokes
1
HUP
Hang Up
Terminate
Y
Y
N
2
INT
Interrupt
Terminate
Y
Y
N
3
QUIT
Quit
Terminate
Y
Y
Y
9
KILL
Kill
Terminate
N
N
N
-
BUS
Bus Error
Terminate
Y
Y
Y
11
SEGV
Segmentation
Fault
Terminate
Y
Y
Y
15
TERM
Software
Termination
Terminate
Y
Y
N
Cntrl-C
-
STOP
Stop
Stop
N
N
N
Cntrl-Z
-
CONT
Continue
Ignore
Y
N
N
Cntrl-Z
24
Managing Processes
• kill - kill a process:
– user can kill process owned by user
– root can kill any process
– must specify PID
– can specify signal level (e.g. kill -9 12345)
– signal 15 is the default
– see man page for more details…
25
Managing Processes
• Foreground process
– process currently associated with keyboard.
interactive process
• Background process
– a process that was kicked off with ‘&’ at end of
command line
– can have many background processes
– to see active background processes, type: jobs
– to bring background job to forground, type: fg %n
(where n is the background job number - not PID).
26
Managing Processes
0001 >cat bigJob
0002 while : ;
0003 do
0004 echo bigJob $1 is alive
0005 sleep 5
0006 done
0007 >bigJob John &
0008 [1] 5823
0009 >bigJob John is alive
0010
0011 bigJob John is alive
0012 bigJob Alice &
0013 bigJob John is alive
0014 [2] 5830
0015 bigJob Alice is alive
0016 bigJob John is alive
0017 bigJob Alice is alive
0018 bigJob John is alive
0019 bigJob Alice is alive
0020 jobs
0021
0022 [2] + Running
bigJob Alice &
0023 [1] - Running
bigJob John &
0024 bigJob John is alive
0025 bigJob Alice is alive
0026 bigJob John is alive
0027 bigJob Alice is alive
0028 kill %1
0029 [1] - Terminated
bigJob John &
0030 bigJob Alice is alive
0031 jobs
0032 [2] + Running
bigJob Alice &
0033 bigJob Alice is alive
0034 fg %2
0035 bigJob Alice
0036 bigJob Alice is alive
0037 bigJob Alice is alive
0038 ^D
27
Managing Processes
• nohup
– “no hang-up”
– a mechanism for running a program, even if
you want to logout.
– Usually used for large programs
– Example:
nohup bigJob &
– Automatically invokes “nice”
28
Managing Processes
• nice
– A method of running a command that may be
cpu intensive that will be friendly to other
processes.
– Affects (lowers) process priority.
– Can be used with foreground and background
jobs
– nohup automatically invokes nice.
29
Managing Processes
• at
– a method for specifying a command or script
to be executed at a specific time.
– Used for “one-shot” scheduling.
– For repetitive tasks, use crontab.
– example:
at 4am < myFile
30
cron Utility
• A Unix facility to schedule repeating tasks
(scripts)
• cron command starts crond deamon.
• crond reads crontab file.
• A unique crontab file is created on a per
user basis.
31
cron Utility
• Format of crontab entry
minutes hours day-of-month month weekday command
minutes
Minutes past the hour (0-59)
hours
Hour of day (0-23)
Day-of-month 1-31
month
1-12
weekday
0-6
command
Output mailed if not
redirected.
32
cron Utility
• Create file with entries
• Use crontab to install entries
• Use crontab -l to check entries
0001: $ cat cronfile
0002: 0,5,10,15,20,25,30,35,40,45,50,55 * * * * date >> ~/trash/output
0003: $ crontab cronfile
0004: $ crontab -l
0005: # DO NOT EDIT THIS FILE - edit the master and reinstall.
0006: # (cronfile installed on Tue Feb 25 21:52:24 2003)
0007: # (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
0008: 0,5,10,15,20,25,30,35,40,45,50,55 * * * * date >> ~/trash/output
33
cron Utility
• check output for progress
• remove crontab entries
• check removal with crontab -l
0012: $ cat output
0013: Tue Feb 25 21:55:00 EST 2003
0014: Tue Feb 25 22:00:00 EST 2003
0015: Tue Feb 25 22:05:01 EST 2003
0016: Tue Feb 25 22:10:00 EST 2003
0017: $ crontab -r
0018: $ crontab -l
0019: no crontab for gvaughan
34
cron Utility
• Look at cron log (must be root)
0021: tail /var/log/cron
0022:
0023: Feb 25 22:15:00 localhost CROND[3002]: (mailman) CMD
(/usr/bin/python -S /var/mailman/cron/gate_news)
0024: Feb 25 22:15:00 localhost CROND[3003]: (gvaughan) CMD (date >>
~/trash/output)
0025: Feb 25 22:15:41 localhost crontab[3013]: (gvaughan) DELETE
(gvaughan)
0026: Feb 25 22:15:46 localhost crontab[3014]: (gvaughan) LIST (gvaughan)
0027: Feb 25 22:16:01 localhost CROND[3020]: (mailman) CMD
(/usr/bin/python -S /var/mailman/cron/qrunner)
35
References
• http://rabbit.eng.miami.edu/class/een521/proces
sstates.html
• http://jan.netcomp.monash.edu.au/OS/l8_2.html
• Essential System Administration, Aeleen Frisch,
2002
• Linux Administration Handbook, Evi Nemeth, et.
al., 2002
• The UNIX Programming Environment Kernighan and Pike, 1984
• The Design of the Unix Operating System Maurice J. Bach, 1986
36
Download