NAME S.ID. # CS 370 Operating Systems Mid-term Example Instructions: The exam time is 50 minutes. CLOSED BOOK. 1. [24 pts] Multiple choice. Check one. a. Multiprogramming is: An executable program that is composed of modules built using different programming languages. Keeping several programs in memory at once and switching between them. Having multiple processors execute different programs at the same time. When a program has multiple threads that run concurrently. b. A process exists in the zombie (also known as defunct) state because: It is running but making no progress. The parent may need to read its exit status. The user may need to restart it without reloading the program. The process may still have children that have not exited. c. Which state transition is not valid?: Ready Blocked Ready Running Running Ready Running Blocked d. Threads within the same process do not share the same: Text segment (instructions). Stack. Data segment. Open files. e. Preemption is when an operating system moves a process between these states:: Running Ready Ready Blocked Running Blocked Blocked Running 10/05/16
f. All processes in UNIX are created using the fork() system call (except the initial one). True False g. Process creation using fork() results in a copy of the parent s memory image. Assume that a new program is not loaded into the child using the exec() command. After the fork() operation, when a variable is changed in the parent process, this change is reflected in the child process. True False h. Consider a Thread T1 that invoked method x, which resulted in the invocation of method y, which resulted in the invocation of method z. How many frames exist on thread T1 s stack? Do all stack frames on thread T1 s stack must be of the same size? 2, no 3, no 3, yes 2, yes 2. [9 pts] Consider the code below. main(int argc, char **argv) { int i; for (i=0; i < 3; i++) { fork(); printf("hello\n ); } } a. What will be the total number of processes? Answer: (Hint complete the diagram below. Label each process with a number (just for counting. It is not necessarily the PID). 0 parent 1 child b. How many times hello message will be printed? Answer: PAGE 2 OF 6
3. [12 pts] Multiple choice. Check one. a. A time-decayed exponential average of previous CPU bursts allows a scheduler to: Estimate when each process will complete execution and exit. Pick the process that will be most likely block on I/O the soonest. Compute the optimum number of processes to have in the run queue. Determine the overall load on the processor. b. Process aging is when: A long-running process gets pushed to a lower priority level. A long-running process gets pushed to a higher priority level. A process that did not get to run for a long time gets a higher priority level. Memory and other resources are taken away from a process that has run for a long time. c. With soft affinity on a multiprocessor system, the scheduler will: Try to use the same processor for the same process but move it if another processor has no work. Use a single run queue so that there is no ongoing association between processors and processes. Associate a process with a specific processor and ensure it always runs on that processor. Periodically reset the association between all processes and processors. d. Answer if the following scheduling algorithms could result in starvation? Shortest job first True False First-come, first-served True False Round robin True False Priority ( when priorities do not change over time.) True False PAGE 3 OF 6
4. [16 points] The following processes are submitted to an operating system. They are calculation processes which require no input/output (i.e. they are always ready to utilize the CPU). For Shortest Remaining Time First and Round Robin (time quantum 3) give the Gantt charts and calculate average waiting time. Mark and label the instants when a process starts running and is switched out. Process Arrival time Burst time 1 0 7 2 1 5 3 2 2 4 4 1 Shortest Remaining Time First Average waiting time = ( )/ = and Round Robin (time quantum 3) Average waiting time = ( )/ = 5. [12 points] True or false select one. A process cannot be context-switched when it is executing an atomic operation supported by the CPU s ISA (Instruction Set Architecture). True False In solutions to the critical section problem, processes that are executing in their remainder section next make decisions about who gets to enter the critical section next. True False A process may have multiple, unrelated critical sections. True False Processes {P0, P1,, Pn-1} are coordinating their accesses to a critical section where they make changes to common variables. It is OK for one of the processes to disregard the protocol for accessing critical sections and just directly access the critical section. True False PAGE 4 OF 6
6. [12 points] The code segment below show how a set of processes accesses their critical section using a binary semaphore mutex that is initialized to 1. do { wait(mutex) critical section signal (mutex) remainder section } while (TRUE); a. A process P1 has entered the critical section and it currently in the critical section. A process P2 is now trying to enter the critical section. What value of mutex it will find? Answer: b. A process P1 is currently executing the remainder section. A process P2 is now trying to enter the critical section. What value of mutex P2 will find, assuming there are no other processes? Answer: c. Assume that the statement wait(mutex) is omitted. What will happen? Ans: is violated. d. What would happen if we were to omit the signal(mutex) statement? Ans: This would result in a since enter the critical section. PAGE 5 OF 6
7. [10 points] Let s consider the producer-consumer problem with a bounded buffer (below). The size of the buffer is N. There are 3 semaphores: A binary semaphore mutex. Counting semaphore X Counting semaphore.y Producer process do { /* produce item p */ wait(x); wait(mutex); /* add p to buffer */ signal(mutex); signal(y); } while (true); Consumer process do { wait( ); (mutex); /* remove item c from buffer*/ signal( ); signal(x); /* consume c */ } while (true); a. The initial value of X is N. Other initial values are mutex and Y (specify both). b. Fill in the blanks in the code for the Consumer process. PAGE 6 OF 6