Tải bản đầy đủ (.pdf) (17 trang)

Thiết kế và lập trình hệ thống - Chương 26

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (354.05 KB, 17 trang )

Systems Design & Programming OS Essentials CMPE 310
1 (Feb. 3, 2002)
UMBC
U M B C
U
N
I
V
E
R
S
I
T
Y


O
F


M
A
R
Y
L
A
N
D


B


A
L
T
I
M
O
R
E


C
O
U
N
T
Y
1

9

6

6
Processes and Tasks
What comprises the state of a running program (a process or task)?
If a second process, P2, is to be created and run (not shown), then the state of
P1 must be saved so it can be later resumed with no side-effects.
Since only one copy of the registers exist, they must be saved in memory.
We’ll see there is hardware support for doing this on the Pentium later.
Microprocessor

DRAM
Address bus
Data bus
Control
P1 stack
P1 Code
P1 Data
P1’s state
by the
register values, OS data structures,
and the process’s
data and stack segments.
OS code
and data
The STATE of a task or process is given
EAX
EBX
ECX
EDX
EBP
ESP
EDI
ESI
EIP
EFlags
CS
SS
DS
ES
FS

GS
...
code/data cache
special caches
Systems Design & Programming OS Essentials CMPE 310
2 (Feb. 3, 2002)
UMBC
U M B C
U
N
I
V
E
R
S
I
T
Y


O
F


M
A
R
Y
L
A

N
D


B
A
L
T
I
M
O
R
E


C
O
U
N
T
Y
1

9

6

6
Memory Hierarchy
For now, let’s focus on the organization and management of memory.

Ideally, programmers would like a fast, infinitely large nonvolatile memory.
In reality, computers have a memory hierarchy:
Cache (SRAMS): Small (KBytes), expensive, volatile and very fast (<
5ns).
Main Memory (DRAM): Larger (MBytes), medium-priced, volatile and
medium-speed (<80ns).
Disk: GBytes, low-priced, non-volatile and slow (ms).
Therefore, the OS is charged with managing these limited resources and cre-
ating the illusion of a fast, infinitely large main memory.
The Memory Manager portion of the OS:
• Tracks memory usage.
• Allocates/Deallocates memory.
• Implements virtual memory.
Systems Design & Programming OS Essentials CMPE 310
3 (Feb. 3, 2002)
UMBC
U M B C
U
N
I
V
E
R
S
I
T
Y


O

F


M
A
R
Y
L
A
N
D


B
A
L
T
I
M
O
R
E


C
O
U
N
T
Y

1

9

6

6
Simple Memory Management
In a multiprogramming environment, a simple memory management scheme
is to divide up memory into n (possibly unequal) fi xed-sized partitions.
These partitions are defi ned at system start-up and can be used to store all
the segments of the process (e.g., code, data and stack).
Advantage: it’s simple to implement.
However, it utilizes memory poorly. Also, in time sharing systems, queueing
up jobs in this manner leads to unacceptable response time for user pro-
cesses.
Partition 4
Partition 3
Partition 2
Partition 1
OS
Multiple
Job Queues
Systems Design & Programming OS Essentials CMPE 310
4 (Feb. 3, 2002)
UMBC
U M B C
U
N
I

V
E
R
S
I
T
Y


O
F


M
A
R
Y
L
A
N
D


B
A
L
T
I
M
O

R
E


C
O
U
N
T
Y
1

9

6

6
Variable-Sized Partitions
In a variable-sized partition scheme, the number, location and size of memory
partitions vary dynamically:
(1) Initially, process A is in memory.
(2) Then B and C are created.
(3) A terminates.
(4) D is created, B terminates.
A
OS
A
OS
B
OS

D
OS
D
OS
B
C C
B
C C
(1)
(2)
(3)
X1
X2
Time
(4) (5)
Systems Design & Programming OS Essentials CMPE 310
5 (Feb. 3, 2002)
UMBC
U M B C
U
N
I
V
E
R
S
I
T
Y



O
F


M
A
R
Y
L
A
N
D


B
A
L
T
I
M
O
R
E


C
O
U
N

T
Y
1

9

6

6
Variable-Sized Partitions
Problem: Dynamic partition size improves memory utilization but compli-
cates allocation and deallocation by creating holes (external fragmentation).
This may prevent a process from running that could otherwise run if the
holes were merged, e.g., combining X1 and X2 in previous slide.
Memory compaction is a solution but is rarely used because of the CPU time
involved.
Also, the size of a process’s data segments can change dynamically, e.g. mal-
loc().
If a process does not have room to grow, it needs to be moved or killed.
code
OS
stack
data
Growth
Processes
Other
Process
A
Systems Design & Programming OS Essentials CMPE 310
6 (Feb. 3, 2002)

UMBC
U M B C
U
N
I
V
E
R
S
I
T
Y


O
F


M
A
R
Y
L
A
N
D


B
A

L
T
I
M
O
R
E


C
O
U
N
T
Y
1

9

6

6
Implementing Memory on the Hard Drive
The hard disk can be used to allow more processes to run than would nor-
mally fi t in main memory.
For example, when a process blocks for I/O (e.g. keyboard input), it can be
swapped out to disk, allowing other processes to run.
The movement of whole processes to and from disk is called swapping.
The disk can be used to implement a second scheme, virtual memory.
Virtual memory allows processes to run even when their total size (code,

data and stack) exceeds the amount of physical memory (installed
DRAM).
This is very common, for example, in microprocessors with 32-bit
address spaces.
If an OS supports virtual memory, it allows for the execution of processes
that are only partially present in main memory.
OS keeps the parts of the process that are currently in use in main mem-
ory and the rest of the process on disk.

×