24
Ch. 4 Memory Ch. 4 Memory Mangement Mangement Parkinson’s law: Parkinson’s law: “Programs expand to fill “Programs expand to fill the memory available to the memory available to hold them.” hold them.”

Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

Embed Size (px)

Citation preview

Page 1: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

Ch. 4 Memory Ch. 4 Memory MangementMangement

Parkinson’s law: “Programs Parkinson’s law: “Programs expand to fill the memory expand to fill the memory available to hold them.”available to hold them.”

Page 2: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

Memory hierarchyMemory hierarchy

RegistersRegistersCacheCache

RAMRAM How can the OS help processes How can the OS help processes

share RAM?share RAM?

Hard diskHard diskCD, DVDCD, DVDTapeTape

Page 3: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

Basic memory managementBasic memory management

Types:Types:

1.1. Moves process back and forth between Moves process back and forth between main memory and disk (swapping and main memory and disk (swapping and paging).paging).

2.2. Those that do not (move processes back Those that do not (move processes back and forth).and forth).

Page 4: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

Basic memory managementBasic memory managementMonoMonoprogramming w/out swapping or programming w/out swapping or pagingpaging only 1 programs runs at a timeonly 1 programs runs at a time OS is also in memoryOS is also in memory

load load execute to completion execute to completion load next load next

Early mainframes

Embedded systems, palmtop

computersMS DOS

Page 5: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

Basic memory managementBasic memory management

MultiMultiprogramming w/ fixed partitionsprogramming w/ fixed partitions Divide memory into n fixed size partitionsDivide memory into n fixed size partitions

All the same size or some larger, some smaller?All the same size or some larger, some smaller?

Single job queue or multiple job queues?Single job queue or multiple job queues?

Page 6: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

Basic memory managementBasic memory management

MultiMultiprogramming w/ fixed partitions programming w/ fixed partitions issues:issues:

1.1. Unused partition space is wasted.Unused partition space is wasted.

2.2. Multiple queuesMultiple queues partitions (often larger) may go unusedpartitions (often larger) may go unused

3.3. Single queueSingle queue Large partitions wasted on small jobsLarge partitions wasted on small jobs Or if we favor larger jobs, smaller (often Or if we favor larger jobs, smaller (often

interactive) jobs may be starved.interactive) jobs may be starved.

Page 7: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

Modeling multiprogrammingModeling multiprogramming

Let p be the probability that a process waits on I/O.Let p be the probability that a process waits on I/O.Given n such processes, what is the probability Given n such processes, what is the probability

that all n processes are waiting on I/O at the that all n processes are waiting on I/O at the same time?same time?

pp11 * p * p22 * … * p * … * pnn = p = pnn

Therefore, for a given p and n, the CPU utilization Therefore, for a given p and n, the CPU utilization = 1-p= 1-pnn

(Assumes that all n processes are independent but that is not the case (Assumes that all n processes are independent but that is not the case for 1 CPU or if we need exclusive I/O! But we’ll employ the “ostrich for 1 CPU or if we need exclusive I/O! But we’ll employ the “ostrich algorithm and live with it!)algorithm and live with it!)

Page 8: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

CPU utilizationCPU utilization

Given that we have enough memory to Given that we have enough memory to support 10 processes and each process support 10 processes and each process spends 80% of its time doing I/O, what’s spends 80% of its time doing I/O, what’s CPU utilization?CPU utilization?

Page 9: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

CPU utilizationCPU utilization

Given that we have enough memory to Given that we have enough memory to support 10 processes and each process support 10 processes and each process spends 80% of its time doing I/O, what’s spends 80% of its time doing I/O, what’s CPU utilization?CPU utilization? Given n=10, p=0.80Given n=10, p=0.80 So CPU utilization = 1-0.8So CPU utilization = 1-0.81010

(about 0.90 or 90%)(about 0.90 or 90%)

Page 10: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

CPU utilizationCPU utilization

Page 11: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

CPU utilizationCPU utilization

Suppose we have 32MB. The OS uses Suppose we have 32MB. The OS uses 16MB. Each user program uses 4MB and 16MB. Each user program uses 4MB and has an 80% I/O wait.has an 80% I/O wait.

How many users, n, can we support in How many users, n, can we support in memory at once?memory at once?

Given the above n, what is our CPU Given the above n, what is our CPU utilization?utilization?

Page 12: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

CPU utilizationCPU utilization

Suppose we have 32MB. The OS uses 16MB. Suppose we have 32MB. The OS uses 16MB. Each user program uses 4MB and has an 80% Each user program uses 4MB and has an 80% I/O wait.I/O wait.

How many users, n, can we support in memory How many users, n, can we support in memory at once?at once? 4 = (32-16)/44 = (32-16)/4

Given the above n, what is our CPU utilization?Given the above n, what is our CPU utilization? (1-0.80(1-0.8044)=0.60 or 60%)=0.60 or 60%

What is our CPU utilization if we add 16M?What is our CPU utilization if we add 16M?

Page 13: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

CPU utilizationCPU utilization

Now we have 48MB. The OS uses 16MB. Each Now we have 48MB. The OS uses 16MB. Each user program uses 4MB and has an 80% I/O user program uses 4MB and has an 80% I/O wait.wait.How many users, n, can we support in memory How many users, n, can we support in memory at once?at once? 8 = (48-16)/48 = (48-16)/4

Given the above n, what is our CPU utilization?Given the above n, what is our CPU utilization? (1-0.80(1-0.8088)=0.83 or 83%)=0.83 or 83% So we went from 60% to 83% with 16M more.So we went from 60% to 83% with 16M more.

What is our CPU utilization if we add 16M?What is our CPU utilization if we add 16M?

Page 14: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

CPU utilizationCPU utilization

Now we have 64MB. The OS uses 16MB. Each Now we have 64MB. The OS uses 16MB. Each user program uses 4MB and has an 80% I/O user program uses 4MB and has an 80% I/O wait.wait.

How many users, n, can we support in memory How many users, n, can we support in memory at once?at once? 12 = (64-16)/412 = (64-16)/4

Given the above n, what is our CPU utilization?Given the above n, what is our CPU utilization? (1-0.80(1-0.801212)=0.93 or 93%)=0.93 or 93% So we went from 83% to 93% with 16M more.So we went from 83% to 93% with 16M more.

Page 15: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

Relocation and protectionRelocation and protection

Relocation – a program should be able to Relocation – a program should be able to execute in any partition of memory execute in any partition of memory (starting at any physical address)(starting at any physical address)Protection – a process should have Protection – a process should have read/write access to data memory, read read/write access to data memory, read access to its own code memory, read access to its own code memory, read access to some parts of the OS, and no access to some parts of the OS, and no access to other partsaccess to other partsBase/limit registers = early methodBase/limit registers = early method

Page 16: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

swappingswapping

We want more processes than memory!We want more processes than memory!

2 solutions:2 solutions:1.1. SwappingSwapping

1.1. bring in each process in its entiretybring in each process in its entirety

2.2. run it for a whilerun it for a while

3.3. put it back on diskput it back on disk

2.2. Virtual memory (paging)Virtual memory (paging)

Page 17: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

swappingswapping

Memory compaction (like disk Memory compaction (like disk fragmentation)fragmentation) memory may become fragmented into little memory may become fragmented into little

pieces so we may have to more all processes pieces so we may have to more all processes down into lowest memory.down into lowest memory.

Page 18: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

swappingswapping

Page 19: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

swappingswapping

What if the memory What if the memory needs of a process needs of a process changes over time?changes over time?

Page 20: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

swappingswapping

Memory management – How do we keep Memory management – How do we keep track of what memory is being used and track of what memory is being used and what memory is available?what memory is available?

1.1. BitmapsBitmaps

2.2. Linked listsLinked lists

Page 21: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

Swapping: memory management Swapping: memory management w/ bitmapsw/ bitmaps

Divide memory into equal size allocation Divide memory into equal size allocation units (e. g., 1K “chunks”).units (e. g., 1K “chunks”).

Bit = 0 means the chunk is free; bit = 1 Bit = 0 means the chunk is free; bit = 1 means that chunk is in use.means that chunk is in use.

Small chunks -> large bitmapSmall chunks -> large bitmap

Large chunks -> small bitmapLarge chunks -> small bitmap Large chunks -> wasteLarge chunks -> waste

Page 22: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

Swapping: memory management Swapping: memory management w/ linked listsw/ linked lists

Linked list of allocated and free memory Linked list of allocated and free memory segments.segments. Segment = memory used by process or hole Segment = memory used by process or hole

(free memory) between processes(free memory) between processes Usually sorted by addressUsually sorted by address May be implemented as one list (of both used May be implemented as one list (of both used

and free) as as two separate listsand free) as as two separate lists

Page 23: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

Swapping: memory management Swapping: memory management w/ linked listsw/ linked lists

Allocation methods:Allocation methods:1.1. First fit – simple, fast; leaves large holesFirst fit – simple, fast; leaves large holes

2.2. Next fit – continue searching from where ff Next fit – continue searching from where ff last left offlast left off

3.3. Best fit – slower than ff; wastes memory = Best fit – slower than ff; wastes memory = leaves many small, useless holesleaves many small, useless holes

4.4. Worst fit – not very goodWorst fit – not very good

5.5. Quick fit – keeps lists with common hole Quick fit – keeps lists with common hole sizessizes

Page 24: Ch. 4 Memory Mangement Parkinson’s law: “Programs expand to fill the memory available to hold them.”

Virtual memoryVirtual memory