Swapping Algorithms
Swapping Algorithms
.Pb: swapped out pages may be frequently used => to be loaded back in
soon
=> clear out bit R, put the page at the end of the quell (i.e give it 1 more
time)
- If bit R = 1, i.e just been used, bit R := 0,the handle skips to the next page
When there is a request, the page with minimal row value will be swapped
out
Ex
0 1 2 3
0 0 0 0 0
1 0 0 0 0
2 0 0 0 0
3 0 0 0 0
Page 1 is used
0 1 2 3
0 0 0 0 0
1 1 0 1 1
2 0 0 0 0
3 0 0 0 0
Page 2 is used
0 1 2 3
0 0 0 0 0
1 1 0 1 1
2 1 1 0 1
3 0 0 0 0
Page 3 is used
0 1 2 3
0 0 0 0 0
1 1 0 1 1
2 1 1 0 1
3 1 1 1 0
.Software implementation
Each page is attached with a time label to keep the latest time when the
page was used,
When there is a request the page with smallest time label (least recently
used) will be swapped out
Time label
P0 2015
P1 2020
2008
P2
P3 2023
Page 2 will be swapped out
Time label
P0 2015
P1 2020
2024
P2
P3 2023
Ex : k =4
0 1 2 3
0 1 0 1
Page was used 1 Page was used 3
clock ticks ago clock ticks ago
Algo:
After each clock tick, all registers are shifted right 1 bit the left - most bit will
be set to
1 if the page was used bit the latest clock tick or 0 was not
When there is a request, the page with smallest register value will be
swapped out
Ex : n = 4, k = 3
P0 0 0 0
P1 0 0 0
0 0 0
P2
P3 0 0 0
P0 , P1, P3 was used
P0 1 0 0
P1 1 0 0
0 0 0
P2
P3 1 0 0
P0 1 1 0
P1 0 1 0
1 0 0
P2
P3 1 1 0
Swapped order:
1,2,[0,3]
Remarks:
AU pages are attached a time label keeping the time when the page was
loaded into the memory
WS – clock is a clock algorithm, i.e it uses R bit, but also used 1 more bit: bit
M(modified)
If bit M = 1: the page content has been changed => Os has to update the
change back to HDD
If bit M = 0: the content is still the same => just discard it
- If the page belong to WS => skip it, move to the next page
Bit M = 1 Os will schedule for update the change to HDD and temporally skip
to the next page if the handle moves 1 round and comes back to the page
now the scheduled write is already done, the page will be swapped out
(R,M) =
(0,0)
P0 = 2015
(R,M) = (R,M) =
(0,1) (1,0)
P3 = 1990 P1 = 1980
(R,M) =
(0,0)
P2 =
3 File system
What is a file ?
Disk structure
Master Boot Record: contains a boot programs to choose which partition will
be use for loading the os
Inside a partition
11 12 13
i.e we have to read all previous blocks to get to the target block
Similar like the previous approach but all pointer are separated in a table
named Index table
Ex:
1 2 3 4 5 6 7
6 2 7 X
a.txt last
Remarks: -To speed up the access, the index table is to be locked to RAM
=> faster
O -> 2^32 – 1
= 16 GB
Index table where each entry value can be ( in case of Fat – 16)
FFFF last block(Null pointer)
0000 free
u g o
Single indirect
Triple ________
Single indirect ptr: points to an index table that each entry is a direct pointer
Data
Single direct
block Direct ptr
4kB
Index table
= 4,144 kb = 4MB
If file size <= 4KB, a single indirect ptr is enough to manage the file content
Direct ptr
Double Indirect ptr: point to an index table that entry is a single indirect ptr
File content
Direct ptr
Single direct
= 4GB
= 4TB
In – node table: only need to load currently use intermediate tables in RAM
Performance:
=> users can read the files directly without being directed
Inode table:will get and access permission in Node field are used to chose
against user access before returning pointer to the files
List of files
name i-node
usr 1
bin 2
home 3
etc 4
Step 2: search for a record with file name appear next in the path then ger
the corresponding i-node
Ex:
Path = /home/user1/a.txt
i-node table
1 3 8 11
3 5 15 20
#3 data blocks
usr 1
Bin 2
home 3
#5 data blocks
User1 8
User2 9
User3 10
a.txt 11
b.txt 12
c.txt 13
Content
a.txt
1st block
. Hardlink implementation
$ls -l linkcount
$ ln a.txt b.txt
$ ln -l
Rwx rw- r—nva K66 .... 2 ..... a.txt share the same content
$rm a.txt
$ ls -l LC
Rwx rw- r—nva K66 .... 1 ..... b.txt has the same content as a.txt
Implemtation:
Option A:
2 files with 2 different I – nodes, but both point to the same data block
a.txt 3
b.txt 2
i-node table
0 1 2 3
10 10
#10 datablock
a.txt
content
Option B
a.txt 3
b.txt 3
i-node table
0 1 2 3
10
#10 datablock
a.txt
content
Option A: when removing a file, we have to scan through all i-nodes table
(take a lot of time) to find out if there is any other i-node still use the data
blocks If none => can free up the block
Option B: we use Link count field is an i-node to count the number of files
using the i-node
Ex:
$ls -l LC
$ ln -s a.txt b.txt
Source symlink
$ls -l
Rwx rw- r—nva K66 ... 1 ... a.txt
It’s a symlink
$rm a.txt
$ls -l
Implementation
current dir
a.txt 3
b.txt 2
i-node table
0 1 2 3
10 15
#10 datablock
Path to a.txt
#15 datablock
a.txt
content
But the symlink file has the content refer to the source
In windows:
- no hardlink
(.link)
- 1 block I is already
- 0 block I is free
Pb: all blocks keeping the bitmap have to be loaded to Ram for quick
allocation
We don’t chain free blocks together as a linked list but we use some blocks
for storing the list of free blocks together as a linked list
2 10 20
4 11 21
5 12 22
3
6 nul
l
Remarks:
- Only the 1st block needs to be loaded to RAM
- when we use up all free blocks into the current linked list block => the
linked list block it self become a free block …
Types of errors:
1. A block belongs to more than a file (busy more than one)
2.A block belongs to a file and the free list at the same time
A 0 1 …………………… N -1
……
o 0 0 …………………… 0 Represent the busy
…… state
i 0 0 …………………… 0 Represent the free
…… state
At the beginning A := 0s
Step1:
Step2: Scan the free block list, If block I appears => A[1,i]++
Step3: checking for error
Cause an error
Type of error
1 A[0,i] > 1
3 A[1,i]>1
4 A[0,I] + A[1,I] = 0
A 0 1 …………………… N -1
……
o 0 0 …………………… 0 Represent the busy
…… state
i 0 0 …………………… 0 Represent the free
…… state
Solution: copy to another block, let one i-node pant to the new block
2, A[1,i] > 1 block i appears more than one time in the free list
Solution: collect all such blocks into a file for user review. After the review,
the user may delete the file, i.e add the block to the pre list
4, None of the above and A[0,i] + A[1,i] > 1: block belongs to a file and the
free list
.Deadlock concept: situation when multiple processes are asking and waiting
for resources kept by other processes, result in none of the processes have
enough resources to finish
Ex: Dinning philosophers pb: when every philosopher got the left chopstick
and waits for the right one
A deadlock occurs iff ( if and only if) the following conditions hold
2 Hold wait: a processes continue to hold allocated resources and wait for
more
Because
Set of vertices
Set of edges
1, A asks for R
2, A ask for S
3 A release R
4 A release S
1, B asks for S
2, A asks for R
3, B release S
4, B release R
A1,A2,B1,B2,A3,A4,B3,B4
A < ----- S
^ |
| v
R ---- B
No cycle
A ---- S
^ |
| v
R <----- B
Detection step
Os runs a cycle checking alg after each allocation. If a cycle exits => there
may be a deadlocks
Recovery step : break the cycle by removing (killing) one of the processes in
the cycle
Using resource allocation graph: row the cycle checking alg before each
request. If the request forms a cycle => it will not be approved
Pb:cost
Assuming that the number of running processes does not change say n
When CPU is allocation to process i, the chart will grow in direction of axis i.
As time cannot be rolled back, the chart can only go forwards in each axis