OS Unit2-Process
OS Unit2-Process
Process Concept:
What is a process?
A program is a passive entity, such as a file containing a list of instructions stored on disk
(often called an executable file). In contrast, a process is an active entity, with a program
counter specifying the next instruction to execute and a set of associated resources.
3
Lecture Notes by Dr. Sumalatha Aradhya,
Dept of CSE, SIT, Tumakuru
OPERATING SYSTEM
Process State
As a process executes, it changes state.
The state of a process is defined in part by the current activity of that process.
3. Waiting: The process is waiting for some event to occur (such as an I/O completion
or reception of a signal).
The state names are arbitrary, and they vary across operating systems.
It is important to realize that only one process can be running on any processor at any
4
Lecture Notes by Dr. Sumalatha Aradhya,
Dept of CSE, SIT, Tumakuru
OPERATING SYSTEM
PCB simply serves as the repository for any information that may vary from process to
process. It contains many pieces of information associated with a specific process as
given below:
Process State: The process state may be new, ready, running, waiting, halted, and so
on.
Program counter:
- The counter indicates the address of the next instruction to be executed for this
process.
CPU registers:
- The registers vary in number and type, depending on the computer architecture.
- They include accumulators, index registers, stack pointers, and general-purpose
registers, plus any condition-code information.
- Along with the program counter, this state information must be saved when an
interrupt occurs, to allow the process to be continued correctly afterward.
- The context switching between processes are shown in Figure 3.4
5
Lecture Notes by Dr. Sumalatha Aradhya,
Dept of CSE, SIT, Tumakuru
OPERATING SYSTEM
CPU-scheduling information:
- This information includes a process priority, pointers to scheduling queues, and
any other scheduling parameters
Memory-management information:
- This information may include such items as the value of the base and limit
registers and the page tables, or the segment tables, depending on the memory
system used by the operating system.
Accounting information:
- This information includes the amount of CPU and real time used, time limits,
account numbers, job or process numbers, and so on.
I/O status information.
- This information includes the list of I/O devices allocated to the process, a list of
open files, and so on.
PROCESS SCHEDULING
Process scheduling is required to meet the multiprogramming objective, i.e., to have
some process running all times, to maximize CPU utilization and to meet the
timesharing objective i.e., to switch the CPU among process along with interactive
response from user.
The process scheduler selects an available process (possibly from a set of several
available processes) for program execution on the CPU.
For a single-processor system, there will never be more than one running process. If there
are more processes, the rest will have to wait until the CPU is free and can be
rescheduled.
6
Lecture Notes by Dr. Sumalatha Aradhya,
Dept of CSE, SIT, Tumakuru
OPERATING SYSTEM
Scheduling Queues:
Job Queue:
- As processes enter the system, they are put into a job queue, which consists of all
processes in the system
Ready Queue:
- The processes that are residing in main memory and are ready and waiting to
execute are kept on a list called the ready queue.
- This queue is generally stored as a linked list. A ready-queue header contains
pointers to the first and final PCBs in the list. Each PCB includes a pointer field
that points to the next PCB in the ready queue
I/O Device Queue:
- When a process is allocated the CPU, it executes for a while and eventually quits,
is interrupted, or waits for the occurrence of a particular event, such as the
completion of an I/O request.
- The list of processes waiting for a particular I/O device is called a device queue.
The representation of scheduling queues is shown in Figure 3.5
7
Lecture Notes by Dr. Sumalatha Aradhya,
Dept of CSE, SIT, Tumakuru
OPERATING SYSTEM
SCHEDULERS:
The operating system must select, for scheduling purposes, processes from scheduling
queues in some fashion. The selection process is carried out by the appropriate
scheduler.
There are different types of schedulers namely, Long Term Scheduler, Short Term
Scheduler and Medium Term Scheduler.
8
Lecture Notes by Dr. Sumalatha Aradhya,
Dept of CSE, SIT, Tumakuru
OPERATING SYSTEM
can be reintroduced into memory, and its execution can be continued where it
left off. This scheme is called swapping.
o The medium term scheduler is diagrammed in Figure 3.7
o The process is swapped out, and is later swapped in, by the medium-term
scheduler.
o Swapping may be necessary to improve the process mix or because a change
in memory requirements has overcommitted available memory, requiring
memory to be freed up.
Context Switch:
Interrupts cause the operating system to change a CPU from its current task and to run a
kernel routine. Such operations happen frequently on general-purpose systems.
When an interrupt occurs, the system needs to save the current context of the process
running on the CPU so that it can restore that context when its processing is done, essentially
suspending the process and then resuming it
The context is represented in the PCB of the process. It includes the value of the CPU
registers, the process state (see Figure 3.2), and memory-management information.
Switching the CPU to another process requires performing a state save of the current process
and a state restore of a different process. This task is known as a context switch.
When a context switch occurs, the kernel saves the context of the old process in its PCB and
loads the saved context of the new process scheduled to run. (Figure 3.4)
Context-switch time is pure overhead, because the system does no useful work while
switching.
Switching speed varies from machine to machine, depending on the memory speed, the
number of registers that must be copied, and the existence of special instructions
Context-switch times are highly dependent on hardware support.
The more complex the operating system, the greater the amount of work that must be done
during a context switch
10
Lecture Notes by Dr. Sumalatha Aradhya,
Dept of CSE, SIT, Tumakuru
OPERATI
NGSYSTEM
OPERATI
ONSONPROCESSES
Dur
ingt
hecour
seofexecut
ion,
apr
ocessmaycr
eat
esev
eralnewpr
ocesses.
Thecreat
ingprocessi
scal
l
edapar
entpr
ocess,
andt
henewpr
ocessesar
ecal
l
edt
he
chi
ldr
enoft hatpr
ocess
Theoper
ati
ngsystemsidenti
fypr
ocessesaccor
dingt
oauni
quepr
ocessi
dent
if
ier(
or
pi
d),
whichist
ypical
lyanint
egernumber.
Thepi
dpr
ovidesauniquevalueforeachprocessi
nthesy
stem,andi
tcanbeusedasan
i
ndext
oaccessvar
iousatt
ributesofaprocesswit
hint
hekernel
Atreeofpr
ocessesmaygetcreat
edbyt heparentpr
ocess.Asampl
ety
pical
tree
st
ructur
eofprocessofLi
nuxOperat
ingSystem i
sshowninFigur
e3.
8
- I
ntr
ee,
eachpr
ocessandcor
respondi
ngpi
disshown
- The'
i
nit
'pr
ocessi
sser
vesast
her
ootpar
entpr
ocessf
oral
luserpr
ocesses.
- Oncethesy
stem hasbooted,t
hei
nitpr
ocesscanal
socreat
evar
ioususerpr
ocesses,
suchasaweborprintser
ver,
ansshserver
,andt
heli
ke.
- Thekthreaddprocess,achi
l
dprocessofini
tprocess,i
sresponsi
bleforcreati
ng
addi
ti
onal pr
ocessesthatper
for
mt asksonbehalfoftheker
nel(i
nthissit
uation,khel
per
andpdflush).
- Thesshdprocess,anot
herchi
l
dpr ocessofini
tprocess,i
sresponsi
blef
ormanagi
ng
cl
ient
sthatconnecttot
hesystem byusingssh(whichisshortf
orsecur
eshel
l)
.
- Thel
ogi
npr
ocessi
sresponsi
blef
ormanagi
ngcl
i
ent
sthatdi
rect
lyl
ogont
othesy
stem.
- Usingt
hebashcommand-
li
nei
nter
face,
thi
suserhascr
eat
edt
hepr
ocesspsaswel
las
theemacsedi
tor
.
Toaccompli
shtasks,
achil
dprocessmaybeneedandbeabl
etoobtai
nit
sr esour
ces
di
rect
lyf
rom t
heoperati
ngsyst
em, ori
tmaybeconst
rai
nedt
oasubsetoftheresour
cesof
2
Lect
ureNot
esbyDr
.Sumal
atha
Ar
adhy
a,
DeptofCSE,
SIT,
Tumakur
u
OPERATI
NGSYSTEM
t
hepar
entpr
ocess.
Thepar
entmayhavet
opart
it
ioni
tsr
esour
cesamongitschi
ldr
en,ori
tmaybeabl
etoshar
e
someresour
ces(
suchasmemoryorfi
l
es)amongsev
eralofit
schil
dren
Rest
rict
ingachi
ldprocesst
oasubsetoft
heparent
’sr
esourcespr
event
sanypr
ocessf
rom
over
loadingt
hesyst
em bycreat
ingt
oomanychi
l
dprocesses.
Inaddi
ti
ontosupplyi
ngvari
ousphysi
cal
andlogi
calr
esour
ces,
thepar
entpr
ocessmaypass
alongi
nit
ial
izat
iondat
a(i
nput)t
othechi
ldpr
ocess
Whenapr
ocesscr
eat
esanewpr
ocess,
twopossi
bil
i
tiesf
orex
ecut
ionex
ist
:
1. Thepar
entcont
inuest
oex
ecut
econcur
rent
lywi
thi
tschi
l
dren.
2. Thepar
entwai
tsunt
ilsomeoral
lofi
tschi
l
drenhav
eter
minat
ed.
Ther
ear
eal
sot
woaddr
ess-
spacepossi
bil
i
tiesf
ort
henewpr
ocess:
1. Thechi
l
dprocessisadupl
i
cat
eoft
hepar
entpr
ocess(
ithast
hesamepr
ogr
am and
dat
aastheparent
).
2. Thechi
l
dpr
ocesshasanewpr
ogr
am l
oadedi
ntoi
t.
Pr
ocessCr
eat
ioni
nUNI
Xoper
ati
ngsy
stem
Anewpr
ocessi
scr
eat
edbyt
hef
ork(
)sy
stem cal
l
.
Thenewprocessconsi
stsofacopyoftheaddr
essspaceoftheori
ginal
process.Thi
s
mechani
sm al
lowstheparentpr
ocesst
ocommuni cat
eeasi
lywit
hitschil
dprocess.
3
Lect
ureNot
esbyDr
.Sumal
atha
Ar
adhy
a,
DeptofCSE,
SIT,
Tumakur
u
OPERATI
NGSYSTEM
Boththeparentandthechildprocessesconti
nueex ecut
ionattheinstr
ucti
onaft
erthefor
k()
,
withonedif
ference:t
hereturncodef ort
hefork()iszer
oforthenew( chil
d)pr
ocess,wher
eas
the(nonzer
o)processidenti
fi
erofthechildi
sr etur
nedtotheparent.
Aft
erafor
k()sy
stem cal
l,
oneoft
hetwoprocessest
ypical
lyusest
heex
ec(
)sy
stem cal
l
tor
epl
acetheprocess’
smemoryspacewi
thanewpr ogram.
Theexec(
)sy
stem cal
ll
oadsabi
nar
yfi
l
eint
omemor
yandst
art
sit
sexecut
ion.
I
nthismanner
,thet
wopr
ocessesar
eabl
etocommuni
cat
eandt
hengot
hei
rsepar
ate
ways.
Theparentcant hencr
eatemorechil
dren;
or,i
fithasnothi
ngelset
odowhilethechi
l
d
runs,
itcanissueawai t
()sy
stem cal
ltomoveit
selfof
fthereadyqueueunt
ilt
he
termi
nati
onoft hechil
d.
4
Lect
ureNot
esbyDr
.Sumal
atha
Ar
adhy
a,
DeptofCSE,
SIT,
Tumakur
u
OPERATI
NGSYSTEM
Acallt
oexec(
)overl
ayst
heprocess’saddr
essspacewi
thanewpr
ogr
am,
thecal
lto
exec(
)doesnotr
etur
ncontr
olunlessanerr
oroccurs
Fi
gur
e3.
9showspr
ogr
am f
orpr
ocesscr
eat
ionusi
ngt
hef
orksy
stem cal
l
- Asrepr
esentedi
ntheprogr
am,t
wodi
ff
erentpr
ocesses(
par
entandchi
l
d)r
unni
ng
copi
esofthesameprogram.
- Theonlydi
ff
erenceist
hatthev
alueofpi
dforthechi
l
dpr
ocessi
szer
o,whi
l
ethatf
or
thepar
enti
sani nt
egerval
uegreat
ert
hanzero.
- Thechi
l
dprocessinher
it
spri
vil
egesandschedul
i
ngat
tri
but
esf
rom t
hepar
ent
,as
wel
lcer
tai
nresour
ces,suchasopenfi
les.
- I
nt heexampl
eprogram ,t
hechil
dpr
ocessthenov er
lay
sitsaddr
essspacewitht
he
UNIXcommand/ bin/l
s(usedt
ogetadir
ectoryli
sti
ng)usi
ngtheexecl
p()sy
stem cal
l
(ex
eclp()i
saver
sionoftheexec(
)sy
stem cal
l).
- Thepar
entwai
tsf
ort
hechi
l
dpr
ocesst
ocompl
etewi
tht
hewai
t(
)sy
stem cal
l
.
- Whenthechildprocesscompl
etes(byei
theri
mpli
cit
lyorexpl
ici
tl
yinvoki
ngexi
t(
)),
thepar
entprocessresumesfr
om thecal
l t
owait
(),
whereitcompletesusi
ngtheexi
t(
)
syst
em cal
l.
- Thereisnothi
ngtopreventthechil
df r
om notinvoki
ngexec( )andinsteadconti
nuing
toexecuteasacopyoft heparentprocess.I
nt hi
sscenar i
o,theparentandchil
dar e
concurr
entprocessesr
unningthesamecodei nstr
uctions.Becausethechildi
sa
5
LectureNotesbyDr .Sumalatha
Ar
adhy
a,
DeptofCSE, SIT,Tumakuru
OPERATI
NGSYSTEM
copyoft
hepar
ent
,eachpr
ocesshasi
tsowncopyofanydat
a.
TheFi
gur
e3.
10i
l
lust
rat
esf
ork(
)sy
stem cal
lsequences
Pr
ocessTer
minat
ioni
nUNI
Xoper
ati
ngsy
stem
Aprocessterminat
eswheni tf
ini
shesexecuti
ngitsfi
nalst
atementandaskst
he
oper
ati
ngsy st
em todelet
eitbyusingtheexi
t()sy
stem cal
l.Att
hatpoi
nt,t
heprocess
mayretur
nast atusval
uetoitsparentpr
ocess.
Al
lther
esourcesoft
hepr
ocess—i
ncl
udingphy
sicalandv
irt
ual
memor
y,openf
il
es,
and
I
/Obuff
ers—aredeal
l
ocat
edbytheoper
ati
ngsystem.
Aprocesscancausetheter
minat
ionofanot
herprocessviaanappr
opri
atesy
stem cal
l
.
Usual
ly,
suchasystem cal
lcanbeinv
okedarbi
tr
ari
lykil
leachot
her’
sjobs.
Aparentneedstoknowthei
denti
ti
esofi
tschi
ldrenifi
tist
otermi
nat
ethem.Thus,when
oneprocesscreat
esanewprocess,
thei
denti
tyofthenewlycr
eat
edprocessi
spassed
tot
hepar ent
.
Aparentmayt
ermi
nat
etheex
ecut
ionofoneofi
tschi
l
drenf
orav
ari
etyofr
easons,
such
ast
hese:
o Thechi
l
dhasexceededi
tsusageofsomeoft
her
esour
cest
hati
thasbeenal
l
ocat
ed.
o Thet
askassi
gnedt
othechi
l
disnol
ongerr
equi
red.
o Theparentisexi
ti
ng,andt
heoper
ati
ngsy
stem doesnotal
l
owachi
l
dtocont
inuei
f
i
tsparentter
minates.
Somesy st
emsdonotal l
owachi l
dtoexisti
fit
sparenthaster
minated.I
nsuchsystems,
i
faprocessterminat
es(ei
thernor
mallyorabnormal
ly)
,thenal
lit
schildr
enmustalsobe
ter
minat
ed.Thisphenomenon,ref
err
edtoascascadingt er
minat
ion,i
snormal
lyi
nitiat
ed
bytheoperat
ingsyst
em.
Howt
oter
minat
epr
ocessi
nUNI
Xoper
ati
ngsy
stem?
o Toi l
lust
rateprocessexecuti
onandt er
minat
ion,
consi
derthat
,inLi
nuxandUNIX
systems, onecanterminateaprocessbyusi
ngtheexi
t()syst
em cal
l,
prov
idi
ngan
exitstat
usasapar ameter
:
/
*exi
twi
thst
atus1*
/
exi
t(
1);
o Undernor
mal
ter
minat
ion,
exi
t(
)maybecal
l
edei
therdi
rectl
yori
ndir
ectl
y
6
Lectur
eNotesbyDr.Sumal
atha
Ar
adhy
a,
DeptofCSE,SIT,
Tumakuru
OPERATI
NGSYSTEM
o Apar entprocessmaywaitfort
heter
mi nat
ionofachil
dprocessbyusingthewai t(
)
system call
.Thewait(
)syst
em cal
li
spassedapar ameterthatal
lowstheparentto
obtaintheexitst
atusoft
hechil
d.Thi
ssy st
em cal
lal
sor et
urnstheprocessidenti
fi
er
ofthet er
minatedchil
dsothatt
heparentcantel
lwhichofitschi
ldr
enhast erminat
ed:
pi
dtpi
d;
i
ntst
atus;
pi
d=wai
t(
&st
atus)
;
o Whenapr ocessterminat
es, i
tsresourcesar edeal
locatedbytheoperati
ng
system.Howev er
,it
sentr
yi nthepr ocesstablemustr emainther
euntilt
hepar
ent
call
swait(
),becausetheprocesst ablecontainst
hepr ocess’
sexitst
atus.
ANot
eonZombi
ePr
ocess:
Aprocessthathast
ermi
nat
ed,
butwhosepar
enthasnoty
etcal
l
edwai
t(
),i
sknownasa
zombieprocess.
All
processestr
ansi
ti
ont
othi
sst
atewhent
heyt
ermi
nat
e,butgener
all
ytheyexi
stas
zombiesonlybr
ief
ly.
Oncethepar
entcal
l
swait(
),t
hepr
ocessi
dent
if
ieroft
hezombi
epr
ocessandi
tsent
ryi
n
t
heprocesstabl
ear
erel
eased.
I
faparentdi
dnotinvokewait(
)andinst
eadt er
minated,
ther
ebyl
eavi
ngit
schil
d
pr
ocessesasorphans.Li
nuxandUNI Xaddressthi
sscenari
obyassi
gni
ngtheini
t
pr
ocessasthenewpar entt
oorphanprocesses.
Theinitprocessperiodi
cal
l
yinvokeswai
t(
),t
herebyal
l
owingtheexi
tst
atusofany
orphanedpr ocesstobecoll
ect
edandrel
easingtheor
phan’
sprocessi
denti
fi
erand
process-t
ableentry
.
7
Lect
ureNot
esbyDr
.Sumal
atha
Ar
adhy
a,
DeptofCSE,
SIT,
Tumakur
u
OPERATI
NGSYSTEM
8
Lect
ureNot
esbyDr
.Sumal
atha
Ar
adhy
a,
DeptofCSE,
SIT,
Tumakur
u