COP4600 Discussion 8 Segmentation, File System, Fork() Implementation TA: Huafeng Jin hj0@cise.ufl.edu Reminder Homework3 is posted on Sakai, it s due one week. Please prepare early. Discussion 7 Recap Assignment 3 C Version Description (IPC producer-consumer) IPC in C (shm: shmget, shmat, shmctl, shmdt) Semaphore in C (sem_init, sem_wait, sem_post) Java Version Description (simulate 3 algorithms, calculate page faults) Page Replacement Algorithms Second Chance (R bit, load time, linkedlist) NRU (R bit, M bit, 4 categories) Aging (R bit, counter, right shift and insert R) Question-1 What does the following programs do? Program1: } char c, *shm, *s; shmid = shmget(key, 27, IPC_CREAT 0666); s = shm; for (c = 'a'; c <= 'z'; c++) *s++ = c; *s = NULL; Question-1 What does the following programs do? Program1: char c, *shm, *s; shmid = shmget(key, 27, IPC_CREAT 0666); s = shm; for (c = 'a'; c <= 'z'; c++) *s++ = c; *s = NULL; } Put 26 letters into a shared memory with key 5678 Question-1(cont) Program 2: } char *shm, *s; shmid = shmget(key, 27, 0666); for (s = shm; *s!= NULL; s++) putchar(*s); putchar('\n'); 1
Question-1(cont) Program 2: } char *shm, *s; shmid = shmget(key, 27, 0666); for (s = shm; *s!= NULL; s++) putchar(*s); putchar('\n'); Get characters from shared memory with key 5678 Question-2 At the following state, using NRU, what happens? 1) Write 5: 2) Read 6: Question-2 At the following state, using NRU, what happens? 1) Write 5: 2) Read 6: vs. paging with paging vs. paging with paging Segmentation Initially, the memory has only one address space : Provide the machine with many logically completely independent address spaces. 2
Segmentation vs. Paging : Programmer is aware of segmentation. Segmentation can be expanded or shrunk. Paging: Programmer is NOT aware of paging. Page size is fixed. Segmentation with paging Virtual memory: Comprised of segments Segment: Comprised of pages For other differences, refer to Figure 3-33 (P238) Segmentation with paging Virtual address format: Segment no. + page no. + offset In virtual space, we have: Page size is 4KB 32 pages Each segment has at most 8 pages How many bits are needed for segment number in the address? How many bits are needed for page number in the address? What is the MULTICS address for 23200? In virtual space, we have: Page size is 4KB 32 pages Each segment has at most 8 pages How many bits are needed for segment number in the address? How many bits are needed for page number in the address? What is the MULTICS address for 23200? Totally 32/8 = 4 segments, so 2 bits for segment no. Each segment has 8 pages, so 3 bits for page no. 23200%4096 = 2720 Seg #: 0, Page #: 5, Offset: 2720 vs. paging with paging 3
File System File A block of resource for storing information based on some durable storage. The basic unit for a file is called block. File operations: create delete open close read write append File System Implementation Methods: Contiguous Allocation Linked List Allocation (File Allocation Table) I-nodes FAT (File Allocation Table) Linked list allocation using a table in memory. : File A: 4, 7, 2, 10, 12 blocks File B: 6, 3, 11, 14 blocks FAT Using FAT, for each block, we have to provide an address. : Disk is 64GB Block size is 16KB What is the memory size to store file allocation table? FAT Using FAT, for each block, we have to provide an address. : Disk is 64GB Block size is 16KB What is the memory size to store file allocation table? Number of blocks: 64GB/16KB = 2 36 /2 14 = 2 22 So the each address takes 22 bits. Memory size: 22 * 2 22 = 92,274,688 bits = 11,534,336 bytes = 11MB I-node : List attributes and disk addresses of the file s blocks. Remember file management system calls: File descriptor table File table I-node table 4
Direct and Indirect fields: Direct and Indirect fields: Direct fields: Records the address of the block Single indirect fields: Records the address of the block that contains the addresses of blocks. Double indirect fields: Records the address of the block that contains the addresses of blocks. Each such block contains address of blocks. Triple indirect fields: Each I-node can store: 12 direct addresses 2 single indirect addresses 1 double indirect address Block size is: 2KB, block address takes 16bits. What is the max file size? Each I-node can store: 12 direct addresses 2 single indirect addresses 1 double indirect address Block size is: 2KB, block address takes 16bits. What is the max file size? 2KB/16bits = 2 11 /2 = 2 10 Each block can hold 2 10 block addresses. Direct address: 12 blocks Single indirect address: 2*2 10 blocks Double indirect address: (2 10 ) 2 blocks (cont) So totally we can hold: 12 + 2*2 10 + (2 10 ) 2 = 1,050,636 blocks 1,050,636* 2KB 2.1GB The maximum file size is 2.1GB Remember: If there is n 0 direct address, 1 single indirect, 1 double indirect, 1 triple indirect, and Assuming N block addresses can fit in a block, then n0 + N + N 2 + N 3 blocks can be hold at most. File Directory Directory: A virtual container of files A file hierarchy: 5
Implement Directories With I-nodes: Each directory has a directory block Directory block: (just like file block) Each entry contains: Directory/file name Pointer to the i-node Implement Directories Structure with directory blocks: Access file f2: For the following file hierarchy: Which blocks do we need to read in order to open f2? 100, 200, 101, 201, 107 If we want to add a new directory d6 under d1, which blocks need to be updated? 201 If we want to add 1 more block to f6, which block need to be updated? 206 vs. paging with paging 6
Fork() Implementation Fork() Implementation Fork(): Creates a new process. Makes a copy of the current process s address space to the new process. Fork(): Creates a new process. Makes a copy of the current process s address space to the new process. Very time consuming, is there a way to delay the copy of the address space? Recap vs. paging with paging Any Questions? 7