Multiprogramming
Multiprogramming
Multiprogramming
programmi ng
Inamul t
iprogrammi ngsy stem t
hereareoneormor eprogramsl oadedi nmai nmemor ywhich
arereadyt oexecut e.Onlyonepr ogram atat i
mei sabletogett heCPUf orexecut i
ngits
i
nstr uct
ions( i
.e.
,thereisatmostonepr ocessrunningont hesy stem)whi l
eal lt
heot her
sar e
waitingtheirturn.
-Themai nideaofmul t
iprogrammi ngistomaximizet heuseofCPUt ime.Indeed,supposethe
currentl
yr unningprocessi sperforminganI/Otask( whi
ch, bydef i
nit
ion,doesnotneedt heCPU
tobeaccompl ished).Then, theOSmayi nterr
uptthatprocessandgi vethecont r
oltooneoft he
otherin-mai n-
memor yprogramst hatarereadytoexecute(i.e.processcont extswit
ching).
- Int
hisway,
noCPUt i
mei swastedbythesystem wai
ti
ngfortheI/
Otasktobe
complet
ed,
andarunningprocesskeepsexecut
ingunt
ilei
theri
tvol
unt
ari
l
yrel
easest
he
CPUorwhenitbl
ocksforanI/Ooperati
on.
- Theref
ore,
theulti
mategoalofmult
ipr
ogr
ammi
ngi
stokeept
heCPUbusyasl
ongas
ther
eareprocessesr
eadytoexecute.
- Notethati
norderforsuchasy st
em tof
uncti
onproper
ly,
theOSmustbeabl etoload
multi
plepr
ogr
amsi ntoseparat
eareasofthemainmemor yandprovi
det herequi
red
pr
otecti
ontoavoidt hechanceofoneprocessbei
ngmodi f
iedbyanotherone.
- Otherpr
oblemsthatneedt
obeaddressedwhenhavi
ngmul
ti
plepr
ogr
amsi
nmemor
yis
f
ragmentat
ionasprogramsent
erorl
eavethemainmemor
y.
- Anot
heri
ssuet
hatneedst
obehandl
edaswelli
sthatl
argeprogr
amsmaynotf
itatonce
i
nmemorywhichcanbesolv ngpagi
edbyusi nati
onandvirt
ualmemor
y.
- Fi
nall
y ,not
ethatift
hereareNr eadyprocessesandalloft
hosear
ehi ghl
yCPU-bound
(i
.e.
,theymostlyexecuteCPUt asksandnoneorv eryfewI
/Ooperati
ons),i
nthevery
worstcaseonepr ogram mightwaital
ltheotherN-1onestocomplet
ebeforeexecut
ing.
Multi
processing
Multi
processingsomet i
mesr efer
st oexecuti
ngmul ti
plepr ocesses( progr ams)atthesame
ti
me.Thi smightbemi sleadingbecausewehav ealreadyi ntr
oducedt het erm
“mult
iprogr
ammi ng”todescr i
bethatbefore.I
nfact,mul ti
processingr ef erstothehardware(i
.e.
,
theCPUuni ts)ratherthant hesof t
ware(i.
e.,r
unningpr ocesses).Iftheunder lyi
nghardware
provi
desmor ethanonepr ocessort henthatismultiprocessing.Sev er alvari
ati
onsont hebasic
schemeexi st,e.
g.,multi
plecor esononedi eormultipl
edi esinonepackageormul ti
ple
packagesinonesy stem.
NOTE: Anyway ,asy st
em canbebot hmultipr
ogrammedbyhav i
ngmul t
ipleprogramsrunningat
thesamet i
meandmul t
iprocessingbyhav i
ngmor et hanonephy sical processor.
Mult
it
aski
ng
Mult
it
aski
nghast
hesamemeaningofmul t
ipr
ogrammingbuti
namor egener
alsense,asi
t
r
efer
stohavi
ngmult
ipl
e(pr
ogr
ams,processes,t
asks,
thr
eads)r
unni
ngatthesamet i
me.This
termisusedi nmoder noperatingsy st
emswhenmul ti
pletasksshar eacommonpr ocessi
ng
resource(e.g.
,CPUandMemor y )
.Atanyt i
met heCPUi sexecut ingonet askonlywhileother
taskswaitingthei
rturn.Theil
lusionofpar al
lel
ism isachievedwhent heCPUi sreassignedto
anothertask(ie.pr
. ocessorthr eadcontextswitchi
ng).
-Therearesubt l
ediffer
encesbet weenmul t
it
askingandmul ti
programmi ng.Ataskina
mul t
it
askingoperati
ngsy st
em i snotawhol eappl i
cati
onpr ogram butitcanalsor ef
ertoa
“thr
eadofexecut i
on”whenonepr ocessisdivi
dedi nt
osub- tasks.
- Eachsmallert
askdoesnothij
acktheCPUunt i
li
tfi
nishesli
keint
heolder
multi
progr
ammi ngbutr
atherafai
rshareamountoftheCPUt i
mecalledquantum.
Justtomakeiteasytor
emember ,bothmult
ipr
ogrammi ngandmult
it
askingoperat
ing
syst
emsar e(CPU)ti
meshar i
ngsystems.
- Howev er
,whil
einmul ti
programming(ol
derOSs)oneprogram asawholekeepsrunni
ng
unti
li
tblocks,i
nmul ti
taski
ng(modernOSs)timeshari
ngisbestmanif
estedbecause
eachrunningprocess/thr
eadtakesonl
yafairquant
um oftheCPUtime.
Mul ti
thr eading
Upt onow, wehav et alkedaboutmul t
ipr ogrammi ngasawayt oall
owmul tiplepr ogr amsbei ng
residenti nmai nmemor yand( apparent ly)runni ngatt hesamet ime.Then, mul titaski ngr ef
ersto
mul ti
pl etasksr unni ng( appar ent l
y)simul taneousl ybyshar i
ngt heCPUt i
me.Fi nally,
mul ti
pr ocessi ngdescr ibessy stemshav ingmul tipleCPUs.So, wher edoesmul t
ithreadi ng
comei n?
Mul ti
thr eadingi sanexecut ionmodelt hatal l
owsasi ngl epr ocesst ohavemul tiplecode
segment s(i.
e. ,threads)r unconcur rent lywi thint he“ cont ext”oft hatprocess.Youcant hi
nkof
threadsaschi ldpr ocessest hatshar et hepar entpr ocessr esourcesbutexecut ei ndependent ly.
Mul ti
pl ethreadsofasi nglepr ocesscanshar et heCPUi nasi ngleCPUsy stem or( purely)run
i
npar alleli
namul tiprocessi ngsy stem
Whyshoul dweneedt ohav emul ti
plethr eadsofexecut i
onwi thinasi nglepr ocesscont ext?
Wel l
,consi derf orinst anceaGUIappl icat i
onwher et heusercani ssueacommandt hatr equire
l
ongt imet of inish( e.g. ,acompl exmat hemat ical comput ation).Unl essyoudesi gnt hi s
commandt ober uni nasepar ateexecut iont hready ouwi llnotbeabl etoi nteractwi t hthemai n
applicat i
onGUI( e.g.,toupdat eapr ogr essbar )becausei tisgoi ngt obeunr esponsi vewhi l
et he
calculat i
oni st akingpl ace.
Ofcour se,desi gni ngmul tit
hr eaded/ concur rentappl i
cati
onsr equirest hepr ogr ammert ohandl e
sit
uat ionst hatsi mpl ydon’ toccurwhendev elopi ngsi ngle-threaded, sequent ial appl i
cat i
ons.For
i
nst ance, whent woormor et hreadst ryt oaccessandmodi fyashar edresour ce( race
condi ti
ons) ,thepr ogr ammermustbesur et hiswi llnotleav et hesy stem inani nconsi stentor
deadl ockst ate.Ty pical l
y,thist hreadsy nchr onizat i
oni ssol vedusi ngOSpr imi tives, suchas
mut exesandsempaphor es.