Department f Cmputer Science University Cege Lndn 1B11 Operating Systems - 3 Memry Management and Prtectin Prf. Steve R Wibur s.wibur@cs.uc.ac.uk 1B11-3 1999 Side 1 Lecture Objectives What des the OS have t d t manage memry? Hw des it prvide required sharing when needed and yet inhibit inapprpriate access? 1B11-3 1999 Side 2 12/06/99 1
Department f Cmputer Science University Cege Lndn Prcess Life Cyce Recap T start a prgram, the OS must: Find the binary/executabe n disk Acate strage fr the cde and data Acate swap space n disk Map the pages in memry and swap space Cpy the cde int the physica pages Start executin by saving current cntext (see ater) and jumping (ading the PC) t the start address f the cde! 1B11-3 1999 Side 3 Memry Management Prbems Memry Memry Memry (2) (3) P2 (4) P2 P3 P2 P3 (1) P1 P1 P1 1B11-3 1999 Side 4 12/06/99 2
Department f Cmputer Science University Cege Lndn Memry Management Prbems - 2 (1) Prgrams may be paced in different catins each time they are aded - references t absute addresses wi be invaid (2) When memry fu and need t ad prgram need repacement picy t decide which (part f) prcess t swap ut (3) Need t prtect against prcesses damaging thers - cntainment r prtectin (4) May be enugh rm t ad prgram, but n he is big enugh - fragmentatin 1B11-3 1999 Side 5 Memry Management Sutins Reative addressing deas with (1), e.g.: Jump +50 instead f Jump 456 Lad -90 instead f Lad 20 where = address f current instructin Base and Limit registers dea with (1, 3): B and L registers set t address and size f prcess memry when it becmes current prcess Prgram cmpied as thugh starts at address 0 Prcessr adds B t addresses and checks resut nt ess than zer r >L fr every memry fetch 1B11-3 1999 Side 6 12/06/99 3
Department f Cmputer Science University Cege Lndn Memry Management Sutins - 2 Paged memry deas with (4): Expensive t shuffe up prcesses Page Tabe (mutipe base registers with fixed imit) makes a hes same size Page tabe aws fragments t be physicay separate but have gicay cntiguus addresses 1B11-3 1999 Side 7 Memry Management Sutins - 3 Repacement Picy with sme hep frm hardware deas with (2) dirty bit in page tabe indicates which pages have been written t and are thus mre expensive t swap ut use bit in page tabe set when page accessed and ceared every few miisecnds indicates thse pages which were recenty used - Principe f Lcaity suggests they are a bad chice t swap ut 1B11-3 1999 Side 8 12/06/99 4
Department f Cmputer Science University Cege Lndn Memry Management Prbems - 3 (5) Prgram t arge t fit in rea memry Memry Sutin: Use paging scheme where address space greater than memry space Sme part f prcess wi aways be n disk P3 (5) wi be swapped in when needed (after deay) 1B11-3 1999 Side 9 Virtua Memry Resut is Virtua Memry Prgrams written by human prgrammers are transated (cmpied r interpreted) int machine cde + data Machine cde is executed in a virtua address space which may prvide user with a virtua memry much arger than rea machine, abeit smewhat swer than main memry Curtesy f the OS and hardware cmbined 1B11-3 1999 Side 10 12/06/99 5
Department f Cmputer Science University Cege Lndn Mapping Prcess Fr a 4 GB virtua address space and a 1 GB physica address space 31 30 29 28.... 15 14 13 12 11 10 9.... 3 2 1 0 Virtua page number Page ffset Transatin 29 28.... 15 14 13 12 11 10 9.... 3 2 1 0 Physica page number Page ffset 1B11-3 1999 Side 11 Virtua Memry - 2 The CPU accesses memry via a memry management unit - this uses a page tabe t transate addresses the CPU it gives (virtua addresses) int physica addresses. The perating system ( kerne ) has its wn page tabe that ets it get at everything User prcesses have page tabes managed by the perating system These page tabes map virtua addresses t physica 1B11-3 1999 Side 12 12/06/99 6
Department f Cmputer Science University Cege Lndn Virtua Memry - 3 S each prgram is run apparenty in the address space 0-max address This is mapped in chunks f a page - usuay the same size as a disk bck Mapped t rea memry - r fagged if swapped ut t disk Cntext switches change the page tabes. 1B11-3 1999 Side 13 Virtua Memry - 4 Q. If yu run mre prgrams than wi fit in rea memry (easy t d) what happens? A. Paging. The mst recenty run prgrams are in memry - the dest ne has the bcks (pages) f physica memry written ut t disk, and the page tabe (in memry) changed t say that the pages are n disk - in the swap area This f curse takes time! The page tabe (kept by the OS) keeps a map f the swap area t 1B11-3 1999 Side 14 12/06/99 7
Department f Cmputer Science University Cege Lndn Virtua Address Mapping Prg A Virtua Addresses Physica Memry Addresses Prg B Virtua Addresses 0x0000 Unused Unused 0x1000 0x2000 0x3000 0x4000 0x5000 Unused Memry address space 0x6000 0x7000 N memry Virtua Address Space ::: Disk Addresses ::: 0xD000 0xE000 0xF000 1B11-3 1999 Side 15 Thrashing If there are a t f prcesses, and they d a t f I/O, and they are cnstanty being de-schedued and re-schedued, then there may be a t f paging (cpying f bcks t and frm disk/memry) This makes the system run ike a dg (purey technicay speaking, yu understand) Its caed thrashing - very itte prgress is made under such circumstances. 1B11-3 1999 Side 16 12/06/99 8
Department f Cmputer Science University Cege Lndn Prtectin Operating System prvides varius degrees f prtectin fr the user and prgrams... Safety - Ps shud nt be abe t damage thers accidentay r deiberatey Fairness - Ps shud get fair treatment based n picies in frce Integrity - basic assumptins shud nt be viated Authenticity - ensure bjects are what they purprt t be OS enhances hardware supprt, t give a smarter virtua service 1B11-3 1999 Side 17 MMU Prtectin Memry maps (page tabes) prvide basic prtectin against damage, because P can ny see rea memry that has been mapped t it Each page can be marked as: read write execute r a cmbinatin 1B11-3 1999 Side 18 12/06/99 9
Department f Cmputer Science University Cege Lndn MMU Prtectin - 2 Fr exampe: cde wud nrmay be execute-ny t prevent cpies being taken r being verwritten accidentay cnstants, incuding strings, wud be read-ny s they cannt be changed ] Cnsider x := x+1 if cnstant 1 was changed t 4!! wrking data and the stack wud be read+write 1B11-3 1999 Side 19 MMU Prtectin - 3 Tw r mre prcesses can share memry pages (nt necessariy at the same address in each P) ne might have write access, the ther read access - prducer-cnsumer prcesses e.g.. print server bth might have read-write access - appicatindefined sharing - need t carefuy cntr use (st updates?) bth might have execute access - shared ibraries Ony OS can change the page maps s it cntrs access 1B11-3 1999 Side 20 12/06/99 10
Department f Cmputer Science University Cege Lndn Sharing Pages Prcess A Physica Memry Prcess B 0x0000 0x1000 0x2000 0x3000 0x4000 0x5000 0x6000 0x7000 - Unused X X X R RW RW - Library 2 Unused Library 1 N memry X X Unused Virtua Address Space Yew pages in rea memry are mapped t different addresses in Prcesses A and B. Even thugh pages are disjint in memry they appear cnsecutive in bth prcesses 1B11-3 1999 Side 21 Kerne and User Mde Q. Hw des the OS stp user prcesses frm getting at these data structures (e.g. Page Tabes, Prcess tabes etc)? A. The prcessr can be set t kerne mde r user mde (When the machine is bted it is set t kerne mde and memry mapping is set t Virtua = Rea) The prcessr mde is changed as a side effect when the prcessr executes a trap r interrupt 1B11-3 1999 Side 22 12/06/99 11
Department f Cmputer Science University Cege Lndn Kerne and User Mde - 2 In kerne mde any instructin can be executed (OS runs in this mde) In user mde, (nrma prcesses,) I/O, hat and sme ther instructins cause a mde change t kerne mde and fr a rutine at a fixed catin t be invked This mechanism is knwn as a trap 1B11-3 1999 Side 23 Traps There are at east 3 types f trap: System cas - Specia instructins which can be used t invke OS functins e.g. create a new prcess Exceptins - these ccur when the prcessr executes an instructin which makes n sense, e.g. ] Divide by 0 ] iega instructin - undefined r nt awed in user mde ] iega memry access Interrupts - generated by I/O devices when they are dne 1B11-3 1999 Side 24 12/06/99 12
Department f Cmputer Science University Cege Lndn Cd Start ROM Btstrap OS Kerne mde Machine starts in kerne mde Memry management ff - i.e. mapped Virtua = Rea ROM btstrap executed t ad OS Page tabe set fr perating system t see whe memry OS R, X, W as apprpriate free space R, W Page Tabes Memry management switched n 1B11-3 1999 Side 25 Cd Start - 2 User mde ROM Btstrap Kerne mde Initia user prcess aded cmmand prcessr OS gin prcess, etc. PCB created User Prcess 1 PCB1 User P s initia PC, SP, page tabe pre-aded int PCB PCB added t head f Ready t Run Q Page Tabes Dispatcher invked 1B11-3 1999 Side 26 12/06/99 13
Department f Cmputer Science University Cege Lndn Cd Start - 3 ROM Btstrap Kerne mde Set up user mde page tabe fr P1 frm faked PCB OS Return t P1 causes PC t be set causes mde change t user mde User Prcess 1 User mde P1 executes PCB1 Page Tabes 1B11-3 1999 Side 27 Cd Start - 4 ROM Btstrap User mde Kerne mde P1 needs t d I/O - e.g. get user s gin name System ca fr synchrnus read Change t kerne mde OS Vatie registers saved in PCB by OS User Prcess 1 PCB1 OS sets up I/O peratin P1 status = Bcked Ide prcess (interna in OS) run unti I/O cmpete Page Tabes Try extending this fr mutipe user prcesses 1B11-3 1999 Side 28 12/06/99 14
Department f Cmputer Science University Cege Lndn Summary Memry management needed t ensure M used efficienty Page tabes prvide safe mechanism Pages can be fagged as read, write r execute r a mixture Ony OS awed t change tabes User mde ensures nrma user cannt access I/O devices r act as OS Traps prvide means f changing mde and cmmunicatin between user P and OS 1B11-3 1999 Side 29 12/06/99 15