CS 5348 Operating Systems, Homework #2

Similar documents
Process Management And Synchronization

( D ) 4. Which is not able to solve the race condition? (A) Test and Set Lock (B) Semaphore (C) Monitor (D) Shared memory

Synchronization Spinlocks - Semaphores

Final Exam Preparation Questions

Solutions - Homework 2 (Due date: October 4 5:30 pm) Presentation and clarity are very important! Show your procedure!

Process Synchronization

Find the flaw. Also show a timing diagram that indicates the sequence of actions for several interleavings.

Synchronization for Concurrent Tasks

Last class: Today: CPU Scheduling. Start synchronization

Sections 01 (11:30), 02 (16:00), 03 (8:30) Ashraf Aboulnaga & Borzoo Bonakdarpour

Dept. of CSE, York Univ. 1

Chapter 6: Process Synchronization

University of Waterloo Midterm Examination Model Solution CS350 Operating Systems

Concurrency: Mutual Exclusion and Synchronization

Fall 2015 COMP Operating Systems. Lab 06

SAMPLE MIDTERM QUESTIONS

CS 433 Homework 4. Assigned on 10/17/2017 Due in class on 11/7/ Please write your name and NetID clearly on the first page.

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

1. Consider the following page reference string: 1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6.

Chapter 6: Process Synchronization

Operating Systems CMPSC 473. Synchronization February 21, Lecture 11 Instructor: Trent Jaeger

1995 Paper 10 Question 7

CS4411 Intro. to Operating Systems Final Fall points 10 pages

Student Name: University of California at Berkeley College of Engineering Department of Electrical Engineering and Computer Science

Embedded Systems Dr. Santanu Chaudhury Department of Electrical Engineering Indian Institute of Technology, Delhi

Techno India Batanagar Department of Computer Science & Engineering. Model Questions. Multiple Choice Questions:

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Process Synchronization

CS370 Operating Systems

Operating Systems (Classroom Practice Booklet Solutions)

CS3733: Operating Systems

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

CS420: Operating Systems. Process Synchronization

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

PESIT Bangalore South Campus

CS370 Operating Systems Midterm Review

ECE 3055: Final Exam

Concurrency: Mutual Exclusion and

CS4411 Intro. to Operating Systems Final Fall points 12 pages

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

Process Synchronization

Suggested Solutions (Midterm Exam October 27, 2005)

CS307 Operating Systems Main Memory

CPS 310 second midterm exam, 11/14/2014

Operating Systems Comprehensive Exam. Spring Student ID # 3/16/2006

Student Name:.. Student ID... Course Code: CSC 227 Course Title: Semester: Fall Exercises Cover Sheet:

CS 537: Introduction to Operating Systems Fall 2015: Midterm Exam #1

CS370 Operating Systems

CS-537: Midterm Exam (Spring 2001)

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Process/Thread Synchronization

COMP 3361: Operating Systems 1 Final Exam Winter 2009

Structure of Computer Systems

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 2 Solution

PROCESS SYNCHRONIZATION

BASIC COMPUTER ORGANIZATION. Operating System Concepts 8 th Edition

CSE 378 Final 3/18/10

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

CS153: Midterm (Fall 16)

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

CSE 141 Spring 2016 Homework 5 PID: Name: 1. Consider the following matrix transpose code int i, j,k; double *A, *B, *C; A = (double

CSE 378 Final Exam 3/14/11 Sample Solution

c. What are the machine cycle times (in nanoseconds) of the non-pipelined and the pipelined implementations?

Virtual to physical address translation

Process/Thread Synchronization

CSCI 346 Final Exam Review Materials

Real-Time Systems / Real-Time Operating Systems EE445M/EE380L.6, Spring 2016

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Department of Computer Science Admission Test for PhD Program. Part I Time : 30 min Max Marks: 15

Midterm Exam Solutions and Grading Guidelines March 3, 1999 CS162 Operating Systems

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

CS 433 Homework 5. Assigned on 11/7/2017 Due in class on 11/30/2017

Performance of Various Levels of Storage. Movement between levels of storage hierarchy can be explicit or implicit

CHAPTER 6 Memory. CMPS375 Class Notes (Chap06) Page 1 / 20 Dr. Kuo-pao Yang

Process Synchronization

Question 1. Notes on the Exam. Today. Comp 104: Operating Systems Concepts 11/05/2015. Revision Lectures

Pipelined processors and Hazards

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Midterm Exam March 3, 1999 CS162 Operating Systems

CHAPTER 6 Memory. CMPS375 Class Notes Page 1/ 16 by Kuo-pao Yang

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

JNTUWORLD. 1. Discuss in detail inter processor arbitration logics and procedures with necessary diagrams? [15]

COSC3330 Computer Architecture Lecture 20. Virtual Memory

UNIX Input/Output Buffering

Operating Systems. Week 9 Recitation: Exam 2 Preview Review of Exam 2, Spring Paul Krzyzanowski. Rutgers University.

Process Coordination

6 - Main Memory EECE 315 (101) ECE UBC 2013 W2

Chapter 7: Process Synchronization. Background. Illustration

CS307: Operating Systems

Segmentation with Paging. Review. Segmentation with Page (MULTICS) Segmentation with Page (MULTICS) Segmentation with Page (MULTICS)

Models of concurrency & synchronization algorithms

(b) External fragmentation can happen in a virtual memory paging system.

Chapter 5B. Large and Fast: Exploiting Memory Hierarchy

Assignment-2 (TCS/TIT 503)

Introduction to OS Synchronization MOS 2.3

Unit In a time - sharing operating system, when the time slot given to a process is completed, the process goes from the RUNNING state to the

STUDENT NAME: STUDENT ID: Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Total

Transcription:

CS 5348 Operating Systems, Homework #2 1. Consider two functions, P and Q, and the main program creates two threads to execute P and Q as shown in the following code. Note that each statement is labeled and the label is just for convenience. function P(); function Q(); { a = b + 1; print(a); { b = a + 3; print (b); main() { a = 5; b = 1; create a thread to execute P(); create a thread to execute Q(); List all possible printouts after the execution of the program. You should consider potential process switches at the instruction level. A load b D load a add 1 add 3 B store a E store b C print a F print b Note: add will not cause interference because it does not access shared memory Note: P did not change b and Q did not change a, so only the order of the two prints matters, how C is ordered with DE and how F is ordered with AB does not matter ABDE CF/FC ADBE/ADEB/DABE/DAEB CF/FC DEAB CF/FC 2, 5 / 5, 2 -- Q loads a after P stores a 2, 8 / 8, 2 -- two loads happens before stores 8, 9 / 9, 8 -- P loads b after Q stores b 2. Consider the following program. Find a counter example (execution sequence) that demonstrates that this software solution is incorrect in terms of mutual exclusion violation. var blocked: array[0..1] of boolean; var turn: 0..1; // could be 0 or 1 function p (id: integer) { repeat blocked[id] := true; while (turn!= id) do { while blocked[1 id] do; // do nothing turn := id; < critical section > blocked[id] := false; < remainder code for p> until false; main () { blocked[0] := false; blocked[1] := false; create thread to execute P(0); create thread to execute P(1);

P1: set blocked[1] = true; P1: check and find turn is not 1 ( while (turn!= id) do ) P1: check and find blocked[0] = false, exit inner loop ( while (blocked[1 id]) do; ) P0: set blocked[0] = true; P0: check and find turn is 0, exit outer loop ( while (turn!= id) do ) P0: enter critical section P1: set turn = 1; P1: check and find turn is 1, exit outer loop ( while (turn!= id) do ) P1: enter critical section 3. Consider a sequential program as follows. Assume that we try to execute all statements concurrently to achieve maximal parallelism, but we want to obtain the same output as the sequential execution. Use (a) locks (b) semaphores to control the synchronization so that the program can be executed in maximal concurrency while still get the same outcome as the sequential execution. 1) x = y+z; -- x should be computed before it is used in 4) and 5) 2) w = y*z; -- w should be computed first before it is used in 3) and 5) 3) u = w 3; -- u, v, k are not used in later statements, so does not matter 4) v = x+t; 5) k = x+w; (a) lock lx = true; // locked, done before any execution lock lw = true; // locked, done before any execution x := y+z; unlock(lx); w := y*z; unlock(lw); lock(lw); unlock(lw); u = w 3; lock(lx); unlock(lx); v := x+t; lock(lx); unlock(lx); lock(lw); unlock(lw); k = x+w; (b) semaphore sx = 0; semaphore sw = 0; x = y+z; signal (sx); signal (sx); w = y*z; signal (sw); signal (sw); wait(sw); u = w 3; wait(sx); v = x+t; wait(sx); wait(sw); k = x+w; 4. In the Jurassic Park Amusement center, tourists ride on a tour bus to visit the park. Each bus takes two visitors from the visitor center, drives around the park as long as the passengers wish, and return to the visitor center to drop off the passengers. There are N tour buses on service. If the N buses are all out, then a passenger who wants a ride must wait. If a bus is ready to load but there are no waiting visitors, then the bus waits. A bus does not leave until it loads two passengers. A bus does not load passengers if there is another bus that is only partly filled. Consider the problem of synchronizing the bus processes and the visitor processes using semaphores. The code for visitor process follows. Three semaphores cust, bus, and busready are used. Write the code for the bus process that performs the proper operations on the three semaphores. Use extra semaphores as needed. In the bus process, you can use the same pseudo-statements visitor get in bus', etc. as in the visitor

process. Do not forget to properly initialize the semaphores (including the three given in the code and any new ones you may introduce). var cust: semaphore (:=?); bus: semaphore (:=?); busready: semaphore (:=?); procedure visitor; begin signal (cust); wait (bus); visitor gets in bus; wait (busready); drive around the park; visitor gets off bus; end; var cust: semaphore := 0; bus: semaphore := 0; busready: semaphore := 0; mutex := 1; procedure bus; begin wait (mutex); signal (bus); signal (bus); wait (cust); wait (cust); signal (busready); signal (busready); signal (mutex); drive around the park; drop visitors; end; *** a bus needs to wait for two customers in mutex, otherwise, two waiting customers may get on two different buses. 5. Implement a monitor solution for the bakery problem. Assume that you have N salesmen and customers arrive at arbitrary times. Note: neither the customers nor the salesmen should have a busy waiting. Monitor { int sales_count, initialized to N int cust_count, initialized to 0 condition sales, cust; get_service() { if sales_count <= 0 sales.wait; sales_count = sales_count 1; cust_count = cust_count + 1; if cust_count < 1 cust.signal;

release_service() { -- this can be omitted prepare_service () { if cust_count <= 0 cust.wait; cust_count = cust_count 1; complete_service() { sales_count = sales_count + 1; if sales_count = 1 sales.signal; // sales_count = 1 => no sales were available // what happens if we remove the condition, always signal // end monitor customer { get_service (); receive the service release_service (); salesman { prepare_service(); provide the service complete_service(); 6. When we implement the code for realizing the monitor, there is one important issue. Monitor can only allow one active thread in it. When signaling a condition variable, we need to decide whether to let the signaler or the signalee to continue. We have discussed how to implement the monitor with the choice of letting the signaler continue. Now, discuss the high-level implementation idea if we choose to let the signalee continue. Assume that the monitor is protected by a semaphore mutex. Either the signaler or the signalee continues, we need to maintain a counter temp_count and have a semaphore temp to hold the one that has to wait. If the signalee proceeds and the signaler waits, we need to let the signaler do temp.wait. When the signalee finishes using the monitor or when it goes for cond.wait on some condition, it needs to do the following: If temp-count > 0, temp.signal; else signal mutex; 7. Consider the bakery problem, but we replace the bakery by the hair salon. There are N hair dressers working for the salon. The customer can get the hair styling service if a hairdresser is available. The salon also has an entertainment center which has M seats. A customer can wait in the entertainment center for a hairdresser to become available. After finishing hair styling service, the customer makes payment to the salon. A salon manager process coordinates the customers and hairdressers. It can admit at most N+M customers. A customer, when wanting to have the hair styling service, sends a message to the salon manager. The manager receives customers from the custport and place them in the entertainment center. If there is a waiting customer, the manager receives an available hairdresser from the dresserport and pairs the

customer and the hairdresser for service. If there is no waiting customer, the manager should not receive a hairdresser (hairdressers wait in a room). After the hair styling service finishes, the customer should make payment via the payport. The customer and hairdresser processes are given as follows. Give the code for the salon manager process using Guarded Communication. Customer process: send (custport, customer); take a seat in the entertainment center and wait; get service from the hairdresser; send (payport, payment); Hairdresser process: repeat select send (dresserport, hairdresser); provide hair styling service; until false; Manager process: numcust = 0; numserv = 0; repeat select when numcust < M { receive (custport, customer); numcust = numcust + 1; give the customer a seat in the entertainment center; when numcust > 0 and numdresser > 0 // gray part: can be omitted { receive (dresserport, hairdresser); pair customer and dresser; numcust = numcust 1; numserv = numserv + 1; numdresser = numdresser 1; when numserv > 0 { receive (payport, payment); process payment; numserv = numserv 1; numdresser = numdresser + 1; until salon-close-time; 8. Consider a memory system with 1GB space (=1024MB). Currently the memory free list contains ((128MB, 64MB), (320MB, 8MB), (512MB, 16MB), (768MB, 256MB)) where each item (x, y) in the list represents the starting location x and length y of a free partition. Four jobs J 1, J 2, J 3, and J 4 of sizes 14MB, 20MB, 160MB, and 64MB, respectively, are to be loaded to the memory. Consider the first fit allocation policy and answer the following questions.

(a) Consider each job J i, 1 i 4. When J i is running, what would be the value for the base register and bound register? J1 gets (128MB, 64MB) slot base register 0x08000000, bound register 0x08dfffff (or 0x08e00000 if we use A>=B as the circuit, but consider the above as the correct answer) J2 gets allocated after J1 in (128MB, 64MB) slot base register 0x08e00000, bound register 0x0a1fffff (starting address of next slot is 0x0a200000) J3 gets (768MB, 256MB) slot base register 0x30000000, bound register 0x39ffffff (starting address of next slot is 0x3a000000) J4 gets allocated after J3 in (768MB, 256MB) slot base register 0x3a000000, bound register 0x3dffffff (starting address of next slot is 0x3e000000) (b) What is the free list after loading all four jobs? ((162MB, 30MB), (320MB, 8MB), (512MB, 16MB), (992MB, 32MB)) (c) Now J 4 is running and it accesses the logical address 0x04213a0c. Compute the corresponding physical address. Also, check and determine whether this causes access violation. J4 base register 0x3a000000 + logical address 0x04213a0c physical address 0x3e213a0c > bound register 0x3dffffff access violation (d) Now J 4 is running and it accesses the logical address 0x02f3f12c. Compute the corresponding physical address. Also, check and determine whether this causes access violation. J4 base register 0x3a000000 + logical address 0x02f3f12c physical address 0x3cf3f12c < bound register 0x3dffffff good address, no access violation (e) Answer the questions (a) through (d) for the best fit allocation policy. For (a) J1 gets (512MB, 16MB) slot with base register: 0x20000000 and bound register: 0x20dfffff J2 gets (128MB, 64MB) slot with base register: 0x08000000 and bound register: 0x093fffff J3 gets (768MB, 256MB) slot base register 0x30000000, bound register 0x39ffffff J4 gets allocated after J3 in (768MB, 256MB) slot base register 0x3a000000, bound register 0x3dffffff For (b) (148MB, 44MB), (320MB, 8MB), (526MB, 2MB), (992MB, 32MB) For (c) and (d) because J4 got the same allocation, so the answer is the same as before

9. Consider a 1GB memory system using buddy scheme. The system loads jobs J 1, J 2, J 3, J 4, and J 5 of size 40MB, 20MB, 160MB, 64MB, and 100MB, respectively. (a) Compute the starting addresses of each job. J1 starting at 0x00000000, internal fragmentation = 24MB J2 starting at 0x04000000 (64MB), internal fragmentation = 12MB J3 starting at 0x10000000 (256MB), internal fragmentation = 96MB J4 starting at 0x08000000 (128MB), internal fragmentation = 0 J5 starting at 0x20000000 (512MB), internal fragmentation = 28MB (b) Give the list of free partitions. For each, include its starting address and size. free list: ((96MB, 32MB), (192MB, 64MB), (640MB, 128MB), (768MB, 256MB)) (c) What is the total internal fragmentation? Total fragmentation = 24+12+96+28 MB = 160MB 10. Consider a 256MB memory system using simple paging scheme. Each page is of size 16KB. The memory is byte addressable. The page table for Process P is as follows. Process P s address space is only 5 pages. The numbers in the table are all in decimal. 0 11 1 25 2 40 3 3 4 13 (a) How many bits are required to address the entire memory? (b) How many bits are required to address the offset within each page? (c) What is the maximal possible internal fragmentation P can have? (d) Given a logical address 0x0000ef5b, compute the corresponding physical address. Also, check and determine whether this causes access violation. (e) Given a logical address 0x00017f5b, compute the corresponding physical address. Also, check and determine whether this causes access violation. (a) 28 bits (b) 14 bits (c) 16KB 1 (d) 0x0000ef5b = 1110 1111 0101 1011 page 3, mapped to frame 3, physical address is the same as the logical address (e) 0x00017f5b = 0001 0111 1111 0101 1011 page 5, does not exist access violation 11. Consider a virtual memory system with cache, main memory, and disk. The cache size is 256KB and has an access time 15ns. The cache hit ratio is 90% (which is considered low). The main memory is 256MB and has an access time 150ns. 99.99% of the memory accesses would find the demanded pages in the main memory and 0.01% of them would generate page faults. The disk size is 20GB and has an access time 1ms. Compute the average memory access time. Average memory access time = (0.90 * 15ns) + [(1 0.9) * 0.9999] * (150 + 15)ns + [(1 (0.9 + (1 0.9) * 0.9999)) * (10 6 + 150 + 15)ns] = 1 * 15ns + 0.1 * 150ns + 0.00001 * 10 ns = 40ns

12. Consider a virtual memory system using 32-bit addressing. The physical memory is of size 4GB. Each page is of size 8KB. Assume that each entry of the page table fits in one word. A two-level page table is used and the root level contains 1K entries. A process P has the following page table. Root level page table Level-2 page table starting at 0x000000000 Level-2 page table starting at 0x00000100 Level-2 page starting at 0x00001000 0x00001000 Frame 150 Frame 33 Frame 160 0x00000100 Frame 21 disk Frame 512 null disk Frame 120 (a) How many bits are required to address the root page table? (b) How many bits are required to address each second level page table? (c) Convert the logical address 0000 0000 0100 0000 0101 0000 1111 0101 (binary) to physical address. (a) 10 bits (b) 9 bits (c) 0000 0000 0100 0000 0101 0000 1111 0101 It is root table entry 1, secondary page table entry 2, which is mapped to frame 120 = 0x78 Physical address is: 0000 0000 0000 1111 0001 0000 1111 0101