CS 540 Midterm Exam - Name: Date: Thursday, April 4, 2002 (in class) Part 1: (60 points - 4 points for each problem) (b) 1. Which one is the advantage of multiprocessor systems? (a) Increased modularity (b) Increased reliability (c) Increased security (d) None of above (d) 2. System calls to allocate and free memory belongs to which of service category below: (a) Device manipulation (b) File manipulation (c) Information maintenance (d) Process control (c) 3. When the currently running process issues a system call for I/O: (a) It is added to the tail of the ready queue to do its I/O. (b) Its PCB is entered into the ready queue for later execution. (c) It is marked blocked and its PCB added to the tail of the device queue associated with the I/O request. (d) It is added to an I/O queue also referred to as blocked list (c) 4. The scheduler that brings processes into memory and swaps them out on disk as needed is referred to as: (a) Short-term scheduler (b) Long-term scheduler (c) Medium-term scheduler (d) None of the above (a) 5. When we have a message-passing scheme between processes P and Q as: send (P, message) and receive (Q,message), this has the following property: (a) The link may be unidirectional or bidirectional. (b) The link is associated with only one process. (c) There can be more than one link between a pair of processes. (d) The processes do not need to know each other's identities. (d) 6. Which is not the property of a signal in UNIX systems? (a) A signal is generated by the occurrence of a particular event. (b) A generated signal is delivered to a process. (c) Once delivered, the signal must be handled. (d) None of the above. (b) 7. In an operating system a utility which lets the users issue and execute commands from the keyboard is called: (a) Terminal Handler (b) Command Interpreter (c) Kernel (d) None of the above (c) 8. Which is not a primary reason for providing an environment that allows process cooperation: (a) Modularity (b) Computational speedup (c) Concurrency (d) Information sharing (a) 9. The primary difference between user-level threads and kernel threads is: (a) User level threads do not use OS services via system calls, where kernel threads require system calls. (b) User level threads are independent of each other, whereas kernel threads can write into each other's memory space. (c) User level threads require memory management where kernel threads do not. (d) None of above. (d) 10. CPU burst distribution is generally characterized as (a) Constant (b) Linear (c) Polynomial (d) Exponential or hyper-exponential (d) 11. Which is not the function of a dispatcher? (a) Switching context (b) Switching to use mode (c) Jumping to the proper location in the user program to restart that program (d) None of above (b) 12. Which is not a CPU scheduling criterion? (a) CPU utilization (b) Burst time (c) Throughput (d) Response time (a) 13. Which is a preemptive scheduling? (a) RR (b) FCFS (c) SJF (d) None of the above (a) 14. If the time quantum is very large, a RR (Round-Robin) scheduling is the same as: (a) FCFS (b) SJF (c) SRTF (d) multilevel queue (b) 15. Which is not a solution the critical-section problem? (a) Conditional critical region (b) Shared memory (c) Monitor (d) Semaphore Part 2: (90 points) 1. Describe the process status. (10 points) Ans: o new: The process is being created. o running: Instructions are being executed. o waiting: The process is waiting for some event to occur. o ready: The process is waiting to be assigned to a process. o terminated: The process has finished execution. 2. What is a thread? What are the benefits of using threads? (10 points) Ans: a. A thread, a lightweight process (LWP), is a basic unit of CPU utilization. It shares with other threads within the same process its code, data, open files and signals. b. Benefits: o Responsiveness: Multithreading an interactive application may allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user. o Resource Sharing: Threads share the memory and the resources of the process to which they belong. o Economy: Allocating memory and resources for process creation is costly. o Utilization of MP (multiprocessor) Architectures: Each thread may be running in parallel on a different processor. 3. What is a race condition? How can we prevent a race condition? (10 points) Ans: a. The race condition is the situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. b. To prevent race conditions, concurrent processes must be synchronized. How many processes will be created when the following program is executed? Assume that all fork system calls are successful. What will be printed? (Hint: Be careful and draw a picture.) (12 points) main() { int i=1; int ret_val = 1; while(i <= 2) { if (ret_val == 0) { /* Child's code */ printf("In child %d. \n", i); exit(0); } else { /* Parent's code */ fork(); printf("In parent %d. \n", i); i = i + 1; } } } There are 4 processes (1 parent and 3 child processes) created when this program is executed. The following could be printed: In parent 1. In parent 1. In parent 2. In parent 2. In parent 2. In parent 2. 4. Add the semaphores necessary to sychronize processes A, B, C, D, and E so that process A must finish executing before B starts, process B must finish before C or D starts, and process D must finish before process E starts. Show your solution. Remember to indicate the initial value of each semaphore. (12 points) Ans: The relations among processes can be represented in the following diagrams. S2 / C S1 / A ---- B \ S4 S3 \ D ---- E The program can be as follows: semaphore S1, S2, S3 = 0, 0, 0, 0; Process A: ---------- - do work of A signal(S1); /* Let B start */ Process B: ---------- wait(S1); /* Block until A is finished */ - do work of B signal(S2); /* Let C/D start */ signal(S2); /* Let C/D start */ Process C: ---------- wait(S2); /* Block until B is finished */ - do work of C Process D: ---------- wait(S2); /* Block until B is finished */ - do work of D singal(S3); /* Let E start */ Process E: ---------- wait(S3); /* Block until D is finished */ - do work of E 5. Suppose that the following processes arrive for execution at time 0 in the order A, B, C: (12 points) Process Run Time Priority A 2 1=high B 3 3=low C 1 2 a. Draw four Gantt charts illustrating the execution of these processes using FCFS, SJF, a nonpreemptive priority (a smaller priority number implies a higher priority), and RR (quantum = 1) scheduling. b. What is the waiting time of each process for each of the scheduling algorithms? c. What is the turnaround time of each process for each of the scheduling algorithms? Ans: a. The four Gantt charts are 0 2 5 6 0 1 3 6 +--+---+-+ +-+--+---+ | A| B |C| FCFS |C| A| B | SJF +--+---+-+ +-+--+---+ 0 2 3 6 0 1 2 3 4 6 +--+-+---+ +-+-+-+-+--+ |A |C| B | Priority |A|B|C|A| B| RR +--+-+---+ +-+-+-+-+--+ b. Waiting time: | FCFS | SJF | Priority | RR ---+------+-----+----------+---- A | 0 | 1 | 0 | 2 B | 2 | 3 | 3 | 3 C | 5 | 0 | 2 | 2 c. Turnaround time: | FCFS | SJF | Priority | RR ---+------+-----+----------+---- A | 2 | 3 | 2 | 4 B | 5 | 6 | 6 | 6 C | 6 | 1 | 3 | 3 6. Consider the following semaphore definition: A semaphore S is an integer variable that can only be accessed via two indivisible (atomic) operations: (12 points) wait(S): while (S <= 0) /* do nothing */ ; S = S - 1; signal(S): S = S + 1; A solution to the mutual exclusion problem, using these operations is shown below: shared int S = 1; /* S is initialized to 1 */ Each process executes the following code: while (TRUE) { wait(S); - in critical section - signal(S); } a. Does the above solution involve any busy-waiting? Explain. b. What is the "priority inversion" problem? c. If priority scheduling is used, can the above solution lead to the priority inversion problem? If so, give an example; otherwise, explain why not. Ans: a. This semaphore definition involves the busy waiting because a waiting process is looping for the shared variable S. b. A priority inversion problem is a situation in which a low-priority process is blocking a high-priority process. c. Yes, the above solution can lead to the priority inversion problem if a high-priority process is waiting for a low-priority process to leave the critical section and signal the shared variable S. 7. Consider the following proposed solution to the dining philosophers problem: semaphore fork[5] = 1,1,1,1,1; /* each semaphore is initialized to 1 */ philosopher (int i) /* philosopher i=0,1,2,3,4 executes this code */ { while(1) { think(); DOWN(fork[i]); DOWN(fork[(i+1)%5]); eat(); UP(fork[(i+1)%5]); UP(fork[i]); } } Briefly explain how the above solution may lead to deadlock. In the above solution, add a single semaphore to make the solution deadlock free. Be sure to indicate the initial value of the semaphore. (12 points) Ans: a. Suppose all philosophers execute the first wait/DOWN operation, before any have a chance to execute the second wait/DOWN operation; that is, they all grab one fork (chopstick). Then, deadlock will occur and no philosophers will be able to proceed. This is called a circular wait. b. semaphore fork[5] = 1,1,1,1,1; /* each semaphore is initialized to 1 */ semaphore count = 4; /* Number of philosophers who can grab their forks */ philosopher (int i) /* philosopher i=0,1,2,3,4 executes this code */ { while(1) { think(); DOWN(count); DOWN(fork[i]); DOWN(fork[(i+1)%5]); eat(); UP(fork[(i+1)%5]); UP(fork[i]); UP(count); } }