10/24/2010 COP4600 Discussion 8 Segmentation, File System, Fork() Implementation Reminder Homework3 is posted on Sakai, it’s due one week. Please prepare early. TA: Huafeng Jin hj0@cise.ufl.edu 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) Question-1 What does the following programs do? Program1: #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> main() { char c, *shm, *s; int shmid; key_t key = 5678; shmid = shmget(key, 27, IPC_CREAT | 0666); shm = shmat(shmid, NULL, 0); s = shm; 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) for (c = 'a'; c <= 'z'; c++) *s++ = c; *s = NULL; } Question-1 Question-1(cont) What does the following programs do? Program 2: Program1: #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> main() { int shmid; #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> main() { char c, *shm, *s; key_t key = 5678; int shmid; key_t key = 5678; shmid = shmget(key, 27, IPC_CREAT | 0666); shm = shmat(shmid, NULL, 0); s = shm; char *shm, *s; shmid = shmget(key, 27, 0666); shm = shmat(shmid, NULL, 0); for (s = shm; *s != NULL; s++) for (c = 'a'; c <= 'z'; c++) *s++ = c; putchar(*s); putchar('\n'); } *s = NULL; } Put 26 letters into a shared memory with key 5678 1 10/24/2010 Question-1(cont) Program 2: Question-2 At the following state, using NRU, what happens? #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <stdio.h> main() { int shmid; 1) Write 5: key_t key = 5678; char *shm, *s; shmid = shmget(key, 27, 0666); shm = shmat(shmid, NULL, 0); for (s = shm; *s != NULL; s++) putchar(*s); putchar('\n'); 2) Read 6: } Get characters from shared memory with key 5678 Question-2 At the following state, using NRU, what happens? Outline Segmentation Segmentation vs. paging Segmentation with paging 1) Write 5: File System File System Implementation FAT I-node File Directory 2) Read 6: Implementation with I-node Outline Segmentation Segmentation vs. paging Segmentation with paging File System Fork() Implementation Segmentation Initially, the memory has only one address space Segmentation: ◦ Provide the machine with many logically completely independent address spaces. File System Implementation FAT I-node File Directory Implementation with I-node Fork() Implementation 2 10/24/2010 Segmentation vs. Paging Segmentation with paging Segmentation: Virtual memory: ◦ Programmer is aware of segmentation. ◦ Segmentation can be expanded or shrunk. Segment: ◦ Comprised of segments ◦ Comprised of pages Paging: ◦ Programmer is NOT aware of paging. ◦ Page size is fixed. For other differences, refer to Figure 3-33 (P238) Segmentation with paging Virtual address format: Example ◦ Page size is 4KB ◦ Segment no. + page no. + offset Example 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 In virtual space, we have: ◦ 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? Outline Segmentation Segmentation vs. paging Segmentation with paging File System File System Implementation FAT I-node File Directory Implementation with I-node Seg #: 0, Page #: 5, Offset: 2720 Fork() Implementation 3 10/24/2010 File System File File System Implementation ◦ A block of resource for storing information based on some durable storage. Methods: ◦ Contiguous Allocation ◦ Linked List Allocation The basic unit for a file is called block. File operations: FAT (File Allocation Table) ◦ I-nodes ◦ create ◦ delete ◦ open ◦ close ◦ read ◦ write ◦ append FAT FAT (File Allocation Table) ◦ Linked list allocation using a table in memory. Example: FAT Using FAT, for each block, we have to provide an address. Example: ◦ Disk is 64GB ◦ Block size is 16KB What is the memory size to store file allocation table? 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. Example: ◦ Disk is 64GB ◦ Block size is 16KB What is the memory size to store file allocation table? Number of blocks: 64GB/16KB = 236/214 = 222 So the each address takes 22 bits. Memory size: 22 * 222 = 92,274,688 bits = 11,534,336 bytes = 11MB I-node I-node: Remember file management system calls: ◦ List attributes and disk addresses of the file’s blocks. ◦ File descriptor table ◦ File table ◦ I-node table 4 10/24/2010 Direct and Indirect fields: Direct fields: Single indirect fields: Direct and Indirect fields: Records the address of the block 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: … Example Each I-node can store: Example ◦ 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 210 block addresses. Direct address: 12 blocks Single indirect address: 2*210 blocks Double indirect address: (2 10)2 blocks Example (cont) So totally we can hold: ◦ 12 + 2*210 + (210)2 = 1,050,636 blocks ◦ 1,050,636* 2KB ≈ 2.1GB The maximum file size is 2.1GB Remember: File Directory Directory: ◦ A virtual container of files A file hierarchy: ◦ If there is n0 direct address, 1 single indirect, 1 double indirect, 1 triple indirect, and ◦ Assuming N block addresses can fit in a block, then n0 + N + N2 + N3 blocks can be hold at most. 5 10/24/2010 Implement Directories With I-nodes: Implement Directories ◦ Each directory has a directory block ◦ Directory block: (just like file block) Structure with directory blocks: ◦ Access file f2: Each entry contains: Directory/file name Pointer to the i-node Example For the following file hierarchy: Example Example Which blocks do we need to read in order to open f2? Outline Segmentation vs. paging Segmentation with paging ◦ 100, 200, 101, 201, 107 If we want to add a new directory d6 under d1, which blocks need to be updated? File System File System Implementation ◦ 201 Segmentation FAT I-node If we want to add 1 more block to f6, which block need to be updated? File Directory Implementation with I-node ◦ 206 Fork() Implementation 6 10/24/2010 Fork() Implementation Fork(): ◦ Creates a new process. ◦ Makes a copy of the current process’s address space to the new process. Fork() Implementation 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 Segmentation Segmentation vs. paging Segmentation with paging File System Any Questions? File System Implementation FAT I-node File Directory Implementation with I-node Fork() Implementation 7