SAMPLE MIDTERM QUESTIONS CS 143A Notes: 1. These questions are just for you to have some questions to practice. 2. There is no guarantee that there will be any similarities between these questions and the actual questions in the midterm. Part A: Mark the following statements as either (T)rue or (F)alse by checking the corresponding box on the right. Each correct answer: T F 1. With preemptive scheduling, the only time the scheduler considers running a different process is when the current one runs to completion. 2. A trap is a hardware-generated interrupt. 3. When working with semaphores in a synchronization algorithm, the placement of signal (i.e. V ) statements is important and could result in deadlock if done incorrectly. 4. When a process blocks on I/O, it is placed in the ready queue. 5. The best scheduling algorithm to minimize average waiting time is Shortest Remaining Time First. 6. In UNIX, the exec() system call creates a new child process, and the fork system call replaces the current process s program with a new program and executes it. 7. The top-most level of the pyramid-shaped memory hierarchy consists of the cache. 8. The progress condition for process synchronization implies that if no process is executing in its critical section and there exists some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely. 9. A scheduler using the Round Robin algorithm is susceptible to the convoy effect. 1
Part B: [Processes / Threads] Question B.1: Processes vs. Threads vs. Programs Explain (in no more than 1-2 sentences each) the similarities and differences between the following: processes, threads, and programs. Question B.2: Inter-process Communication a) What is the key distinction between named and ordinary pipes? b) Consider the following scenario: a web server process listens for incoming connections, spawns a new process when a client connects, and communicates with the new process to notify it about additional client connections to facilitate a chat room feature. Which style of pipe would you use for this communication? Why? 2
Question B.3: Concurrent Execution of Processes. Consider the concurrent execution of process X and process Y (see Listing 1). Initialization int a=0; int b=0; Process X while (a==0) {do-nothing}; printf( 4 ); b=1; b=0; printf( A ); Process Y printf( 1 ); a=1; while (b==0) {do-nothing}; printf( 3 ); Listing 1 What could the output of the concurrent execution of process X and process Y be, assuming output is not buffered (it is flushed immediately)? State all possible outputs. 3
Part C: [CPU Scheduling] Question C.1: Waiting / Turnaround Time Consider the following set of processes: Process ID Arrival Time Burst Time Priority* P1 0 60 3 P2 20 40 4 P3 50 30 1 P4 110 10 2 *Assume that the highest priority is Priority=1. Now complete the GANTT charts, average waiting time, and average turnaround time for the 3 algorithms in the following table: Scheduling algorithm Average waiting time Average turnaround time First Come First Served (FCFS) Priority (preemptive) Show your work for partial credit. 4
This page intentionally left blank for you to show your calculations / GANTT charts. 5
Question C.2: Round Robin and the Time Quantum Q Consider N processes sharing the CPU in a round-robin fashion (N>=2). Assume that each context switch takes S ms and that each time quantum is Q ms. For simplicity, assume that processes never block on any event and simply switch between the CPU and the ready queue. Also, assume that a process is still in the ready queue while a context switch is happening. a) How does the behavior of the system change with varying Q? That is, what happens if Q is much smaller than S? What happens when Q, i.e. is much larger than the maximum turnaround time of all the processes? b) Which of the 3 scheduling algorithm performance metrics (waiting, response, turnaround time) would you use RR scheduling for in an attempt to improve? Why (1-2 sentences max)? 6
Part D: [Process Synchronization] Question D.1: Bakery Algorithm Consider the following implementation of the Bakery Algorithm that protects the critical sections of n processes, guaranteeing mutual exclusion, progress, and bounded waiting (Listing 2). 1 repeat 2 choosing[i] := true; 3 number[i] := max(number[0], number[1],,number[n-1]) + 1; 4 choosing[i] := false; 5 for j := 0 to n-1 6 do begin 7 while choosing[j] do no-op; 8 while number[j]!= 0 and (number[j],j) < (number[i],i) do no-op; 9 end; 10 critical section 11 number[i]:= 0; 12 remainder section 13 until false; Listing 2 a) Explain (in 1-3 sentences) how bounded waiting is realized in this algorithm. 7
b) What happens if we delete line 11? Does the algorithm still satisfy mutual exclusion, progress, and bounded waiting? Explain in a maximum of 2-3 sentences. 8
Question D.2: Semaphores Consider the following process execution diagram (Figure 1): Figure 1 Using semaphore notation ( wait, signal, and body ), show how to enforce the execution order (don t forget to declare/initialize your semaphores): 9