Review Problems for CS 302 Exam 2 Spring 2007 April 25, 2007. Version 1.0. (In case I need to modify something.) 0. Look at the following problems at the end of Chapters 6, 8, 9 from our textbook "Operating Systems Concepts". Chapter 6, Problems 6.3, 6.4, 6.5, 6.6, 6.9. Chapter 8, Problems 8.1, 8.3, 8.5, 8.6, 8.13. Chapter 9, Problems 9.2, 9.3, 9.4, 9.6, 9.7, 9.8, 9.15, 9.16, 9.17, 9.20. 1. What is a race condition? 2. What is a critical section? 3. Suppose that an operating system has the policy that scheduling decisions are only made whenever a process terminates or a running process blocks. How does this policy affect the problem of critical sections? 4. Some people interpret a semaphore as taking on values greater than or equal to zero. Other people interpret a semaphore as taking on both positive and negative values. In either case, the behavior of the semaphore is the same, so you are always free to think about a semaphore using either model. Explain how the wait function works in each interpretation. Explain how the release function works in each interpretation. (Try writing a pseudo code sketch of each function in each interpretation.) 5. Explain in detail how two threads, each one running in one of the thread functions given below, can lead to a race condition (where globalCount is a global variable). DWORD WINAPI thread1(LPVOID threadParam) { while(1) { globalCount++ } return 0; }//thread1 DWORD WINAPI thread2(LPVOID threadParam) { while(1) { globalCount++; } return 0; }//thread2 6. The following code outlines a certain kind of synchronization pattern. In what way are the two threads synchronized? How might this pattern get used? int main() { //thread function prototypes DWORD WINAPI thread1(LPVOID); DWORD WINAPI thread2(LPVOID); semaphore = CreateSemaphore(NULL, 0, 1, NULL); // not signaled CreateThread(NULL, 0, thread1, 0, 0, NULL); CreateThread(NULL, 0, thread2, 0, 0, NULL); while(1){ Sleep(1000); } return 0; }//main DWORD WINAPI thread1(LPVOID threadParam) { for (int i = 0; i < N; i++;) { < do a calculation > ReleaseSemaphore(semaphore, 1, NULL); < do a long calculation > ReleaseSemaphore(semaphore, 1, NULL); < do another long calculation > } return 0; }//thread1 DWORD WINAPI thread2(LPVOID threadParam) { while(1) { WaitForSingleObject(semaphore, INFINITE); < do a very short calculation > } return 0; }//thread2 7. The following code outlines a certain kind of synchronization pattern. In what way are the two threads synchronized? How might this pattern get used? int main() { //thread function prototypes DWORD WINAPI thread1(LPVOID); DWORD WINAPI thread2(LPVOID); semaphore1 = CreateSemaphore(NULL, 0, 1, NULL); // not signaled semaphore2 = CreateSemaphore(NULL, 0, 1, NULL); // not signaled CreateThread(NULL, 0, thread1, 0, 0, NULL); CreateThread(NULL, 0, thread2, 0, 0, NULL); while(1){ Sleep(1000); } return 0; }//main DWORD WINAPI thread1(LPVOID threadParam) { for (int i = 0; i < N; i++;) { ReleaseSemaphore(semaphore2, 1, NULL); WaitForSingleObject(semaphore1, INFINITE); < do first calculation > ReleaseSemaphore(semaphore2, 1, NULL); WaitForSingleObject(semaphore1, INFINITE); < do second calculation > } return 0; }//thread1 DWORD WINAPI thread2(LPVOID threadParam) { while(1) { ReleaseSemaphore(semaphore1, 1, NULL); WaitForSingleObject(semaphore2, INFINITE); < do a calculation > } return 0; }//thread2 8. In the last example, what would happen if the second ReleaseSemaphore(semaphore2, 1, NULL); in Thread1 was deleted? 9. The following code outlines a certain kind of synchronization pattern. In what way are the two threads synchronized? How might this pattern get used? int main() { //thread function prototypes DWORD WINAPI thread1(LPVOID); DWORD WINAPI thread2(LPVOID); semaphore1 = CreateSemaphore(NULL, 1, 1, NULL); // signaled semaphore2 = CreateSemaphore(NULL, 0, 1, NULL); // not signaled CreateThread(NULL, 0, thread1, 0, 0, NULL); CreateThread(NULL, 0, thread2, 0, 0, NULL); while(1){ Sleep(1000); } return 0; }//main DWORD WINAPI thread1(LPVOID threadParam) { for (int i = 0; i < N; i++;) { WaitForSingleObject(semaphore1, INFINITE); < do first calculation > ReleaseSemaphore(semaphore2, 1, NULL); WaitForSingleObject(semaphore1, INFINITE); < do second calculation > ReleaseSemaphore(semaphore2, 1, NULL); } return 0; }//thread1 DWORD WINAPI thread2(LPVOID threadParam) { while(1) { WaitForSingleObject(semaphore2, INFINITE); < do a calculation > ReleaseSemaphore(semaphore1, 1, NULL); } return 0; }//thread2 ======================================================================================================= 10. List the steps to process a page-fault. 11. In a paged virtual memory system, why does the page-size have to be a power of two? 12. What is the advantage of using multi-level page tables in a paged virtual memory system? 13. List an advantage and a disadvantage of increasing the size of a page frame in a paged virtual memory system. 14. In a paged virtual memory system, can the computer's physical memory space be larger than a process's virtual memory space? Explain your answer. 15. In a paged virtual memory system, explain how if a process modifies an arbitrary location in its virtual memory space, the change is not reflected at the same address of other processes. 16. Does a paged virtual memory system have internal or external fragmentation? Explain your answer. 17. In a paged virtual memory system with 32 bit virtual addresses and 8KB page frames, how many bits of a virtual address are used as an offset into a page frame and at most how many page frames can a process have allocated to it? 18. In a paged virtual memory system, explain how two processes can share physical memeory. 19. Conceptually, which of the following quantities describes how many entries are there in a page table? a) |virtual address space| b) |(virtual address space)/(page size)| c) (# of page frames allocated to the process) * (page size) d) (# of page frames allocated to the process) / (page size) e) # of page frames allocated to the process 20.In a translation lookaside buffer implementation of a page table, how many entries are in the page table? a) |virtual address space| b) |(virtual address space)/(page size)| c) (# of page frames allocated to the process) * (page size) d) (# of page frames allocated to the process) / (page size) e) # of page frames allocated to the process 21. Define spatial reference locality and explain how it is related to a process's working set. 22. In a demand paged virtual memory system, what is the purpose of the replacement policy? What is the purpose of the allocation policy? Name a few different replacement and allocation policies. 23. A paged virtual memory system must have a "fetch policy" that decides when a page should be loaded into primary memory. The simplest fetch policy is to load a page only when it has been page faulted. What is another possible fetch policy and what advantage might it have over the simplest one. (Hint: prefetch) 24. Assume that a computer has only three physical pages. The following diagrams show two sequences of referenced pages. Write down in the empty slots below each reference the pages that are loaded in physical memory at each page reference. Also, write down the number of page hits and misses. Use the LRU page replacing policy in both cases. Page Referenced: 1 2 3 4 1 2 3 4 Phys Page 1 Phys Page 2 Phys Page 3 # of hits = # of misses = Page Referenced: 1 2 1 2 3 4 3 4 Phys Page 1 Phys Page 2 Phys Page 3 # of hits = # of misses = 25. Suppose a computer has 4 physical pages, and a process references its virtual pages (page 0 through page 7) in the following order: 0 2 1 3 5 4 6 3 7 4 7 3 3 5 5 3 1 1 1 7 2 3 4 1 a) Suppose the kernel uses FIFO page replacement algorithm. How many page faults would the process have? Which page references are page faults? b) Suppose the kernel uses LRU as the page replacement algorithm. Answer the above two questions. c) Suppose the kernel uses the optimal page replacement algorithm. Answer the above two questions. 26. Let w = 0,4,1,4,1,5,1,6,2,6,3,6,2,6,4,5 be a page reference stream. Given a static page frame allocation of 3 and assuming the primary memory is initially unloaded, how many page faults will the given reference stream incur using the optimal strategy? Using the LRU strategy? 27. Assume the following 2-level page table. Translate the VM addresses 0x00801B4B and 0x00C01578 to their corresponding physical address. Assume a 32-bit word size. Assume a page size of 4KB and that the first 10 bits of the VM address index the level 1 page table, the next 10 bits index the second level and the bits left are the page offset. The third column in the level 2 page tables are physical page addresses. VM Address 0x00801B4B is ______________ in Physical Memory VM Address 0x00C00578 is ______________ in Physical Memory Level 1 0 0x38000 1 0x36000 2 0x32000 3 0x40000 Level 2 at 0x38000 0 0x42000 1 0x50000 2 0x70000 Level 2 at 0x32000 0 0x56000 1 0x58000 2 0x72000 Level 2 at 0x36000 0 0x5a000 1 0x5c000 2 0x62000 at 0x40000 0 0x89000 1 0x82000 2 0x86000