0% found this document useful (0 votes)
7 views

OS Unit2-Process

The document discusses processes in an operating system. It defines what a process is and describes the different states a process can be in. It also explains process control blocks which contain information about each process, and discusses process scheduling and different scheduling queues.

Uploaded by

Shubham Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

OS Unit2-Process

The document discusses processes in an operating system. It defines what a process is and describes the different states a process can be in. It also explains process control blocks which contain information about each process, and discusses process scheduling and different scheduling queues.

Uploaded by

Shubham Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

OPERATING SYSTEM

Process Concept:
What is a process?

 An activity or task or user program or system program performed by operating system is


called as process.
 A process is a program in execution
 A process is more than program code and is a text section component
 A process includes current activity as represented by program counter and contents of
processor's registers
 A process also includes processor stack, which contains temporary data such as function
parameters, local variables and return address.
 A process also includes data section which consists of global variables.
 A process also include a heap which is a memory dynamically allocated during process
run time.
 A structure of process is shown in Figure 3.1

 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.

 A program becomes a process when an executable file is loaded into memory.

 Example for the process -

- a word processor, a Web browser, and an e-mail package

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.

 A process may be in one of the following states:

1. New: The process is being created.

2. Running: Instructions are being executed.

3. Waiting: The process is waiting for some event to occur (such as an I/O completion

or reception of a signal).

4. Ready: The process is waiting to be assigned to a processor.

5. Terminated: The process has finished execution.

 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

instant. Many processes may be ready and waiting, however.

 The state diagram corresponding to these states is presented in Figure 3.2

4
Lecture Notes by Dr. Sumalatha Aradhya,
Dept of CSE, SIT, Tumakuru
OPERATING SYSTEM

Process Control Block


 Each process is represented in the operating system by a process control block (PCB)—
also called a task control block.
 A PCB is shown in Figure 3.3.

 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

 A common representation of process scheduling is a queuing diagram and is shown in


Figure 3.6

 Each rectangular box represents a queue.


 Two types of queues are present: the ready queue and a set of device queues.
 The circles represent the resources that serve the queues, and the arrows indicate the
flow of processes in the system
 A new process is initially put in the ready queue. It waits there until it is selected for
execution, or dispatched.
 Once the process is allocated the CPU and is executing, one of several events could
occur such as,
I. The process could issue an I/O request and then be placed in an I/O queue.
II. The process could create a new child process and wait for the child’s
termination.
III. The process could be removed forcibly from the CPU, as a result of an
interrupt, and be put back in the ready queue.
- In the first two cases - I and II, the process eventually switches from the waiting state
to the ready state and is then put back in the ready queue.
 A process continues this cycle until it terminates, at which time it is removed from all
queues and has its PCB and resources deallocated.

 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

 Long Term Scheduler:


o Selects processes from job pool and loads them into memory for execution
o The long-term scheduler executes much less frequently; minutes may separate
the creation of one new process and the next.
o The long-term scheduler controls the degree of multiprogramming (the number of
processes in memory).
- If the degree of multiprogramming is stable, then the average rate of process
creation must be equal to the average departure rate of processes leaving the
system. Thus, the long-term scheduler may need to be invoked only when a
process leaves the system.
o Because of the longer interval between executions, the long-term scheduler can
afford to take more time to decide which process should be selected for execution
o It is important that the long-term scheduler make a careful selection. In general,
most processes can be described as either I/O bound or CPU bound.
o An I/O-bound process is one that spends more of its time doing I/O than it spends
doing computations. A CPU-bound process, in contrast, generates I/O requests
infrequently, using more of its time doing computations. It is important that the
long-term scheduler select a good process mix of I/O-bound and CPU-bound
processes.
o On some systems, the long-term scheduler may be absent or minimal.
For example, time-sharing systems such as UNIX and Microsoft Windows
systems often have no long-term scheduler but simply put every new process in
memory for the short-term scheduler

 Short Term Scheduler:


o Selects from among the processes that are ready to execute and allocates the CPU
to one of them. Hence, can also be termed as CPU scheduler
o The short-term scheduler must select a new process for the CPU frequently.
o A process may execute for only a few milliseconds before waiting for an I/O
request.
o Often, the short-term scheduler executes at least once every 100 milliseconds.
Because of the short time between executions, the short-term scheduler must be
fast. If it takes 10 milliseconds to decide to execute a process for 100 milliseconds,
then 10/(100 + 10) = 9 percent of the CPU is being used (wasted) simply for
scheduling the work
o If all processes are I/O bound, the ready queue will almost always be empty, and
the short-term scheduler will have little to do.
o If all processes are CPU bound, the I/O waiting queue will almost always be
empty, devices will go unused, and again the system will be unbalanced.
o The system with the best performance will thus have a combination of CPU
bound and I/O-bound processes.
 Medium Term Scheduler:
o Used in Time sharing Systems
o The key idea behind a medium-term scheduler is that sometimes it can be
advantageous to remove a process from memory (and from active contention for
the CPU) and thus reduce the degree of multiprogramming. Later, the process
9
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

You might also like