CSE 232 Systems Programming LECTURE NOTES ! INTRODUCTION TO OPERATING SYSTEMS 1. " ntrodu#tion A computer system can be !"!e !nto # components$ - t%e hardware &CPU' memory' !nput(output e"!ces' etc.)' - t%e operating system' - t%e system programs &*or processors' sprea s%eets' account!n+ so,t*ares' comp!-ers'.) - t%e application programs. App-!cat!on Pro+rams System Pro+rams Operat!n+ System /ar*are $igure %. Hierarchy of computer components 1 2& Computer System Stru#ture pr!nter !s0 !s0 !s0 pr!nter tape1r!"e CPU contro--er contro--er contro--er system bus memory contro--er memory $igure 2. Hardware of a computer system 2&%& Run'time Environment - Conta!ns a set o, ser"!ce rout!nes t%at are a"a!-ab-e ,or use ur!n+ pro+ram e2ecut!on &e+. I(O ,unct!ons$ Pro+ram !n"o0es a ser"!ce rout!ne' OS ta0es care o, status test!n+' count!n+ o, bytes trans,erre' error c%ec0!n+) - Pro"!es ,ac!-!t!es ,or mana+!n+ t%e resources &e+. CPU !s s*!tc%e amon+ user 3obs accor!n+ to some pree,!ne po-!cy) 2&2& "nput and Output (")O* I(O e"!ces$ b-oc0 e"!ces c%aracter e"!ces I(O types$ Pro+ramme I(O Interrupt r!"en I(O &Interrupt ser"!ce rout!ne) 4 D!rect memory access &DMA) Example 1: Keyboard 5eyboar !nter,ace &contro--er) 6us Port 7 &Status) 5eyboar CPU e-ectron!cs Port 1 &Data) $igure 3& Keyboard controller Port$ an aressab-e re+!ster - input only ports$ CPU on-y reas ,rom !t - output only ports$ CPU on-y *r!tes to !t - ports for both input and output E"ery port %as a number. A port can be re,erre to !n an assemb-y -an+ua+e pro+ram by !ts number' suc% as$ IN 1 Status port$ c%ec0e by t%e CPU to see !, a 0ey %as been presse an ata !s reay to be sent to CPU Data port$ *%en a 0ey !s presse' !ts ASCII coe !s ,oun !n t%e ata port Po++ing$ CPU cont!nua--y e2am!nes t%e status port unt!- a 0ey !s presse &Pro+ramme I(O) Ex: Rea!n+ a c%aracter ,rom 0eyboar 5EYIN$ IN 7 8 rea status port !nto A ANA A 8 test A &17/ 9 0ey !s presse' 77/ 9 ot%er*!se) :; 5EYIN 8 3ump !, 77/ IN 1 8 ta0e ata !nto A RET < Example 2: Printer Pr!nter !nter,ace &contro--er) 6us Port 4 &Contro-) Pr!nter CPU !nter,ace e-ectron!cs Port < &Data) $igure ,& Printer controller Data port$ c%aracter to be pr!nte !s put !n t%!s port Contro+ port$ *%en t%e c%aracter !s reay !n t%e ata port' strobe s!+na- &t%at te--s t%e pr!nter t%at !t can ta0e t%e c%aracter !n ata port) !s sent. A t!m!n+ -oop must be use !n t%e CPU so t%at t%e c%aracters are ne"er passe to t%e pr!nter at a rate ,aster t%an !t can accept. Spec!a- c%aracters$ - carriage return$ causes t%e pr!nter %ea to mo"e to t%e -e,t s!e o, t%e paper - line feed$ causes t%e pr!nter to a"ance t%e paper by one -!ne Ex: =r!t!n+ a c%aracter to pr!nter C/AROUT$ OUT < 8 put c%ar. !n accumu-ator !nto output port CA>> DE>AY 8 ma0e sure pr!nter !s reay M?I A' 17/ 8 set up strobe *or &17/ 9 c%ar. !n ata port !s a"a!-ab-e ,or pr!nt!n+) OUT 4 8 put !t !nto contro- port to start strobe s!+na- to pr!nter M?I A' 77/ 8 reset strobe &77/ 9 c%ar. !n ata port !s not a"a!-ab-e ,or pr!nt!n+) OUT 4 8 put !t !nto contro- port to ,!n!s% strobe s!+na- RET # "nterrupt Pro#essing An !nterrupt !s a s!+na- t%at causes t%e computer to a-ter !ts norma- ,-o* o, !nstruct!on e2ecut!on. eg& !"!e by @ero' t!me out' I(O operat!on T%e !nterrupt automat!ca--y trans,ers contro- to an !nterrupt process!n+ rout!ne *%!c% ta0es some act!on !n response to t%e con!t!on t%at cause t%e !nterrupt' an t%en contro- !s returne to t%e pro+ram at *%!c% !ts e2ecut!on *as !nterrupte. pro+ram !nterrupt rout!ne !nterrupt occurs
Some !nterrupt types$ - I! interrupt: Generate by an I(O c%anne- or e"!ce. - Program interrupt: Generate by some con!t!on t%at occurs ur!n+ pro+ram e2ecut!on &e+. !"!e by @ero' !--e+a- mac%!ne !nstruct!on' etc.) - "imer interrupt: Generate by an !nterna- t!mer *!t%!n t%e CPU - #uper$isor call interrupt %#&'($ A S?C !nstruct!on +enerates an !nterrupt t%at trans,ers contro- to an OS ser"!ce rout!ne. T%e CPU s*!tc%es ,rom user moe to super"!sor moe. =%en an !nterrupt occurs' t%e status o, t%e CPU !s sa"e &re+!sters' pro+ram counter etc.) an t%e !nterrupt ser"!ce rout!ne !s e2ecute. A,ter t%e !nterrupt !s processe' t%ey are restore by t%e !nterrupt rout!ne an contro- !s trans,erre to t%e pro+ram. Sa"!n+ an restor!n+ t%e processor status !s ca--e context switching) Interrupt priority: Dur!n+ t%e process!n+ o, an !nterrupt' a-- !nterrupts o, eAua- or -o*er pr!or!ty are !n%!b!te' %!+%er pr!or!ty !nterrupts are a--o*e. At t%e en o, an !nterrupt' !, t%ere are more t%an one !nterrupt pen!n+' one *!t% t%e %!+%est pr!or!ty !s e2ecute. Interrupt mas*ing' !s use to pre"ent certa!n !nterrupts ,rom occurr!n+ *%!-e t%e ,!rst one !s be!n+ processe. B Example +: ,is* 6us reAuest -!ne D!s0 contro--er
CPU RAM DMA ot%er !nter,ace R(= -!ne De"!ce e-ectron!cs re+!sters Aress bus Data bus Contro- bus $igure -& ,is* controller Inter,ace re+!sters Status port Contro- port DMA aress re+!sters D./ (Dire#t .emory /##ess*0 !s a proceure ,or trans,err!n+ ata !rect-y bet*een contro--er an memory *!t%out t%e %e-p o, CPU. D./ devi#e$ !s -!0e a separate CPU *!t% ,unct!ons re-ate to ata trans,er' suc% as$ - s%uts t%e CPU o*n ,or s%ort per!os - se!@es contro- o, t%e system bus - trans,ers ata bet*een memory an e2terna- e"!ce *!t%out t%e !nterme!at!on o, CPU Dis1 #ontro++er$ accepts commans ,rom CPU suc% as$ - se-ect !s0 r!"e - set up DMA re+!sters - return current sector aress - return current trac0 aress - mo"e %ea !n(out one trac0 - mo"e %ea to trac0 7 - rea(*r!te current sector Status port$ conta!ns t%e resu-t o, a Auery ,rom CPU or status Contro+ port$ commans ,or t%e contro--er are p-ace D./ address registers$ t%ey rece!"e aresses ,rom CPU C 3& Operating Systems An operating system !s a pro+ram t%at acts as an !nterme!ary bet*een a user o, a computer an t%e computer %ar*are. Ma!n ,unct!ons o, an operat!n+ system are$ 1. T%e operat!n+ system !s an !nter,ace bet*een t%e user an t%e computer pro"!es access to t%e to t%e ut!-!ty pro+rams suc% as e!tors' ebu++ers' etc. starts t%e e2ecut!on o, t%e pro+rams pro"!es -o* -e"e- I(O rout!nes to t%e user contro-s access to t%e ,!-es pro"!es access ,unct!ons ,or t%e protect!on o, t%e resources an ata ,rom unaut%or!@e users etects errors suc% as memory error' e"!ce ,a!-ure' ar!t%met!c o"er,-o*' etc. an pro"!es error %an-!n+ rout!nes co--ects per,ormance stat!st!cs 4. T%e operat!n+ system mana+es t%e resources o, t%e system contro-s an a--ocates resources System resources are memory' CPU' I(O e"!ces' etc. 3&%& 2istori#a+ Revie3 of Operating Systems .onitors - Mon!tors are t%e s!mp-est operat!n+ systems. - S!n+-e user systems - A--o* user !nteract!on Simp+e 4at#5 Systems T%e user prepares a 3ob &*%!c% cons!sts o, pro+ram' ata an some contro- !n,ormat!on)' subm!ts !t to t%e computer operator an rece!"es t%e output a,ter t%e pro+ram !s e2ecute. T%e 3ob !s usua--y punc%e on cars an t%e output !s usua--y pr!nte. To spee up process!n+' 3obs *!t% s!m!-ar nees can be batc%e to+et%er an run as a +roup. T%us' t%e operator sorts t%e pro+rams *!t% s!m!-ar reAu!rements !nto batc%es' runs eac% batc% an sens t%e output to t%e pro+rammer. T%ere,ore !n a batc% operat!n+ system t%ere !s a -ac0 o, !nteract!on bet*een t%e user an t%e 3ob *%!-e t%at 3ob !s e2ecut!n+. T%e operat!n+ system !s a-*ays !n memory an !ts 3ob !s to trans,er contro- automat!ca--y ,rom one 3ob to t%e ne2t. D As t%e I(O e"!ces are s-o*er t%an t%e spee o, t%e CPU &t%e !,,erence may be t%ree orers o, ma+n!tue or more)' t%e CPU !s o,ten !-e. Eor us!n+ t%e system more e,,!c!ent-y' cars can be rea ,rom t%e car reaer onto t%e !s0. T%e operat!n+ system recors t%e!r -ocat!on on !s0 !n a tab-e. =%en a 3ob reAuests t%e pr!nter' t%e output !s cop!e !nto a system bu,,er an !s *r!tten to t%e !s0. =%en t%e 3ob !s comp-ete' t%e output !s pr!nte. T%!s ,orm o, process!n+ !s ca--e spooling &s!mu-taneous per!p%era- operat!on on +!ne). Spoo-!n+ !s a-so use ,or process!n+ ata at remote s!tes. T%e CPU sen t%e ata "!a commun!cat!on pat%s to a remote s!te an t%e process!n+ !s one *!t% no CPU !nter"ent!on. T%e CPU 3ust nees to be not!,!e *%en t%e process!n+ !s comp-ete' so t%at !t can spoo- t%e ne2t batc% o, ata. Spoo-!n+ o"er-aps t%e I(O o, one 3ob *!t% t%e computat!on o, ot%er 3obs. Dur!n+ t%e e2ecut!on o, one 3ob' t%e spoo-er may be rea!n+ t%e !nput o, anot%er 3ob *%!-e pr!nt!n+ t%e output o, a !,,erent 3ob. !s0 car reaer I(O -!ne pr!nter CPU $igure !. Spoo-!n+ .u+tiprogrammed 4at#5 Systems Spoo-!n+ pro"!es a -ob pool on !s0. In orer to !ncrease CPU ut!-!@at!on' 3obs !n t%e poo- may be sc%eu-e !n !,,erent *ays' suc% as' ,!rst1come ,!rst1ser"e' s%ortest 3ob ,!rst' pr!or!ty bas!s' etc. T%e most !mportant aspect o, 3ob sc%eu-!n+ !s t%e ab!-!ty to multiprogram. T%e operat!n+ system 0eeps se"era- 3obs &a subset o, 3obs !n t%e 3ob poo-) !n memory at a t!me. Dur!n+ t%e e2ecut!on o, one 3ob' !, !t *a!ts ,or an I(O operat!on to comp-ete' t%e operat!n+ system s*!tc%es to an e2ecutes anot%er 3ob. In t%!s *ay' as -on+ as t%ere !s a-*ays some 3ob to e2ecute' t%e CPU *!-- ne"er be !-e. Time'S5aring Systems In mu-t!pro+ramme batc% systems' t%e user cannot !nteract *!t% t%e pro+ram ur!n+ !ts e2ecut!on. In t!me1s%ar!n+ or mu-t!tas0!n+ systems' mu-t!p-e 3obs are e2ecute by t%e CPU s*!tc%!n+ bet*een t%em' but t%e s*!tc%!n+ occurs so ,reAuent-y t%at t%e user may !nteract *!t% eac% pro+ram *%!-e !t !s runn!n+. T!me1s%ar!n+ operat!n+ systems$ - uses CPU sc%eu-!n+ an mu-t!pro+ramm!n+' - uses t!me1s-!ce mec%an!sm' - a--o*s !nteract!"e I(O' F - a--o*s many users to s%are t%e computer s!mu-taneous-y. 2&6& Persona+ Computer (PC* Systems A computer system e!cate to a s!n+-e user !s re,erre to as a PC. In t%e ,!rst PCs' t%e operat!n+ system *as ne!t%er mu-t!user nor mu-t!tas0!n+ &e+. MS1DOS). T%e operat!n+ system concepts use !n ma!n,rames an m!n!computers' toay' are a-so use !n PCs &e+. UNIG' M!croso,t =!no*s NT' Mac!ntos% OS). 2&7& Para++e+ Systems Para--e- systems %a"e more t%an one processor. In mu-t!processor systems &t!+%t-y coup-e)' processors are !n c-ose commun!cat!on' suc% as t%ey s%are computer bus' c-oc0' memory or per!p%era-s. 2&-& Distri8uted Systems D!str!bute systems a-so %a"e more t%an one processor. Eac% processor %as !ts -oca- memory. Processors commun!cate t%rou+% commun!cat!on -!nes &e+. Te-ep%one -!nes' %!+%1spee bus' etc.). Processors are re,erre to as s!tes' noes' computers epen!n+ on t%e conte2t !n *%!c% t%ey are ment!one. Mu-t!computer systems are -oose-y coup-e. E2amp-e app-!cat!ons are e1ma!-' *eb ser"er' etc. 2&!& Rea+'time Systems Rea-1t!me systems are spec!a- purpose operat!n+ systems. T%ey are use *%en t%ere are r!+! t!me reAu!rements on t%e operat!on o, a processor or t%e ,-o* o, ata' an t%us !t !s o,ten use as a contro- e"!ce !n a e!cate app-!cat!on &e+. ,ue- !n3ect!on systems' *eapon systems' !nustr!a- contro- systems' .). It %as *e-- e,!ne' ,!2e t!me constra!nts. T%e process!n+ must be one *!t%!n t%e e,!ne constra!nts' or t%e system ,a!-s. T*o types$ - Hard real-time systems +uarantee t%at cr!t!ca- tas0s comp-ete on t!me. - In #oft real-time systems' a cr!t!ca- rea-1t!me tas0 +ets pr!or!ty o"er ot%er tas0s' an t%e tas0 reta!ns t%at pr!or!ty unt!- !t comp-etes. 3&2& User "nterfa#es for Operating Systems Inc-ue$ - s!mp-e comman -an+ua+e - a menu - +rap%!ca- representat!on o, pro+rams an ata - I(O rout!nes &ser"!ce rout!nes) H Operating System $eatures 6ootstrap pro+ram$ !n!t!a-!@es CPU' -oas OS 0erne- !nto memory OS *a!ts ,or e"ent' s!+na-e by an interrupt e!t%er ,rom a %ar*are or so,t*are &system call. monitor call)' !nterrupt "ector Dua- moe o, operat!on$ user mode' *ernel mode &super"!sor moe) /ar*are Protect!on &base' -!m!t re+!sters) user pro+ram 1 User pro+rams run !n user moe user pro+ram 4 5erne- ca-- , 3 ser"!ce Operat!n+ % proceure system runs !n !spatc% tab-e 0erne- moe 2 $igure 9& How a system call can be made 4"OS &6as!c Input Output System) It !s an !nter,ace bet*een t%e operat!n+ system an %ar*are. C%an+es ,rom one computer to anot%er. E:0 1 trans,ers a s!n+-e c%aracter to pr!nter 1 reas a sector ,rom !s0 /I!# routines of 'P0: 6OOT READER =6OOT SE>DS5 CONST SE>TR5 CONIN SETSEC CONOUT SETDMA >IST READ PUNC/ =RITE /OME 17 "o call a /I!# routine: :ump Tab-e$ &,oun at t%e be+!nn!n+ o, 6IOS rout!nes) 1 byte 4 bytes 66ASE :MP 6OOT :MP =6OOT :MP CONST :MP CONIN . . "o call 1/!!": CA>> 66ASEI< 11 ,& Some Operating System $un#tions ,&%& Pro#ess S#5edu+ing CPU !s ass!+ne to processes &tas0s) by t%e operat!n+ system. In a mu-t!pro+ramm!n+ system' process scheduling !s t%e mana+ement o, t%e CPU by s*!tc%!n+ contro- amon+ t%e "ar!ous processes accor!n+ to some sc%eu-!n+ po-!cy. b-oc0e *a!t!n+ ,or a*a!te e"ent some e"ent %as occurre t!me s-!ce e2p!re runn!n+ reay !spatc% <.4. .emory .anagement 2eal 0emory Rea- memory !s !"!e !nto part!t!ons' eac% process !s ass!+ne to a !,,erent part!t!on$ - E!2e part!t!ons - ?ar!ab-e part!t!ons - Re-ocatab-e part!t!ons &irtual 0emory In a "!rtua- memory system' user pro+rams are a--o*e to use a -ar+e cont!nuous "!rtua- aress space &"!rtua- memory)' *%!c% may be e"en -ar+er t%an t%e tota- amount o, t%e rea- memory a"a!-ab-e. Port!ons o, t%e "!rtua- memory are mappe to t%e rea- memory as t%ey are neee by t%e pro+ram. ,emand paging !s one o, t%e mapp!n+ met%os. T%!s mapp!n+ process !s comp-ete-y !n"!s!b-e to t%e user. 14 ?!rtua- memory Rea- memory mapp!n+ !s0 "!rtua- aress mapp!n+ rea- aress <.<. $i+e .anagement E!-e mana+ement ,unct!on o, an operat!n+ system !s an !nterme!ate sta+e bet*een t%e user pro+ram an t%e I(O super"!s!on. user pro+. recor -o+!ca- reAuest ,!-e ,!-e !n,ormat!on mana+er b-oc0 p%ys!ca- reAuest I(O super"!sor I(O !nterrupt super"!sor I(O I(O c%anne- 1< <.#. ;o8 S#5edu+ing :ob sc%eu-!n+ !s t%e tas0 o, se-ect!n+ t%e ne2t user 3ob to be+!n e2ecut!on. In a mu-t!pro+ramm!n+ en"!ronment' 3ob sc%eu-er spec!,!es t%e orer o, 3ob e2ecut!on. !nput Aueue 3ob act!"e !spatc%er CPU sc%eu-er 3obs <.B. Resour#e /++o#ation T%e resources suc% as CPU' memory' I(O c%anne-s are neee by a-- user 3obs' an t%e!r a--ocat!on !s %an-e automat!ca--y by t%e operat!n+ system. Some prob-ems to be %an-e by t%e operat!n+ system are$ - mutua- e2c-us!on - process sync%ron!@at!on' - ea-oc0 a"o!ance - secur!ty an protect!on