Professional Documents
Culture Documents
Unix Internals: Ms. Radha Senthilkumar, Lecturer Department of IT MIT, Chromepet Anna University, Chennai
Unix Internals: Ms. Radha Senthilkumar, Lecturer Department of IT MIT, Chromepet Anna University, Chennai
1
MEMORY MANAGEMENT
POLICIES
2
Table of Contents
SWAPPING
DEMAND PAGING
Data
Structure Map(in core table) Linked List
Catalog of free blocks
Free space
1 10000
Initial Swap Map
(a) (b)
Allocate 50 units
(d) (c)
Address Units
1 150 100 units free at 1
251 9750
(c)
Text 0 278K
Swap Device
1k 432K 684
empty
.
.
.
.
Data 64K 573K
65K 647K
66K 595K
690
empty
.
.
.
Stack 128K 401K
empty
CS502 / IT524 OVERVIEW 11
Swapping a Process into memory
Layout of Virtual Address
Virtual, Physical Address
Text 0 401K
Swap Device
1k 370K 684
empty
.
.
.
.
Data 64K 788K
65K 492K
66K 647K
690
empty
.
.
.
Stack 128K 955K
empty
CS502 / IT524 OVERVIEW 12
Fork Swap
There is not enough memory when fork()
called.
Swaps the process out without freeing
the memory occupied by the in-core
copy.
When the swap is complete, the child process exists
on the swap device.
parent places the child in the “ready-to-run” state
and returns to user mode.
Swap in when kernel schedule it
empty
CS502 / IT524 OVERVIEW 15
Swapping Process in
Process 0, the swapper is the only process
that swaps process into memory from swap
device.
At the conclusion of system initialization,
swapper goes into an infinite loop.
Swapper swaps process in or out ,unable to
work sleeps.
Kernel periodically wakes swapper up.
runs 0 0 0
1
1
1 1 1 1
runs
2
2 2 2 2 2
swa out swap out swap out swap out
0 0 0
3
runs
1 1 1
4
1 3
runs
2 2 2 2 4
swap in swap out swap out swap out
5
0 0 0 0
1 3 1 1 1
6
runs
2 4 2 2 2
swap out swap in swap out swap out
0 0 0 0
runs
CS502 / IT524 OVERVIEW 18
Thrashing due to Swapping
Time Proc A B C D E
0 0 swap out nice 25 swap out
0
1 1 1 1 1
runs
2
2 2 2 2 2
swa out swap out swap in swap in
0 0 0 0
3
runs
1 1 1 1 3
4
1 1
swap in swap out
0 0
1 3 1 1 2
6
24 24 24 24 24
15 15 24 15 24 15 24 15 24
18 1815 18 15 24 18 15 24 18 15 24
23 23 18 23 18 15 23 18 15 24 23 18 15 24
24 24 23 24 23 18
17 17 24 17 24 23 17 24 23 18 17 24 23 18 15
18 18 17 17 17 24
24 24 18
18 18 24
17 17 18
17 17
15 15 17 15 17 18 15 17 18 24
24 24 15 24 15 17
17 17 24
24 24 17
18 18 24 18 24 17
Swap Dev Block Num type (swap, file, fill 0, demand fill)
Block No 2743
local = 1;
if (vfork() == 0)
{
/* child */
global = 2;
local = 3;
_exit();
}
0
Inode
84
Logical Block 84 279
Page in 1 2 3 n
Memory
Page
Out of
Swap In Memory Swap Out
Proc A 30 pgs
Proc A 6 pgs Proc A 12 pgs
Proc B 34 pgs
Proc B 50 pgs
Proc D 8 pgs
A 30 B 34 B 6 C 50 D 8
In an executable file
1648 1618 1
67K
CS502 / IT524 OVERVIEW 38
After Swapping Page into Memory
Page Table Disk block
Entries Descriptors
Virt addr Phys PageState State Block Page Disk Block Count
66K 1776 Val Disk 847 1776 847 1
Thrashing
The swapper swaps out entire
processes until available memory
exceeds the high-water mark
Slow down the system fault rate and
reduces thrashing
1. Driver Interfaces
2. Disk Drivers
3. Terminal Drivers
4. Streams
I/O subsystem?
allows a process to communicate with peripheral
devices
device driver: kernel modules that control devices
device drivers : device types = 1: 1
One terminal driver control all terminals
Software devices
Have no associated physical device
Ex)The system treats physical memory
as a device
to allow a process access to physical memory
outside its address space
Device Interrupts
Figure 10.1. Driver Entry Points
CS502 / IT524 OVERVIEW 51
Device special file
Create the device file
mknod /dev/tty13 c 2 13
/dev/tty13: file name of the device
c: character special file ( b: block special file)
2: major number
Indicate a device type that correspoinds to the
appropriate entry in the block or charcter devcie switch
tables,
13: minor number:
A unit of the device
if (block device)
{
use major number as index to block device switch table;
call driver open procedure for index:
pass minor number, open modes;
}
else
{
use major number as index to character device switch table;
call driver open procedure for index:
pass minor number, open modes;
}
}
CS502 / IT524 OVERVIEW 57
Read and Write
Similar to those for a regular file
Some cases?
kernel transmits data directly between the user address
space and the device
device drivers may buffer data internally
The precise method in which a driver communicates
with a device depends on hardware
Memory mapped I/O
Programmed I/O
(raw I/O)
tty00
160110 CSR tty01
RDB dz00
TDB tty00
tty08
160120 CSR tty09
160122 RDB dz01
160126 TDB tty15
Figure 10.5. Memory Mapped I/O with the VAX DZ11 Controller
tty00
tty01
tty07
ttyintr 0
tty08
tty09
ttyintr 1
tty15
console
consintr
printer00
printer03 printintr 0
1 64000 944000
2 168000 840000
3 336000 672000
4 504000 504000
5 672000 336000
6 840000 168000
7 0 1008000
No raw interface
Raw interface
Driver input/output
Device input/output
Figure 10.9. Call sequence and Data flow through line discipline
7 14 g a r b a g e | e q n |
1 8 i c f i l e
26
0 8 * | t b l
chars
0 8 | t r o f f
0 3 - m m
19 0 8 * | t b l
chars 0 8 | t r o f f
0 3 - m m
0 8 p i c f i l e
23
0 8 * | t b l
chars
0 7 | t r o f f
0 8 p i c f i l e
25
0 8 * | t b l
chars
0 8 | t r o f f
0 1 -
Pty Pty
pair 1 pair 2 Tty driver
Output Input
Driver Queue Queue a queue pair
Block
Output Input
Line Discipline Queue Queue
Output Input
Terminal Driver Queue Queue