522 unix process(w3 1)
TRANSCRIPT
-
7/26/2019 522 UNIX Process(w3 1)
1/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 1
UNIX Process
1. Processes
2. Commans t!at eal wit! "rocesses
#. UNIX $%stem Call & usin' system(shell
command)(4. forkn exec $%stem Calls to mani"ulate %our "rocesses
5. UNIX $%stem Calls & accessin' t!e kernel
). Process I*+ Parent Process I*+ Process ,rou" I*
PI*+PPI*+,I*
-
7/26/2019 522 UNIX Process(w3 1)
2/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 2
-!e inu 0ernel
ri'inall% 1+ lines o3 C+ now a 4it4i''er+ t!e kernel controls access to t!e!arware. It allows t!e creation o3 user
"ro'rams an allocates resources ankee"s track o3 w!o is usin' w!at. -!e 3irst "ro'ram run is calle initit starts
ot!er "ro'rams+ e.' t!at monitor consoles.
console is a screen an !as ake%4oar& t!ese ma% 4e t!rou'! serial"orts or et!ernet
-
7/26/2019 522 UNIX Process(w3 1)
3/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian #
-!e 0ernel 4oot
-!e "rocesses monitorin' 6serial "orts7 willt!en run a login"ro'ram once in"ut isetecte.
Eac! user w!o 'i8es a known username an"asswor is t!en 'i8en a shellusuall% bash. 9look at t!e 3ile/etc/passwd& t!e last lines
nce in %our s!ell+ %ou can launc!"ro'rams
-
7/26/2019 522 UNIX Process(w3 1)
4/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian ;
Process Mana'ement
-o kee" track o3 all t!e "rocesses+ ta4les areuse. lso t!e CPU s!oul "ro8ie some
kin o3 !arware "rotection. E.' sper!isor
mode an ser mode. r use protectedmemory< t!e aim is t!at a user "ro'ram
can not access memor% or e8ices 6owned
4% t!e kernel+ or su"er8isor "ro'ram+sometimes calle an execti!ein ot!er
$es
-
7/26/2019 522 UNIX Process(w3 1)
5/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 5
0ernel ser8ices
Uni 4o can a""ear to 4e runnin' a lot o3"ro'rams at once. I3 re=uests come o8er t!enetwork t!en 8arious net orientate
"ro'rams can run in t!e 4ack'roun+ t!elo''e in user is not e8en aware o3 t!is.E.'. a we4ser8er 9!tt" or 3t" ser8er 93t"or a remote lo'in 3acilit% 9ss! or telnet.Note t!e >? stans 3or aemon& a4ack'roun "ro'ram
-
7/26/2019 522 UNIX Process(w3 1)
6/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian )
Processes an "ro'rams
"rocess is a "ro'ram in a state o3 eecution.
Pro'rams eist on isk or in ram 4ut onl%
w!en a "ro'ram is eecute is a "rocess iscreate. Usuall% t!ere will 4e more t!an one
"rocess "er "ro'ram+ an% in"ut or out"ut
will cause ot!er "rocesses to 4e launc!e.9Uni is a multi"ro'rammin' s%stem
-
7/26/2019 522 UNIX Process(w3 1)
7/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian @
Process mana'ement
-!e Process ta4le is mana'e 4% t!e 0ernel. It !ols one 6line7 "er "rocess( c.3 "s
-
7/26/2019 522 UNIX Process(w3 1)
8/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian B
User ata re'ion
Eac! user "ro'ram re=uires ata s"ace& a userstack an ata area 9!ea". n a coe s"ace.
"rocess ima'e is com"rise o3 t!e""#$+ t!e userstack+ user ata an coe. It 'ets swo""e to isk.
Coe can 4e rea onl%& allows more t!an one userto run it at t!e same time. Eac! user !as !is ownuser ata 4ut coe ma% 4e s!are rea onl% callepre textin Uni
ea onl% coe nee not 4e swo""e9written toisk+ since it !asn?t c!an'e. Uni !as a t!ir ta4leto kee" track o3 reaAonl% s!are coe .
-
7/26/2019 522 UNIX Process(w3 1)
9/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian D
User moe an 0ernel Moe
"rocess will eecute instructions in t!e user"ro'ram until some outsie 3acilit% is re=uire oran outsie e8ent+ suc! as a clock interru"t + occurs.-!e CPU switc!es 3rom ser mode to kernelmode.
!en t!e eecution o3 kernel instructions is
com"lete anot!er "rocess will resume. -!e kernel!as aprocess schedleto look a3ter t!is. "rocessma% 4e sus"ene or 4locke 9waitin' on aneternal e8ent suc! as isk I/ to com"lete
-
7/26/2019 522 UNIX Process(w3 1)
10/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 1
Processes
Process. unnin' "ro'ram / a""lication.
$%stem < e.'. cs!. User initiate < e.'. "ico.
Eac! uni=uel% num4ere.
Process I* pid Comman 6"s7 lists 9see man "a'es
-
7/26/2019 522 UNIX Process(w3 1)
11/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 11
Eam"le o3 %ps &el
F"s PI* --G -IME CM*
;B) tt%1 :: 4as! ;1#B tt%1 :: "s F"s
-
7/26/2019 522 UNIX Process(w3 1)
12/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 12
Commans 3or "rocess control
-r% t!e 3ollowin'& look u" t!e man "a'es
F3in / Aname A"rint
Fjo4s
F3'
F4'
F"s
-
7/26/2019 522 UNIX Process(w3 1)
13/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 1#
$%stem calls
stdlib.hinclues t!e 3unction system() Permits s%stem commans to 4e run 3rom
insie a >C? "ro'ram. Protot%"e&..
int system(char *string)
!ere 6strin'7 Uni comman s!ell scri"t
user "ro'ram
-
7/26/2019 522 UNIX Process(w3 1)
14/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 1;
$%stem call A eam"le
Callin' lswit! a "ro'ram system(ls); or
char *command = ls;system(command); or
int main(void
{
printf(Files in Directoryn);
system(ls !l);
ret"rn #;
$
-
7/26/2019 522 UNIX Process(w3 1)
15/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 15
s%stem9 A return
s%stem9 returns an inte'er
eit status o3 comman eit9(8alue
3inal return 8alue in main
-!at is w!% main returns a 8alue.
-
7/26/2019 522 UNIX Process(w3 1)
16/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 1)
estrictions
estrictions on usin' s%stem9
8aila4ilit% o3 memor% Callin' "ro'ram remains in memor%
New co"% o3 s!ell an callin' "ro'ram loae
into M.
I3 insu33icient memor% an error messa'e isis"la%e.
-
7/26/2019 522 UNIX Process(w3 1)
17/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 1@
t!er s%stem calls
eecl9+ wait9 an 3ork9 eecl < eecute an lea8e
"rocess eecute an t!en terminate -!e 6ls7 "ro'ram coul !a8e 4een writtenint main(void){
printf(Files in Directoryn);e%ecl(&bin&ls' ls' l '#);
ret"rn #;$
$ee man "a'es 3or etails.
-
7/26/2019 522 UNIX Process(w3 1)
18/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 1B
UNIX $%stem Calls
$%stem Calls
A s%stem call is a irect entr% "oint t!rou'! w!ic! an acti8e
"rocess can o4tain ser8ices 3rom t!e kernel.
A -!e s%stem calls are s"eci3ic routines in t!e o"eratin'
s%stem kernel t!at are irectl% accessi4le to a""lication
"ro'rams.
-
7/26/2019 522 UNIX Process(w3 1)
19/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 1D
Jeatures o3 UNIX $%stem Calls
Jeatures o3 $%stem Calls
A -!e s%stem calls allow %ou to "er3orm lowAle8el I/+
mani"ulate 3iles an irectories+ create an control
multi"le concurrent "rocesses+ mana'e interru"ts+ ancontrol I/ to terminals.
A Lecause UNIX is im"lemente in C+ its s%stem calls are
s"eci3ie in C s%nta an irectl% calle 3rom C
"ro'rams.
A s%stem call ma% nee one or more associate !eaer 3iles.
-!ese !eaer 3iles are clearl% inicate wit! eac! call
escri4e.
-
7/26/2019 522 UNIX Process(w3 1)
20/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 2
UNIX Process
UNIX Process
A n UNIX "rocess is an instance o3 a "ro'ram t!at is 4ein'
eecute 4% t!e o"eratin' s%stem. 9forks%stem call
A Jrom a "ro'rammin' "oint o3 8iew+ a UNIX "rocess is t!e
entit% create 4% t!eforks%stem call.
-
7/26/2019 522 UNIX Process(w3 1)
21/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 21
$tates o3 t!e Process 91
unnin'-!e "rocess is eecutin'.
slee"
"rocess in t!is state is waitin' 3or an e8ent to occur. $uc!
an e8ent coul 4e an I/ com"letion 4% a "eri"!eral e8ice+t!e termination o3 anot!er "rocess+ t!e a8aila4ilit% o3 ate or
s"ace in a 4u33er+ t!e 3reein' o3 a s%stem resource+ an so on.
!en a runnn' "rocess !as to wait 3or suc! an e8ent it is
4locke an 'oes to slee". -!is creates an o""ortunit% 3or a
contet switc!+ s!i3tin' t!e CPU to anot!er "rocess. ater+
w!en t!e e8ent t!at a slee"in' "rocess is waitin' 3or occurs+
it awakens an 4ecomes rea% to run.
-
7/26/2019 522 UNIX Process(w3 1)
22/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 22
$tates o3 t!e Process 92
ea%
"rocess in t!is state is t!en sc!eule 3or CPU ser8ice.
Oom4ie3ter termination o3 eecution+ a "rocess 'oes into t!e
om4ie state. -!e "rocess no lon'er eits. -!e ata structure
le3t 4e!in contains its eit status an an% timin' statistics
collecte. -!is is alwa%s t!e last state o3 a "rocess.
-
7/26/2019 522 UNIX Process(w3 1)
23/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 2#
Creatin' a Process
1. ProcessIn Uni+ w!en an eecuta4le "ro'ram is rea into s%stem
memor% 4% t!e kernel an eecute+ it 4ecome a "rocess.
2.fork$%stem Call
it! t!e ece"tion o3 some initial "rocess 'enerate 4% t!ekernel urin' 4ootstra""in' 9e.'. swa""er+ init an
"a'eaemon+ all "rocesses in a Uni "ro'rammin'
en8ironment are create 4% aforks%stem call.
#."arent"rocess an *hild "rocess -!e initiatin' "rocess in terme t!eparent"rocess.
-!e newl% 'enerate "rocess t!e child"rocess.
-
7/26/2019 522 UNIX Process(w3 1)
24/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 2;
fork$%stem Call
1. $%nta o3fork$%stem CallInclue Jile9s: Qsys/types.hR
Qunistd.hR
$ummar%:pid_t fork (void)(
eturn $uccess : in c!il+ c!il PI* in "arent
Jailure: A1
$et errno: %es
2. Junction o3fork$%stem Call
!en a 3ork s%stem call is mae+ t!e o"eratin' s%stem
'enerates a co"% o3 t!e "arent "rocess w!ic! 4ecomes t!e
c!il "rocess.
-
7/26/2019 522 UNIX Process(w3 1)
25/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 25
Parent /Child Process Relationship
0ernel
swa""er PI* init PI* 1 "a'eaemon PI* 2
0ernel Process Parent 0ernel Process
Parent/c!il Parent/C!il
C!il C!il C!il
-
7/26/2019 522 UNIX Process(w3 1)
26/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 2)
Child Process Unique Information
1.-!e o"eratin' s%stem will "ass to t!e c!il "rocess most o3t!e "arent?s s%stem in3ormation 9e.'. o"en 3ile escri"tors+
en8ironment in3ormation+ etc.
2. Uni=ue In3ormation to t!e C!il Process
S -!e c!il !as its own "rocess I* 9PI*.S -!e c!il will !a8e a i33erent "arent "rocess I* 9PPI* t!an
its "arent.
S $%stem im"ose "rocess limits 9e.'.+ amount o3 CPU time t!e
"rocess is allotte are reset to ero.S ll recor locks on 3iles are reset.
S -!e action to 4e taken w!en recei8in' si'nals is i33erent.
-
7/26/2019 522 UNIX Process(w3 1)
27/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 2@
eneratin! Child Process(ian".cc)
Parent:
T3ork9( C!il/Parent
"rint396elloVn7( T"rint396elloVn7(
3ork9( 3ork9(Print396L%eVn7(W Print396L%eVn7(W
C!il C!il
Print396L%eVn7( Print396L%eVn7(
-
7/26/2019 522 UNIX Process(w3 1)
28/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 2B
Process I*
Process I*ssociate wit! eac! "rocess is a uni=ue "ositi8e inte'er
ienti3ication num4er calle a "rocess I* 9PI*.
Initial Processes 'enerate 4% t!e 0ernel
A Process 9swapper"rocess in L$*A4ase UNIX+ sched"rocess in $olaris: res"onsi4le 3or "rocess sc!eulin'.
A Process 1 9init"rocess : 4asic res"onsi4ilit% is t!e
mana'in' o3 terminal line an w!o is t!e "arent "rocess
o3 all ot!er UNIX "rocesses.A Process 2 9pagedaemon"rocess in L$*A4ase UNIX+
pageot"rocess in $olaris: res"onsi4le 3or "a'in'.
9L$*: Lerkele% $o3tware *istri4ution
-
7/26/2019 522 UNIX Process(w3 1)
29/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian 2D
getpids%stem Call
getpid $%stem CallJunction: to o4tain t!e "rocess I*
Inclue Jile9s: Qs%s/t%"es.!R
Qunist.!R
$ummar%: "it 'et"i98oi(eturn: $uccessA t!e "rocess I*
JailureA A1
$ets errnoA%es
Eam"le o3getpid
printf (+y "'# is ,d -n getpid( ) )
-
7/26/2019 522 UNIX Process(w3 1)
30/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian #
Parent Process I*
Parent Process I*-!e "arent "rocess is t!e "rocess t!at 3orke t!e c!il
"rocess. ssociate wit! eac! "arent "rocess is a uni=ue
"ositi8e inte'er ienti3ication num4er calle a "arent "rocess
I* 9PPI*.
$a8e t!e eturn C!il Process I*
-!ere is no s%stem call t!at allows a "arent "rocess to
etermine t!e "rocess I*s o3 all its c!il "rocesses. i3 suc!in3ormation is neee+ t!e "arent "rocess s!oul sa8e t!e
returne c!il "rocess I* 8alue 3rom t!e 3ork s%stem call as
eac! c!il "rocess is create.
-
7/26/2019 522 UNIX Process(w3 1)
31/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian #1
getppid$%stem Call
getppid$%stem CallJunction: to o4tain "arent "rocess I*
Inclue Jile9s: Qs%s/t%"es.!R
$ummar%: "it 'et""i9 8oi (
eturn: $uccessA t!e "arent "rocess I*JailureA A1
$ets errnoA%es
Eam"le o3getppid$%stem Call
printf(+y "arent "rocess '# is ,d -n getppid( ) )
-
7/26/2019 522 UNIX Process(w3 1)
32/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian #2
Process ,rou" I*
Process ,rou"!en a "rocess 'enerates c!il "rocesses+ t!e o"eratin'
s%stem will automaticall% create a "rocess 'rou". -!e initial
"arent "rocess is known as t!e "rocess leaer.
Process ,rou" I*
E8er% "rocess 4elon' to a "rocess 'rou" t!at is ienti3ie 4%
an inte'er num4er calle a "rocess 'rou" I*. -!e "rocess
leaer?s I* will 4e t!e same as its "rocess 'rou" I*.
-
7/26/2019 522 UNIX Process(w3 1)
33/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian ##
getpgid$%stem Callgetpgid$%stem Call
Junction: to o4tain "rocess 'rou" I*
Inclue Jiles: Qs%s/t%"es.!R
Qunist.!R
$ummar%: "it 'et"'i9"it "i(eturn: $uccess A t!e "rocess 'rou" I*
Jailure A A1
$ets errno: %es
errno messa'e:S1 : in8ali access "ermissions 3or t!e callin' "rocess
S#: no suc! "rocess I* as "i
-
7/26/2019 522 UNIX Process(w3 1)
34/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian #;
Eam"le o3getpgid9ian#.cc 91
"rint396...PI* PPI* ,I*...+...( 9PI* 2@2B PPI* 2;#@ ,I* 2;#@
iY(
i393ork9YY
"rint39...(2@2DiY1( iY1(
i393ork9YY i393ork9YY
"rint39...(2@## "rint39...(2@#
iY2( iY2( iY2( iY2(
i393ork9YY i393ork9YY i393ork9YY i393ork9YY(
"rint39...(2@#5 "rint39...(2@#; "rint39...(2@#2 "rint39...(2@#1
-
7/26/2019 522 UNIX Process(w3 1)
35/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian #5
Eam"le o3 'et"'it 9Ian#.cc 92
2;#@
2@2B
2@2D 2@## 2@#5
2@# 2@#2 2@#;
2@#1
-
7/26/2019 522 UNIX Process(w3 1)
36/37
EEE522: Ian McCrum: www.eej.ulst.ac.uk/~ian #)
To Use Floppies:
c /mntmkir 3lo"
mount
-
7/26/2019 522 UNIX Process(w3 1)
37/37
EEE522: Ian McCrum: www eej ulst ac uk/ ian #@
Eercises 2
1. Use t!e 3in comman to list al"!a4eticall% all #letter commans on %our s%stem. 9!int searc! t!e
"at!+ 3in 3iles wit! t!eir >? 4it set+ use t!ewilcar KKK. $en t!e out"ut o3 %our comman into
a tet 3ile calle #.tt2. Put %our comman into a scri"t+ c!eck it works
#. rite a C "ro'ram t!at calls it usin' t!e s%stem call
;. rite a C "ro'ram t!at in8okes 3ork t!ree times eac!c!il can call a scri"t. $earc! t!ree i33erentirectories( /4in+ /usr/4in an /usr/X11)/4in anwritet!e results to t!ree tet3iles 6pid.t#t$