CS2106 Tutorial Group 03/04 - She Jiayu

61
CS2106 Tutorial Group 03/04 She Jiayu 7

Transcript of CS2106 Tutorial Group 03/04 - She Jiayu

CS2106 Tutorial Group 03/04

She Jiayu

7

Recap

Process Management

3

Text

Data

Stack

Heap

PCB1

PCB2

………

PCB3PID

Process

State

PC, FP,

SP, ……

GPRs

Memory

Region

Info

Process

Table

Process Control

Block

Memory Space of a

Process

Memory Management

4

● Memory model○ Contiguous

■ One-piece■ In-RAM

○ Disjoint■ In-RAM

○ Virtual■ Secondary storage

Memory Management

5

● Memory abstraction○ Physical address○ Logical address○ Address translation

Memory Management

6

● Contiguous memory model○ Fixed-size partition (internal fragmentation)○ Variable-size partition (external fragmentation)

■ Bookkeeping● Linked list

■ Allocation algorithm● First fit● Best fit● Worst fit● Buddy system

Tutorial 07

Q1

8

● In the lecture, we used linked list to store partition information under the dynamic allocation scheme. One common alternative is to use bitmap (array of bits) instead.○ A single bit represents the smallest allocatable memory

space, 0 = free, 1 = occupied. Use a collection of bits to represent the allocation status of the whole memory space. As a tiny example, suppose the memory size is 16KB and the smallest allocatable unit is 1KB. We need 16 bits (2 bytes) to keep track of the allocation status:

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Q1

9

● Give brief pseudo code to: ○ Allocate X KB using the bitmap using first-fit. ○ Deallocate (free) X KB with start location Y. ○ Merge adjacent free space.

Q1

10

● Note that bits cannot be accessed directly as if it is an array. Bitwise operations are needed in order to retrieve/modify a bit. Bit manipulation is the main overhead of using bitmap. For ease of discussion, we use array indexing to present the algorithm.

● Allocate X KB using the bitmap using first-fit.

Q1a

11

start = 0while start < len(bitmap):

start = “index of the first ‘0’ in the bitmap after start”if bitmap[start, start + X - 1] = {0}:

bitmap[start, start + X - 1] = {1}break

else:start = “index of the first ‘1’ in the bitmap after start”

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

● Deallocate (free) X KB with start location Y.

Q1b

12

bitmap[Y, Y + X - 1] = {0}

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

● Merge adjacent free space.

Q1c

13

// NO-OP

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

Q2

14

● Consider six memory partitions of size 200 KB, 400 KB, 600 KB, 500 KB, 300 KB and 250 KB. These partitions need to be allocated to four processes of sizes 357 KB, 210 KB, 468 KB and 491 KB in that order.

● Perform the allocation of processes using: ○ First Fit Algorithm○ Best Fit Algorithm○ Worst Fit Algorithm

200KB 400KB 600KB 500KB 300KB 250KB

Q2a

15

● First Fit Algorithm

200KB 400KB 600KB 500KB 300KB 250KB

P1 (357) P2 (210) P3 (468) P4 (491)

Q2a

16

● First Fit Algorithm

200KB 400KB 600KB 500KB 300KB 250KB

P1 (357) P2 (210) P3 (468)

P4 (491)

Q2b

17

● Best Fit Algorithm

200KB 400KB 600KB 500KB 300KB 250KB

P1 (357) P2 (210) P3 (468) P4 (491)

Q2b

18

● Best Fit Algorithm

200KB 400KB 600KB 500KB 300KB 250KB

P1 (357) P2 (210)P3 (468)P4 (491)

Q2c

19

● Worst Fit Algorithm

200KB 400KB 600KB 500KB 300KB 250KB

P1 (357) P2 (210) P3 (468) P4 (491)

Q2c

20

● Worst Fit Algorithm

200KB 400KB 600KB 500KB 300KB 250KB

P1 (357) P2 (210)

P3 (468) P4 (491)

Q2

21

● Which algorithm makes the most efficient use of memory in this particular case? Which algorithm has the best average runtime?○ In this particular example, Best Fit Algorithm turns out to be

the best in terms of memory efficiency. However, that does not need to hold true in general.

○ Regarding the runtime, Best Fit (Worst Fit) must go through the entire list to find the best (worst) candidate, which takes 𝑂(𝑛). First Fit has the best runtime as the search stops as soon as the first free hole that accommodates the request is available.

Q3

22

● Suppose we use the following allocation algorithm:○ For the first allocation, traverse the list of free partitions

from the beginning of the list until the first partition which can accommodate the request.

○ For the following allocations, instead of starting from the beginning, start from the partition where the previous allocation was performed.

● Call this algorithm Next Fit. Compare it with First Fit in terms of runtime and efficiency of memory use.

Q3

23

● Next Fit Algorithm

200KB 400KB 600KB 500KB 300KB 250KB

P1 (357) P2 (210) P3 (468) P4 (491)

Q3

24

● Next Fit Algorithm

200KB 400KB 600KB 500KB 300KB 250KB

P1 (357) P2 (210) P3 (468)

P4 (491)

Q3

25

● First Fit always starts searching from the beginning of the free list. Over time, the holes close to the beginning will become too small, so the algorithm will have to look further down the list, increasing the search time.

● Next Fit avoids the above problem by changing the starting point of the search. This in turn leads to a more uniform distribution of hole sizes across the free list, and will therefore lead to faster allocation.

Q4

26

● Given a 1024KB memory with smallest allocatable partition of 1KB, use buddy system to handle the following memory requests.○ Allocate: Process A (240 KB) ○ Allocate: Process B (60 KB) ○ Allocate: Process C (100 KB) ○ Allocate: Process D (128 KB) ○ Free: Process A ○ Free: Process C ○ Free: Process B

Q4

27

● Show the physical memory layout as a way to track the results, i.e. below is the physical memory layout after request (a).

A [256KB (240)] Free[256KB] Free [512KB]

Q4

28

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

0

● Initial

Q4a

29

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

0

● Allocate: Process A (240 KB)

1. Find smallest S, such that 2^S >= N2. Check if A[S] has a free block2.1. If yes, remove from list and return2.2. Else,2.2.1. Find smallest R from S+1 to K such that A[R] has a free block2.2.2. From R-1 to S, split2.2.3. Goto 2

Q4a

30

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

0

● Allocate: Process A (240 KB) 512

1. Find smallest S, such that 2^S >= N2. Check if A[S] has a free block2.1. If yes, remove from list and return2.2. Else,2.2.1. Find smallest R from S+1 to K such that A[R] has a free block2.2.2. From R-1 to S, split2.2.3. Goto 2

Q4a

31

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

0

● Allocate: Process A (240 KB) 512

2561. Find smallest S, such that 2^S >= N2. Check if A[S] has a free block2.1. If yes, remove from list and return2.2. Else,2.2.1. Find smallest R from S+1 to K such that A[R] has a free block2.2.2. From R-1 to S, split2.2.3. Goto 2

Q4a

32

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

● Allocate: Process A (240 KB) 512

256

A: 0, 256

1. Find smallest S, such that 2^S >= N2. Check if A[S] has a free block2.1. If yes, remove from list and return2.2. Else,2.2.1. Find smallest R from S+1 to K such that A[R] has a free block2.2.2. From R-1 to S, split2.2.3. Goto 2

Q4b

33

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

A: 0, 256

● Allocate: Process B (60 KB) 512

1. Find smallest S, such that 2^S >= N2. Check if A[S] has a free block2.1. If yes, remove from list and return2.2. Else,2.2.1. Find smallest R from S+1 to K such that A[R] has a free block2.2.2. From R-1 to S, split2.2.3. Goto 2

256

Q4b

34

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

A: 0, 256

● Allocate: Process B (60 KB) 512

256 384

1. Find smallest S, such that 2^S >= N2. Check if A[S] has a free block2.1. If yes, remove from list and return2.2. Else,2.2.1. Find smallest R from S+1 to K such that A[R] has a free block2.2.2. From R-1 to S, split2.2.3. Goto 2

Q4b

35

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

A: 0, 256

● Allocate: Process B (60 KB) 512

256

384

320

1. Find smallest S, such that 2^S >= N2. Check if A[S] has a free block2.1. If yes, remove from list and return2.2. Else,2.2.1. Find smallest R from S+1 to K such that A[R] has a free block2.2.2. From R-1 to S, split2.2.3. Goto 2

Q4b

36

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

A: 0, 256

● Allocate: Process B (60 KB) 512

384

320

B: 256, 64

1. Find smallest S, such that 2^S >= N2. Check if A[S] has a free block2.1. If yes, remove from list and return2.2. Else,2.2.1. Find smallest R from S+1 to K such that A[R] has a free block2.2.2. From R-1 to S, split2.2.3. Goto 2

Q4c

37

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

A: 0, 256

● Allocate: Process C (100 KB) 512

384

320

B: 256, 64

1. Find smallest S, such that 2^S >= N2. Check if A[S] has a free block2.1. If yes, remove from list and return2.2. Else,2.2.1. Find smallest R from S+1 to K such that A[R] has a free block2.2.2. From R-1 to S, split2.2.3. Goto 2

Q4c

38

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

A: 0, 256

● Allocate: Process C (100 KB) 512

320

B: 256, 64

C: 384, 128

1. Find smallest S, such that 2^S >= N2. Check if A[S] has a free block2.1. If yes, remove from list and return2.2. Else,2.2.1. Find smallest R from S+1 to K such that A[R] has a free block2.2.2. From R-1 to S, split2.2.3. Goto 2

Q4d

39

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

A: 0, 256

● Allocate: Process D (128 KB) 512

320

B: 256, 64

C: 384, 128

1. Find smallest S, such that 2^S >= N2. Check if A[S] has a free block2.1. If yes, remove from list and return2.2. Else,2.2.1. Find smallest R from S+1 to K such that A[R] has a free block2.2.2. From R-1 to S, split2.2.3. Goto 2

Q4d

40

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

A: 0, 256

● Allocate: Process D (128 KB)

512

320

B: 256, 64

C: 384, 128768

1. Find smallest S, such that 2^S >= N2. Check if A[S] has a free block2.1. If yes, remove from list and return2.2. Else,2.2.1. Find smallest R from S+1 to K such that A[R] has a free block2.2.2. From R-1 to S, split2.2.3. Goto 2

Q4d

41

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

A: 0, 256

● Allocate: Process D (128 KB)

512

320

B: 256, 64

C: 384, 128768

640

1. Find smallest S, such that 2^S >= N2. Check if A[S] has a free block2.1. If yes, remove from list and return2.2. Else,2.2.1. Find smallest R from S+1 to K such that A[R] has a free block2.2.2. From R-1 to S, split2.2.3. Goto 2

Q4d

42

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

A: 0, 256

● Allocate: Process D (128 KB)

1. Find smallest S, such that 2^S >= N2. Check if A[S] has a free block2.1. If yes, remove from list and return2.2. Else,2.2.1. Find smallest R from S+1 to K such that A[R] has a free block2.2.2. From R-1 to S, split2.2.3. Goto 2

320

B: 256, 64

C: 384, 128768

640

D: 512, 128

Q4e

43

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

● Free A

1. Check A[S] where 2^S == size of B2. Check if the buddy C of B exist2.1. If yes,2.1.1. merge B and C into B’2.1.2. B = B’3. Insert B into A[S]

320

B: 256, 64

C: 384, 128768

640

D: 512, 128

A: 0, 256

0 = 0000000000768 = 1100000000

Q4e

44

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

● Free A

1. Check A[S] where 2^S == size of B2. Check if the buddy C of B exist2.1. If yes,2.1.1. merge B and C into B’2.1.2. B = B’3. Insert B into A[S]

320

B: 256, 64

C: 384, 128768

640

D: 512, 128

0

Q4f

45

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

● Free C

1. Check A[S] where 2^S == size of B2. Check if the buddy C of B exist2.1. If yes,2.1.1. merge B and C into B’2.1.2. B = B’3. Insert B into A[S]

320

B: 256, 64

C: 384, 128768

640

D: 512, 128

384 = 0110000000640 = 1010000000

0

Q4f

46

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

● Free C

1. Check A[S] where 2^S == size of B2. Check if the buddy C of B exist2.1. If yes,2.1.1. merge B and C into B’2.1.2. B = B’3. Insert B into A[S]

320

B: 256, 64

768

640

D: 512, 128

0

384

Q4g

47

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

● Free B

1. Check A[S] where 2^S == size of B2. Check if the buddy C of B exist2.1. If yes,2.1.1. merge B and C into B’2.1.2. B = B’3. Insert B into A[S]

320

B: 256, 64

768

640

D: 512, 128

0

384

256 = 0100000000320 = 0101000000

Q4g

48

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

● Free B

1. Check A[S] where 2^S == size of B2. Check if the buddy C of B exist2.1. If yes,2.1.1. merge B and C into B’2.1.2. B = B’3. Insert B into A[S]

B: 256, 128

768

640

D: 512, 128

0

384

256 = 0100000000384 = 0110000000

Q4g

49

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

● Free B

1. Check A[S] where 2^S == size of B2. Check if the buddy C of B exist2.1. If yes,2.1.1. merge B and C into B’2.1.2. B = B’3. Insert B into A[S]

B: 256, 256

768

640

D: 512, 128

0

256 = 01000000000 = 0000000000

Q4g

50

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

● Free B

1. Check A[S] where 2^S == size of B2. Check if the buddy C of B exist2.1. If yes,2.1.1. merge B and C into B’2.1.2. B = B’3. Insert B into A[S]

B: 0, 512

768

640

D: 512, 128

Q4g

51

K

10210 = 1024

929 = 512

828 = 256

727 = 128

626 = 64

...

020 = 1

● Free B

1. Check A[S] where 2^S == size of B2. Check if the buddy C of B exist2.1. If yes,2.1.1. merge B and C into B’2.1.2. B = B’3. Insert B into A[S]

768

640

D: 512, 128

0

Q4

52

A Free Free

A B Free Free Free

A B Free C Free

A B Free C D Free Free Free B Free C D Free Free Free B Free Free D Free Free Free D Free Free

Q5

53

● Regardless of the partitioning schemes, the kernel needs to maintain the partition information in some way (e.g. linked lists, arrays bitmaps etc). These kernel data, which is the overhead of the partitioning scheme, can consume considerable memory space.

● Given an initially free memory space of 16MB (224 Bytes), briefly calculate the overhead for each of the scheme below. You should try to find a representation that reduces the overhead if possible.

● For simplicity, you can assume the following size during calculation: ○ Starting address, Size of partition or Pointer = 4 bytes each ○ Status of partition (occupied or not) = 1 byte

Q5a

54

● Fixed-Size Partition: Each partition is 4KB (212).

● What is minimum and maximum overhead?

Q5a

55

● Since we know the total number of partitions (16𝑀𝐵4𝐾𝐵

= 4𝐾 = 212

partitions) in this scheme, the simplest representation is to have an array of 212 entries, each entry represent the status of a partition (occupied or free).

● As the partition number is fixed, maximum overhead = minimum overhead = 212 entries × 1 byte each = 212 bytes.

Q5b

56

● Dynamic-Size Partition (Linked List): The smallest request size is 1KB (210), the largest request size is 4KB. Allocations happen in multiples of 1KB.

● What is the minimum and maximum overhead using linked list?

Q5b

57

● A common linked list node structure contains Start Address, Partition Size, Status and Next Node Pointer. This gives a size of 13bytes.

● Minimum overhead○ When the whole partition is free, only one node is needed.○ overhead = 13 bytes

● Maximum overhead○ If every request is of the smallest size, we have the maximum

number of partitions is 16𝑀𝐵1𝐾𝐵

= 16𝐾 = 214.○ overhead = 214 partitions × 13 bytes per partition = 212992 bytes

Q5b

58

● An alternative is to store two separate linked lists for free and occupied partitions respectively.

● In that case, the status field can be removed, reducing the node size to 12 bytes instead of 13 bytes.

● minimum overhead = 12 bytes

● maximum overhead = 214 × 12 = 196608 bytes

Q5c

59

● Dynamic-Size Partition (Bitmap): The smallest request size is 1KB (210), the largest request size is 4KB.

● What is the minimum and maximum overhead using bitmap?

Q5c

60

● Assuming we can store the status with 1 bit, then each bit in the bitmap represents the smallest allocatable unit, 1KB in this case.

● The size of the bitmap is not affected by the number of partitions, hence minimum = maximum overhead = 214 bits = 211 bytes.

61

Thanks!Any questions?