Sie sind auf Seite 1von 37

Operating Systems Martin O Hanlon

Memory Management In a multiprogramming computer the operating system resides in part of memory and the rest is used by multiple processes. The task of subdividing the memory not used by the operating system among the various processes is called memory management.

Memory management requirements Relocation - programs not loaded into a fi ed point in memory but may reside in various areas. !rotection - each process should be protected against un"anted interference by other processes either accidentally or intentionally. #ll memory references generated by a process must be checked at run time to ensure that they only reference memory that has been allocated to that process. Sharing $ !rotection mechanisms must be fle ible enough to allo" a number of processes to access the same area of memory. %or e ample if a number of processes are e ecuting the same program& it is advantageous to allo" each program to access the same copy of the program. 'ogical Organisation $ !rograms are normally organised into modules some of "hich are non modifiable and some of "hich contain data that may be modified. Ho" does the operating system organise R#M "hich is a linear address space to reflect this logical structure. !hysical Organisation $ (omputer memory is organises into a least t"o levels ) main memory and secondary memory. The task of moving information bet"een the t"o levels of memory should be the systems responsibility *not the programmers+.

-,-

Operating Systems Martin O Hanlon Relocation Ho" a program is loaded from disk.

fig, The first step in the creation of an active process is to load a program into main memory and create a process image& as sho"n above. The actual program itself may consist of a number of compiled or assembled modules that are linked together to resolve any references bet"een modules *see %ig - belo"+. The 'oader places the program in memory. It may do so in three "ays) #bsolute 'oading Relocatable loading .ynamic run-time loading

---

Operating Systems Martin O Hanlon

%ig#bsolute 'oading) #n absolute loader re/uires that a given module is al"ays loaded into the same location in memory. Therefore any references in the program must correspond e actly to the actual memory location "hen the program is loaded. The assignment of specific address values to memory references "ithin a program can be done either by the programmer or at compile or assembly time. There are several disadvantages to the former approach. %irstly every programmer "ould have to kno" e actly ho" programs are loaded into main memory. Secondly& if any modifications are made to the program that involved insertions or deletions in the body of the program& then all addresses "ould have to be altered. %or this reason it is preferable to allo" memory references "ithin programs to be e pressed symbolically *see %ig0 belo"+. and have those symbolic references resolved at compilation or assembly. This is illustrated belo" every reference to an instruction or item of data is initially represented by a symbol. In preparing the module for input to an absolute loader& the assembler or compiler "ill convert all these references to specific address. The absolute loader "ill then load the program at the addresses specified.

-0-

,-11

Operating Systems Martin O Hanlon

%ig0

-2-

Operating Systems Martin O Hanlon

Relocatable loader) # huge disadvantage of binding references to specific addresses prior to loading is that the resulting load module can only be places in one region of main memory. #nd as normally memory is shared bet"een a number of processes it is undesirable to state in advance into "hich region of memory a particular module should be loaded. It "ould be better to make that decision at run time. 3hat "e need is a module that can be loaded into any part of available memory. To satisfy this re/uirement& the assember4compiler produces not actual main memory addresses * absolute addresses+ but addresses that are relative to some kno"n point such as the start of the program. This is sho"n above in %ig0. The start of the load module is assigned the relative address 1 and all other memory references "ithin the module are e pressed relative to the beginning of the module. 3ith all the references e pressed in relative format it is /uite easy for the loader to place the module in the desired location. If the module in to be loaded beginning at location & then the loader must simply add to each memory reference as it loads the module into memory. To assist in this task& the load module must include information that tells the loader "here the address references are and ho" they are to be interpreted & usually relative to the program origin but also possibly relative to some other point in the program such as current location.

-5-

Operating Systems Martin O Hanlon

.ynamic Run Time 'oader) The relocatble loader is an obvious advance over absolute loaders ho"ever in a multiprogramming environment it is still inade/uate. In a multiprogramming environment there is often a need to s"ap process images in and out of main memory to ma imise the use of the processor *or some other operational reason+. To implement this it "ill be necessary to reload the process in different memory locations *depending "hat is available at the time+. Thus a program once loaded may be s"apped out and reloaded at a different location. This "ould not be possible "ith the relocatable loader "here memory references are bound to absolute at initial load time. The solution to this is to defer the calculation of an absolute address until it is actually needed at run time. Ho" this "orks is that the load module is brought into main memory "ith all the memory references in relative form as in c+ *%ig0+ above. It is not until an instruction is actually e ecuted that the absolute address is calculated. To ensure that this conversion of addresses takes place as fast as possible it is done by special processor hard"are *Memory Management 6nit+. .ynamic address calculation provides complete fle ibility. # program can be loaded into any region of main memory and logical addresses converted to physical addresses on the fly. %ig2 belo" sho"s ho" the address translation is achieved. 3hen a process is assigned to the Running state& a special register called the base register is loaded "ith the starting address in main memory of the process. # bounds register "hich indicates the ending location of the program is also used. .uring the course of program e ecution& relative addresses are encountered. These include the contents of instruction registers& instruction addresses that occur in branch and call instructions and data addresses that occur in load and store instructions. 7ach such relative address goes through t"o steps of manipulation by the processor. %irst the value in the base address is added to the relative address to produce an absolute address. Second& the resulting address is compared to the value in the bounds register. If the address is "ithin bounds then the instruction e ecution proceeds. Other"ise an interrupt indicating an error is generated. This scheme allo"s programs to be s"apped in and out of memory during e ecution and also provides a measure of protection $ each process image is isolated by the contents of the base and bounds register and is safe from un"anted access by other processes.

-8-

Operating Systems Martin O Hanlon

Single User, Single process System In this system $ only one user is present and one process is running at any time. Memory management is easy $ memory is divided into t"o parts $ one for the Op system and one for the program currently being e ecuted.
Op System

6ser !rogram

Fixed Partitioning Main memory divided into a number of fi ed si9e partitions $ partitions may be the all the same si9e or une/ual si9e partitions. Internal %ragmentation %i ed :umber of processes in memory (ontiguous memory :o 'onger used

!artition 0 211k

!artition - 011k

!artition , -11k

-;-

#l"ays present in Memory

Operating Systems Martin O Hanlon

Dynamic Partitioning !artitions are created dynamically and each process is loaded into a partition of e actly the same si9e as the process. 7 ternal fragmentation !eriodic compaction of memory space needed !lacement algorithms) <est-fit& %irst-fit and :e t-fit

The !!ects o! dynamic partitioning

-=-

Operating Systems Martin O Hanlon

Placement algorithms <ecause of the overheads involved in compaction& the O! system must be clever in deciding ho" to assign processes to memory. "est #!it$ #llocated the smallest hole that is big enough. First%!it$ #llocates the first hole that is big enough& starting from the beginning of memory. &ext%!it$ Starts scan from last allocation and allocates the ne t hole big enough. .eciding "hich placement algorithm is the best depends on the si9e of the processes and the e act se/uence of process s"apping that takes place. In general & the %irst-fit usually results in the best and fastest allocation. :e t-fit usually results in the last block being allocated and is in general large in si9e. <est-fit results in compaction having to be carried out more fre/uently than the others $ because it results in lots of small fragments scattered throughout memory.

->-

Operating Systems Martin O Hanlon

The above diagram sho"s an e ample of memory configuration after a number of placement and s"apping-out operations. The last operation "as allocating a ,2 Mb process to an used -- Mb block. The left side sho"s ho" a ,8 Mb re/uest might be satisfied.

Paging Introduced to alleviate problem of fragmentation. Memory is divided into page !rames all of e/ual si9e. The logical address space divided into pages of e/ual si9e. The memory manager determines ,. The number of pages in the program -. 'ocates enough empty page frames to facilitate 0. 'oads all of the pages into memory& pages need not be contiguous. # number of tables need to be maintained for this system to operate) ,. 'ob Table- for each ?ob holds the si9e of the ?ob& the memory location of the !age table. -. Page Table $ %or each active ?ob the !age & page %rame memory address 0. Memory Map table $ for each page %rame its location and "hether free or busy. (ogical )ddress Is a reference to a memory location independent of the current assignment of data to memory $ a translation must be made to a physical address before memory access can be achieved. * Mov Reg,8192+ Physical )ddress Is an actual location in main memory

Page addressing Memory address translation takes place at run time. Reading a "ord from memory involves translating a virtual or logical address& consisting of a page number and offset& into an actual physical address& consisting of a frame number and offset. This process "ill make use of the !age Table entries. 'ogical address
!age :o Offset

!hysical address
%rame :o Offset

- ,1 -

Operating Systems Martin O Hanlon

If the system used ,8 bits then it could utilise memory in this fashion) ,5
11,1,

,1
11111,1,1,1

1 .isplacement

!age no

"ith this set up the system "ould have 0- pages *-5 + each "ith -12= bytes *- ,,+ 7 ample If a logical address of 11,1,11111,1,1,1 "as encountered this "ill represent offset 2- on page 5. The !age table "ould be accessed to see the Mapping of page 5. logical address
11,1, 11111,1,1 ,1 ,1,,, 11111,1,1

!hysical address
1,,,, 11,1, 1111, 111,, ,1,,,

!age Table

Static paging) :o e ternal fragmentation %i ed si9e pages Internal fragmentation $ only on last page :on- contiguous memory *page table+

- ,, -

Operating Systems Martin O Hanlon

- ,- -

Operating Systems Martin O Hanlon !aging 'ets say memory is only ,1-2 bytes @ -,1 address locations. # page frame si9e is -58 byte -= . 'ogical address page1 11&,,,,&,,,, 1,&1111&1111 page, 1,&,,,,&,,,, ,1&1111&1111 page,1&,,,,&,,,, ,,&1111&1111 page0 ,,&,,,,&,,,, !age Table 1, ,, 11 ,1 "here is logical address 11&1111&111,1 1111&11,1 frame, "here is ,,&1111&,,,, ,,,,&,,,, 1111&,,,, frame1111&1111 frame1 ,,,,&,,,, 1111&1111 11&1111&1111

frame0

- ,0 -

Operating Systems Martin O Hanlon

Segmentation *static+

The concept of segmentation is based on the common practice by programmers of structuring their programs in modules-logical groupings of code. 3ith segmented memory allocation& each ?ob is divided into several segments of different si9es& one for each module "hich contains pieces that perform related functions. # subroutine is an e ample of one such logical group. This is fundamentally different from a paging scheme& "hich divides the ?ob into several pages all of the same si9e each of "hich often contains pieces from more than one program module. # second important difference is that main memory is no longer divided into page frames because the si9e of each segment is different - some are large and some are small. Therefore& as "ith the dynamic partitions & memory is allocated in a dynamic manner. 3hen a program is compiled or assembled& the segments are set up according to the programAs structural modules. 7ach segment is numbered and a Segment Map Table *SMT+ is generated for each ?ob - it contains the segment numbers& their lengths& and *"hen each is loaded into memory+ its location in memory.

- ,2 -

Operating Systems Martin O Hanlon

The system maintains three tables) ,. The ?ob table *as "ith static paging+ -. The segment Map table list details about each ?ob *one for each ?ob+ 0. Memory Map Table *as before+.

'ike paging& the instructions "ithin each segment are ordered se/uentially& but the segments need not be contiguous in memory. 3e only need to kno" "here each segment is stored. The contents of the segments themselves are contiguous *in this scheme+. To

- ,5 -

Operating Systems Martin O Hanlon

access a specific location "ithin a segment "e can perform an operation similar to the one used "ith paged memory management. The only difference is that "e "ork "ith segments instead of pages. The addressing scheme re/uires the segment number and the displacement "ithin that segment& and& because the segments are of different si9es& the displacement must be verified to make sure it isnAt outside of the segmentAs range.

# segmented address reference re/uires the follo"ing steps) ,. e tract the segment number and the displacement from the logical address -. use the segment number to inde the segment table& to obtain the segment base address and length. 0. check that the offset is not greater than the given lengthB if so an invalid address is signaled. 2. generate the re/uired physical address by adding the offset to the base address.

Main !oints) The benefits of segmentation include modularity of programs and sharing and protection. There is a ma imum segment si9e that the programmer must be a"are of. :o internal fragmentation 6ne/ual si9ed segments :on $contiguous memory.

- ,8 -

Operating Systems Martin O Hanlon

Some e ternal fragmentation.

Segmentation greatly facilitates the sharing of procedures or data bet"een a number of processes. <ecause a segment normally has either program code or data "ithin them different segments "ithin the same process can have different protections set for them. 3hile protection is possible in a paging environment it is far more difficult to implement& the programmer is una"are of "hat pages hold a procedure or data area that he may "ish to share& to do this the programmer "ould have to keep track of the e act pages that held a procedure and then assign the necessary protection to those pages. ,irtual Memory% Demand Paging Demand paging introduces the concept of loading only a part of the program into memory for processing. When a process begins to run, pages are brought into memory only as they are needed. It "as the first "idely used scheme that removed the restriction of having the entire ?ob in memory from the beginning to the end of its processing. 3ith demand paging& ?obs are still divided into e/ually si9ed pages that initially reside in secondary storage. 3hen the ?ob begins to run& its pages are brought into memory only as they are needed. .emand paging takes advantage of the fact that programs are "ritten se/uentially so that "hile one section& or module& is being processed all of the other modules are idle. .emand paging allo"s users to run processes "ith less main memory than "ould be re/uired if the O! system "as using any of the schemes described earlier. .emand paging can give the appearance of almost infinite amount of physical memory. 3ith a virtual memory system& the logical address space available to a program is totally independent of the amount of physical memory space. %or instance a program may have a logical address space of --1 bytes and be capable of running in a system "ith only - ,8 bytes of physical memory. Three tables used in demand paging) ob !able "age Map !able $part or all loaded into memory depending on si9e of virtual address space of process. %or e ample in Ca architecture a process can have - 0@- Db of virtual address space. If each page "as - > @5,- <ytes then there "ould need to be --- entries in the page table per process $most systems store page tables in virtual memory rather than real memory. Memory Map !able !age Table Three e tra bits are re/uired in the page table for demand paging.

- ,; -

Operating Systems Martin O Hanlon

page table present in memory *! bit+ "hether the page has been modified or not * M bit+ if the page has been referenced recently * R bit+

,irtual-(ogical address
!age :umber Offset

Page Table ntry


! M ! R Other (ontrol <its %rame number

Page Fault 3hen a program tries to use a page "hich is not memory& a page fault occurs. This is one of the reasons for a process to be suspended for I4O. #lgorithm ,. <ind virtual address to physical address -. If the address is illegal terminate the process 0. %ind page table containing address *look up page table+ 2. If the page table is in memory get data and finish instruction advance to ne t instruction return to step , else generate a page interrupt call page interrupt handler 5. The page interrupt handler takes the follo"ing action %ind a free frame in memory * memory map table+ if there is no free memory frame then ) Select a page to be s"apped out using "age Replacement algorithm 6pdate processEs page table if contents of page have been modified then "rit page to disk endif 8. load the re/uest page into freed frame ;. update page table =. update memory map table 3hen an e ecutable file is first used to create a ne" process& only a portion of the program and data for that file may be loaded into real memory. 'ater as page faults occur& ne" portions of the program and data are loaded. #t is only at the time of first loading that virtual memory pages are created and assigned to locations on one of the devices $hard dis%s& used for s'apping.

- ,= -

Operating Systems Martin O Hanlon

(ocality o! Re!erence 3ell structured programs obey the principle of locality of reference. 3hat this means is that the address references generated by a process tend to cluster "ithin narro" ranges "hich are likely to be contained in a fe" pages. The implications of this is that the needs of a process over a period of time can be satisfied by having a small number of resident pages *not necessarily ad?acent+. This situation leads to a relatively small number of page faults. The set of pages that a process is currently using& at this stage is called its .or/ing set. If the entire "orking set is in memory& the process "ill run "ithout incurring many page faults until it moves into another e ecution phase. #t different stages of the processes e ecution there "ill be different "orking sets in memory. If the available memory is to small to hold the "orking set& the process "ill run slo"ly as it incurs many page faults. It is possible that a situation can arise "here a page s"apped out of memory can be needed almost immediately $ this can lead to a condition called thrashing & "here the processor spends most of its time s"apping pages rather than e ecuting instructions. In a multiprogramming system process are often moved to disk to let other processes have a turn on the (!6. 3hen the process is again scheduled for running a /uestion arises as to "hat pages to bring into memory. Many systems try to keep track of the "orking set of each process& in order that all the pages belonging to the set can be brought in their entirety. This eliminates the numerous costly page faults that "ould occur. <ecause the "orking set varies slo"ly "ith time it is possible to make a reasonable assumption as to "hich pages "ill be needed "hen the program is restarted on the basis of its "orking set "hen it "as suspended.

- ,> -

Operating Systems Martin O Hanlon

Multi%(e0el Page Table Moti0ation$ The problem "ith page tables "as that they could take up too much memory to store them. (onsider a process on a system "ith a 0- bit virtual address spaceF It "ill be laid out as follo"s in its virtual space)

The program al"ays starts at virtual address 1. The heap al"ays starts on then first page boundary after the end of the program. The stack al"ays starts at the 2 D< mark and "orks its "ay do"n. So ho. big is the gap in most programs1 #t is huge. 2o. many 3/b pages are being used in the abo0e process1 1( M) * ( +b , 1( - 2.2/ * ( - 2.1/ , 0.1 - 2 .1/ $appro2 (-2.1/ , (M)& 4hat does that say about most o! the 556 page table entries !or this process1 !hey 'ill be empty. So do .e need to /eep the empty page table entries in memory1 3o, they 'ill never be used so 'hy %eep them in memory. 4ets 5ust %eep the one6s 'e need. Multi%(e0el Page Table 7mplementation$

- -1 -

Operating Systems Martin O Hanlon # multi-level page table system *on a -1 bit system "ith 2kb pages+ "orks as follo"s)

The Cirtual address is no" divided slightly differently. There are t"o page table inde es The first page inde is used to inde the outer page table. 7ach entry in the outer page table can possibly point to a second level page table. The present bit in the outer page table is used to indicate if there is a second level page table for this entry. The second page inde is used to inde the second level page table pointed to by the outer page table. The entry of the second level page table might contain the page frame number of the desired page. If the present bit of the second level page table indicates the presence or absence of a page in the physical memory. If the present bit is a , then the page frame number is contained in the entry. If the present bit is a 1 then a page fault occurs and the virtual page must be brought into the physical memory. In a 0- bit system the virtual address might be broken up as follo"s)

- -, -

Operating Systems Martin O Hanlon

4hat si8e are the pages in this system1 !hey 'ould be (+b because the offset has 12 bits. 4hat is the si8e o! each second le0el page table1 7ach second level page table 'ill have 1/2( entries because the second #nde2 is 1/ bits. #f each entry is ( bytes as 'e assumed previously that ma%es each table (%b. interesting that the second level page tables are e actly the same si9e as a page. is it a coincidence or "as it done on purposeG 4hat is the si8e o! the outer page table. !he same si8e for the same reasons. 2o. much memory is represented by each entry in a second le0el page table1 7ach entry points to a single page that is (%b. 2o. much memory does each second le0el page table represent1 7ach table points to up to 1/2( pages of (%b each so ( M). 2o. many entries in the outer page table .ould be used in the example process !rom page 91 !he program is 8 M) so it needs 2 entries, the heap is ( M) so that is another entry and the stac% is 2 M) 'hich needs a full entry. 4hat about the second le0el page table !or the outer page table entry !or the stac/, is it !ull1 3o it 'ill be half empty. So by using a - level page table& for our e ample& "e only need to keep 5 sub-tables in memory. The outer page table. 2 inner page tables. Therefore& "hile this e ample "ould have re/uired 2 M< of page table space for a single level page table it only re/uires -1 H< "hen a - level page table is used. 4hat is the maximum amount o! memory that the t.o le0el page table could ta/e up1 ( +b more than the single level page table. #f every second level page table is full then there 'ill be ( M) 'orth of second level page tables and one outer page table. So in that "orst case scenario the t"o level page table doesnAt help. 'uckily programs that are that large are very rare so most of the time using a t"o level page table is very helpful. It is also possible to use 0 or more level page tables. The :ther Problem 4ith Page Tables$

- -- -

Operating Systems Martin O Hanlon So paging solved the problem of not being able to run enough processes at a given time or not being able to run really large processes. Multi-level page tables solved the problem of the page table taking up too much memory. 'etAs consider the follo"ing instruction on a machine that doesnAt use any virtual memory) 'O#. <I& -11 This instruction gets "hat is in memory address -11 and puts it into the <I register.

2o. many memory re!erences does it ta/e to execute this instruction1 ust one. #t accesses location 2// to get the data and then puts that value into the register )9. Of course the instruction had to be loaded from memory before it could be e ecuted so "e should probably count that also. Therefore& it takes - references. One to fetch the instruction. One to fetch the data from location -11. 2o. many memory re!erences .ould that same instruction ta/e on a machine .ith a single le0el page table1 *7ncluding !etching the instruction.+ :our; 'ook up the physical address of the instruction in the page table. %etch the instruction from that physical memory location. 'ook up the physical address of the data in the page table. I.e. Translate the virtual address -11 to a physical address. %etch the data from the memory. 2o. many memory re!erences .ould that same instruction ta/e on a machine .ith a t.o le0el page table1 <i2 'ook up the second level page table for the instruction in the outer page table. 'ook up the physical address of the instruction in the second level page table. %etch the instruction from that physical memory location. 'ook up the second level page table for the data in the outer page table. 'ook up the physical address of the data in the second level page table. I.e. Translate the virtual address -11 to a physical address. %etch the data from the memory.

- -0 -

Operating Systems Martin O Hanlon Memory accesses take time. # single level page table makes the machine t"ice as slo" as it "ould be "ithout the virtual memory. # t"o level page table makes the machine three times as slo" as it "ould be "ithout the virtual memory. This is a big problem and no one .ould use ,M i! it couldn;t be !ixed. Translation (oo/%)side "u!!ers$ The follo"ing sho" the use of a translation look-aside buffer.

The Translation 'ook-#side <uffer *T'<+ is a small amount of associative memory *,8& 0- or 82 "ords+. This memory very fast& very e pensive and resides on the (!6 chip so that no main memory references are needed to access its contents.

- -2 -

Operating Systems Martin O Hanlon The left column of the associative memory is the key value. It holds the virtual page number. The right column is the value of the associated memory cell and holds the physical page frame that contains the virtual page. 3hen the associative memory is presented "ith a key value it looks at all of the cells in the memory simultaneously for the key value. If the key value is found the value in the associated memory cell is output. 3hen a memory re/uest is made the page indices are sent simultaneously to the page translation system *could be a single level or a multi-level paging system+ and to the Translation 'ook-#side buffer *T'<+. The paging system begins its look up process. The T'< also searches for the page inde in its associative memory. If the page inde is found in the T'< then the physical page frame that is returned by the T'< is used. 3hen the page frame is found in the T'< the lookup by the paging system is canceled. If the page inde is not found in the T'< then the paging system continues its lookup process and finds the page frame in the same "ay that "as discussed previously. If the page inde "as not in the T'< it and the associated page frame are put into the T'< after the paging system finishes the lookup. This may involve removing one of the page indices that is currently in the T'< to make room for the ne" one. If a page inde is removed from the T'< it might be the one that has not been used for the longest period of time. *There are lots of different algorithms for picking the page inde to remove - these are very similar to the page replacement algorithms that "e "ill see in the ne t class.+ Does the T(" ma/e a signi!icant impact on the per!ormance o! the system1 It might appear not= (onsider a 2 M< process on a system "ith 2H< pages. That process "ill have ,111 pages. If the T'< has 0- entries then only 0- of ,111 pages "ill be in the T'<. Therefore& one might think that only 0-4,111 page re/uests "ould be found in the T'<. That might be true if the process "as ?ust making random memory re/uests. 'uckily& most processes do not behave that "ay. 'ocality of Reference) 'ocality of reference basically says that most processes "ill make a large number of references to a small number of pages as opposed to a small number of references to a large number of pages. 'ocality of reference can be summari9ed by the >14,1 rule of thumb. # process spends >1J of its time in ,1J of its code. It is not clear that that also holds for a programAs data. *<ut "e "ill ignore that for no".+

- -5 -

Operating Systems Martin O Hanlon

4hat does that do !or us as !ar as the T(" goes1 Well, given that about 9/> of the references 'ill be to 1/> of the pages 'e 'ould do very 'ell 'ith the !4) if it could hold those 1/> of the pages that are used most often. ,1J of ,111 is ,11 so "e canAt /uite hold all ,11 page translations in the T'<. Ho"ever& holding the 0- most often used pages "ill ensure that most of the page re/uests can be handled by the T'<. Most machines that have Cirtual Memory use some combination of multi-level paging "ith a T'<. Intel !entium& Macintosh& Sun Sparc& .7( #lpha.

# flo"chart that sho"s the use of the T'< is sho"n above. :ote that by the principle of locality& it is hoped that most page references "ill involve page table entries in the cache.

- -8 -

Operating Systems Martin O Hanlon

- -; -

Operating Systems Martin O Hanlon Page Si8e The page si9e of a system has a significant impact on overall performance. Internal fragmentation $ smaller page si9e less internal fragmentation less internal fragmentation better use of main memory. Smaller page si9e $ more pages per process& larger page tables. In multiprogramming environments this means page tables may be in virtual memory $ this may mean a double page fault one to bring in re/uired portion of page table one to bring the re/uired process page. The graph belo" sho"s the effect on page faults of t"o variables one the page si9e and the second the number of frames allocated to a process.

The leftmost graph sho"s that as page si9e increases that number of page fault correspondingly increase. This is because the principle of locality of reference is "eakened. 7ventually as the page si9e approaches the si9e of the process the faults begin to decrease. On the right the graph sho"s that for fi ed page si9e the faults decrease as the number of pages in memory gro"s. Thus& a soft"are policy *the amount of memory to allocated to each process+ affects a hard"are design decision *page si9e+. Of course the actual si9e of physical si9e of memory is important. More memory should reduce page faults. Ho"ever as main memory is gro"ing the address space used by applications is also gro"ing reducing performance and modern programming techni/ues such as Ob?ect-Oriented programming *"hich encourages the use of many small program and data modules "ith references scattered over a large number of ob?ects+ reduce the locality of reference "ithin a process. # small page si9e reduces internal fragmentation. # large page si9e reduces the number of pages needed& thereby reducing the si9e of the page table *page table takes up less memory+. # large page si9e reduces the overhead in s"apping pages in or out. In addition to the processing time re/uired to a handle a page fault & transferring - ,H blocks of data from disk is almost t"ice as long as transferring , -H block .

- -= -

Operating Systems Martin O Hanlon

# smaller page si9e& "ith its finer resolution& is better able to target the processEs locality of references. This reduces the amount of unused information copied back and forth bet"een memory and s"apping storage. It also reduces the amount of unused information stored in main memory& making more memory available for useful purposes.

Page Replacement 3hen a page fault occurs and all frames are occupied a decision must be made as to "hat page to s"ap out. The page replacement algorithm is used to choose "hat page should be s"apped. :ote that if there is no !ree !rame a busy one must be s"apped out and the ne" one s"apped in. This means a double transfer $ "hich takes time. Ho"ever if "e can find a busy page that has not been modified since it "as loaded to disk then it does not need to be "ritten back out. The modi!ied bit in the page table entry indicates "hether a page has been modified or not. Thus the I4O overhead is cut by half if the page is unmodified. Page Replacement )lgorithm :ptimal Replacement The best policy is one "hich s"aps out a page that "ill not be used for the longest period of time. This "ill give the lo"est possible page fault rate for a fi ed number of frames. (onsider the follo"ing string of pages "hich are to be processed. ;&1&,&-&1&0&1&2&-&0&1&0&-&,&-&1&,&;&1&, If "e assume an address space of three frames& the pattern of page faults is sho"n belo". ; 1 , -1 pages to be processed 1 0 1 2 0 1 0 , 1 , ; 1 ,

!age %rames

Optimal page replacement 6nfortunately the optimum scheme is impossible to implement as "e need to kno" in advance "hat pages "ill be demanded. Ho"ever it can be used as a standard to ?udge other schemes by. First in First out %i%o uses the oldest page first . 3e do not need to kno" ho" long it has been in memory if the pages are placed in a %I%I /ueue. This is easy to implement but suffers from being to simple and does not al"ays give good results.

- -> -

Operating Systems Martin O Hanlon

(east Recently Used #n alternative scheme is to replace 'east recently 6sed pages. It replaces the page that has not been used for the longest period of time. To see ho" this "orks "e use the same string ; 1 , - 1 0 1 2 as before. #ssume that page 2 is re/uested. #t this stage& pages 1&-&0 are in memory. <y scanning back"ards& "e see that of the three frames in memory page - is the least recently used. The frame holding this page is the one that "ill be sacrificed to make "ay for page 2.

<y the principle of locality the 'R6 policy should and does "ork "ell. Ho"ever it is difficult to implement. 7ach page "ould need a tag "ith the time it "as last referenced and even if the hard"are supported such a scheme the overheads "ould be tremendous. &ot Recently Used*:R6+is an appro imation of (RU. 3ith this policy each page table entry contains a modified bit and a reference bit as sho"n above. 3hen a page is loaded& the operating system sets the reference bit to ,. !eriodically the operating system "ill reset the reference bit to 1. 3hen a page is accessed the bit is set to ,. 3hen selecting a page for replacement the system "ill use t"o criteria) the first priority is to select a page "ith a reference bit of 1& "hich indicates that it has not been accessed "ithin a period of time& the second priority is to select a page "hose modified bit is 1 *indicating that it has not been modified "hile in memory+.

- 01 -

Operating Systems Martin O Hanlon #nother appro imation of 'R6 is called aging. #ging is achieved by a adding an e tra byte to a page table entry. 3hen a page is accessed& the hard"are sets the most significant bit in the reference byte. !eriodically& as "ith &RU above& the contents of this byte is ad?usted by shifting all the bits right. # page "ith the lo"est value in its reference byte is selected for removal. #s "ith :R6& the operating system may pick any page from among all the pages "ith the lo"est value. Over the years designers have tried to implement algorithms that appro imate to the performance of 'R6 but "ithout the overheads. Many of these are referred to as the cloc/ policy.

The simplest form of clock policy re/uires the association of an additional bit "ith each frame& referred to as the reference bit. 3hen a page is first loaded into a frame in memory& the reference bit for that frame is set to ,. 3hen the page is subse/uently referenced *after the reference that generated the page fault+& its reference bit is set to ,. %or , the page replacement algorithm& the set of frames that are candidates for replacement is considered to be a circular buffer& "ith "hich a pointer is associated. 3hen a page is replaced& the pointer is set to indicate the ne t frame in the buffer. 3hen it comes time to replace a page& the operating system scans the buffer to find a frame "ith a use bit set to 9ero. 7ach time it encounters a frame "ith a use bit of ,& it resets that bit to 9ero. If any of the frames in the buffer have a use bit of 9ero at the beginning of this process& the first such frame encountered is chosen for replacement. If all of the frames have a use bit of ,& then the pointer "ill make one complete cycle through the buffer& set ting all the use bits to 9ero& and stop at its original position& replacing the page in that frame. 3e can see that this policy is similar to %I%O& e cept that& in the clock policy& any frame "ith a use bit of , is passed over by the algorithm. The policy is referred to as a clock policy because "e can visuali9e the page frames as laid out in a circle. # number of operating systems have employed some variation of this simple clock policy *for e ample& Multics+. The figure belo" provides an e ample of the simple clock policy mechanism. # circular buffer of n? , main memory frames is available for page replacement. Kust prior to the replacement of a page from the buffer "ith incoming page ;-;& the ne t frame pointer points at frame -& "hich contains page 25. The clock policy is no" e ecuted. <ecause the use bit for page 25 in frame - is e/ual to ,& this page is not replaced. Instead& the use bit is set to 9ero and the pointer advances. Similarly& page ,>, in frame 0 is not replacedB its use bit is set to 9ero and the pointer advances. In the ne t frame& frame 2& the use bit is set to O. Therefore& page 558 is replaced "ith page ;-;. The use bit is set to , for this frame and the pointer advances to frame 5& completing the page replacement procedure.

- 0, -

Operating Systems Martin O Hanlon

- 0- -

Operating Systems Martin O Hanlon

,irtual Segmented systems In simple segmented systems previously considered& all the segments of an active process "ere resident in real memory. In a virtual segmented scheme& the segments of a process are loaded independently and hence may be stored in any available memory positions or may not be in memory at all Cirtual segmented systems have certain advantages) facilitates the handling of gro"ing data structures. 3ith segmented virtual memory& the data structure can be assigned its o"n segment and the O! system "ill e pand and shrink the segment as needed. It lends itself to protection and sharing. logical structure of the process is reflected in the physical structure "hich reinforces the principle of locality.

The segmented address translation process sho"n belo"& is similar to the simple segmentation scheme& e cept that& firstly& the segment table entry& usually called a segment descriptor, must no" have a number of additional bits *see belo"+ and secondly& the logical address is no" a virtual address typically much larger than the real address. The diagram also sho"s the presence of a segment table register "hich points to the start of the segment table for he current process. The segmentation virtual address consists of t"o components& $s, d+. The value inde es the segment table for the appropriate descriptor& "hile the d value gives the displacement "ithin the segment. The typical contents of a segment descriptor are listed belo") base address of segment segment limitB ie si9e of segment& used to detect addressing errors. segment in memory bitB indicates "hether segment is currently in real memory segment used bitB indicates "hether segment has been accessed since previous reset of this bit. 6sed to monitor usage of segment. protection bitsB specify read& "rite and e ecution access of segment.

- 00 -

Operating Systems Martin O Hanlon

<ombined Paging and segmentation The respective merits of paging and segmentation can be combined into a paged-segmented system. Segmentation is visible to the programmer and meets many of his re/uirements. !aging provides transparent memory management "hich eliminates "asted space and facilitates comple optimi9ation techni/ues. # combined paged4segmented system appears to the programmer to be a simple segmented systemB ho"ever& the segments defined are subdivide at the physical level into a number off fi ed length pages. !ages do not bridge segmentsB a segment smaller than a page "ill use a "hole page& and typically one page "ithin a segment "ill be partially unfilled. The diagram belo" sho"s ho" a process could be divided into paged segments.

- 02 -

Operating Systems Martin O Hanlon

#ddress translation in a paged segmented system combines the translations of the individual paging and segmentation systems. # memory reference is specified by a three element value such as) *s& p, d& 'here@ s is the segment number p is the page "ithin this segment d is the displacement "ithin this page

The figure belo" sho"s the complete translation process. The segment number& S& is used as an inde to a segment table& as before. The segment descriptor& among other things& contains a pointer to a page table for that segmentB note that each segment is treated as an independent address space& each "ith its o"n page table. The page number& p, is used to inde this page table "hich translates the virtual page number p to a physical page number. The displacement d is then used to locate the e act address "ithin this physical page frame. # paged-segmented system is clearly /uite comple & but provides a po"erful environment for modem computers& giving the programmer control over process structure "hile efficiently managing memory space. #mong current systems using such a scheme are Microsoft 3indo"s& OS4- and I<M MCS47S#.

- 05 -

Operating Systems Martin O Hanlon

Segmentation lends itself to the implementation of protection and sharing policies. To achieve sharing& it is possible for a segment to be references in the segment tables of more than one process. This mechanism is available in a paging system. Ho"ever& in this case the page structure of programs and data is not visible to the programmer& making the specification of protection and sharing more difficult to implement

- 08 -

Operating Systems Martin O Hanlon ,irtual memory ad0antages # ?obEs si9e is no longer restricted to the si9e of main memory *or the free space "ithin main memory+. Memory is used more efficiently because the only sections of a ?ob stored in memory are those needed immediately "hile those not needed remain in secondary storage. It allo"s unlimited amounts of multiprogramming. It eliminates e ternal fragmentation and minimi9es internal fragmentation by combined segmentation and paging. It allo"s for sharing of code and data

These far out"eigh the disadvantages) Increased hard"are costs Increased overheads for handling paging interrupts. Increased soft"are comple ity to prevent thrashing.

- 0; -

Das könnte Ihnen auch gefallen