Sie sind auf Seite 1von 13

Lab 5

Lab Objective: Process creation, execution & termination in LINUX 1. Process Concept 1.1 What is a Process?
A process is a program in execution. Processes exist in main memory.

Figure 4.1: Process in Linux

Code Section: Sequence of Instructions


Static Data Section: e.g., global ariables an! arrays D na!ic Data Section: e.g., malloc"# or calloc"# calls Stac" Section: e.g., local ariables, function$proce!ure parameters, an! return a!!ress

PC #Progra! Counter$: next instruction

%. Process Schedu&ing 1
'u&tiprogra!!ing% S&itc' t'e (PU among processes so efficiently t'at t'e (PU is al&ays running

1 (i!e)sharing: S&itc' t'e (PU among processes so frequently t'at users can interact &it' t'eir programs &'ile t'ey are running

%.1 Process State

Figure %: Process State )iagram

* Ne& process 'as +rea!y, state * -ea!y state% .'e process is &aiting to be assigne! to t'e (PU * -unning state% An instruction of t'e process is being execute! by t'e (PU
* /aiting "bloc0e!# state% .'e process is &aiting for an e ent "e.g., an I$1 completion, termination of a c'il! process, a signal$message from anot'er process#.

*. Process Creation and Process (er!ination


Linux$UNIX System (alls +for0,, +exec ,, +&ait,, +abort$exit"2#$exit"3#$exit"4#$0ill,, +getpi!,, an! +getppi!,

*.1 Process Creation i. For" #$

Figure *: Linux 5for0"#6 operation

7 If you use +&ait, system call, t'e parent &aits until some or all of its c'il!ren terminate

7 If you !on6t use +&ait, system call, t'e parent continues to execute concurrently &it' its c'il!ren

7 If you use only +for0, call, t'e c'il! process is a !uplicate of t'e parent process 7 If you use +for0, in t'e parent an! +exec , in t'e c'il!, t'e c'il! process &ill 'a e a program loa!e! into it.

Figure 4: After t'e c'il! calls +exec , to loa! P3, t'e stac0, !ata, an! co!e of t'e c'il! are o er&ritten

Note t'at a parent can call +for0, se eral times to create more c'il!ren. ('il! process can also call +for0, a 'ierarc'y of processes

So!e di++erences bet,een the chi&d and parent process are:


* * * *
*

!ifferent pi!s in t'e parent, for0" # returns t'e pi! of t'e c'il! process if a c'il! process is create! in t'e c'il!, for0" # al&ays returns 8 separate copies of all !ata, inclu!ing ariables &it' t'eir current alues an! t'e stac0
separate program counter "P(# in!icating &'ere to execute next9 originally bot' 'a e t'e same

alue but t'ey are t'ereafter separate

after for0, t'e t&o processes !o not s'are ariables

+or" returns:
*

t'e pi! of t'e ne& c'il! process% to t'e parent process9 t'is is equi alent to telling t'e parent

t'e name of its c'il!.


* *

8% to t'e c'il! process :2% 2 if t'ere is an error9 i.e., for0" # faile! because a ne& process coul! not be create!

2.

,ait #$
a system call t'at causes t'e process to &ait for a signal "&aits until any type of signal is recei e! from any process#. * most commonly use! in a parent process to &ait for t'e signal t'at t'e 1S sen!s to a parent &'en its c'il! is terminate! * returns t'e pi! of t'e process sen!ing t'e signal * see
*

man 3 &ait for !ocumentation

iii. e-ec& #$
Suppose &e &ant t'e ne& process to !o somet'ing quite !ifferent from t'e parent process, namely run a !ifferent program. .'e execl system call loa!s a ne& executable into memory an! associates it &it' t'e current process. In ot'er &or!s, it c'anges t'ings so t'at t'is process starts executing executable co!e from a !ifferent file. In t'e follo&ing program, execl is calle! to loa! t'e file $bin$ls into memory. .'e argument ector is set to t'e &or!s ;ls; an! ;:l;. * arg <8= is set to ;ls; * arg <2= is set to ;:l; * .'e number of arguments to execl can ary so &e en! &it' NULL to s'o& t'at t'ere are no more arguments.
*

3.2 Process (er!ination In UNIX, a process terminates &'en it calls +exit, "Normal termination# or +abort, "abnormal termination#.
7 +exit, terminates t'e calling process imme!iately. o > ery open file &ill be close!.

o All t'e resources &ill be free!.

15

.'e c'il!ren of t'e process are in'erite! by process 2 "i.e., +init, process#.

2 Parent can terminate any of its c'il!ren &it' +0ill"c'il! PI), SI?@ILL#, operation for a ariety of reasons. 3 Note t'at a c'il! can terminate its parent &it' +0ill"getppi!"#, SI?@ILL#, operation.

Figure .: 1 er ie& of t'e use of for0"#, exit"#, &ait"#, an! exec e"#

Lab (as":
1. /rite a program t'at creates a c'il! process. Parent process s'oul! print + / a! a parent process. ' id is #Parent id$, an! c'il! process s'oul! print +/ a! chi&d Process. ' id is #chi&d id$,. 2. /rite a co!e t'at performs t'e a!!ition an! multiplication of elements in array. Aour co!e s'oul! use !i i!e an! conquer t'at is addition shou&d be acco!p&ished b chi&d process an! !u&tip&ication shou&d be acco!p&ished b parent process 3. /rite a co!e t'at fin!s t'e user name #,hois$ in c'il! co!e an! t'e list of all files an!
!irectories #&s 0&$ in parent co!e. Parent mo!ule s'oul! &ait for c'il! mo!ule to en!.

PDF to Wor

Das könnte Ihnen auch gefallen