CS 470 - Mid term Exam Maximum points 100; Maximum time allowed: 75 minutes Name: Note: Answer all questions. Your answers should be brief and precise to the point. Note that a statement is true means it is always true, false means it is sometimes true or never true 1. For each of the following statements, state whether it is true or false. (10 points) (a) I/O devices and CPU can execute concurrently (b) Each device controller maintains a local buffer storage and a set of special purpose registers. (c) Under Blocking I/O, after I/O starts, control returns to user program only upon I/O completion. (d) A multi-threaded process has one program counter per thread. (e) Operating system ensures efficient operation of the system. (f) Services provided by the operating system are accessed from user programs through system calls. (g) Under layered approach for designing operating system, operating system is divided into a number of layers, each built on top of lower layers. (h) Operating system helps in protecting unauthorized access to resources. (i) Operating system allocates resources to processes. (j) Operating system provides an user-interface to use the computer system. 1
2. (5+10+15=30 points) (a) Describe briefly the functions of the following : (i) long term scheduler. (ii) short term scheduler, (iii) medium term scheduler (b) For each of the following statements, state whether it is true or false. i. fork() system call creates a new process whose address space is identical to its parent process. ii. fork() system call returns 0 in parent and process id of the child in the child process. iii. fork() system call returns 2 if it fails. iv. exec() system call and it variations replace the new process memory space with a new program. v. exec() system call never returns. vi. Multithreading an interactive application increases responsiveness to the user. vii. Threads have their own registers and stack. viii. Threads share memory and other resources of the process they belong. ix. Multithreaded programming provides a mechanism for more efficient use of the multiple cores in current day desktops. x. Process creation is more expensive than thread creation. xi. Two or more processes can be executing concurrently even if there is only one processor. xii. Two or more processes can be executing in parallel even if there is only one processor. 2
(c) Consider the following program: #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/wait.h> main() { int id1,id2,id; if ((id2=fork())==0){ execlp("pwd", "pwd",null); printf("hello World 0 \n"); if ((id1=fork())==0){ id=fork(); execlp("ls", "ls",null); printf("hello World 1 \n"); id= fork(); id=fork(); printf("hello World 2 \n"); Suppose foo is the executable file for this program. What is one possible output you will get when foo is executed (assume that the directory in which the executable file of this program, called foo lies is the directory /fun and foo is the only file in the directory /fun )? Will it produce the same output (i.e., in the same order) on all machines and under all runs? Justify your answer. How many processes are created when you run foo? Draw the parent child relationship diagram among the processes created as a result of its execution. (Note that pwd is the Linux command for outputting the present working directory and ls is the Linux command for listing the contents of current directory.) 3
3. (10+10+10+10=40 points) (a) If concurrent access to shared resource is not synchronized, shared resource may be left in an inconsistent state. Explain with an example clearly why this statement is true. (b) Explain the three requirements a solution to the critical section problem needs to satisfy? 4
(c) A parking garage has space for parking 100 vehicles. Write a semaphore-based solution to control access to the garage. Make the following assumptions: The Full light sign at the entrance is turned on when the garage is full. Assume that you have two built-in commands turn on Full sign and turn off Full sign for turning the Full sign on or off. A driver enters the garage when the Full sign is off after swiping her/his Decal into the machine at the entrance. When the driver leaves the garage he/she swipes the card at the exit and leaves. The machine at the entrance runs the Enter process and the machine at the exit runs the Exit process. Write the code for the two processes. Use as many semaphores and shared variables as you need. Shared Data Structures: Process Enter: do{ Wait until someone swipes her/his Decal; Process Exit: do{ Wait until someone swipes her/his Decal; while(1); while(1); 5
(d) Write a monitor-based solution for the following problem: The two ends of a rope are tied to the top of the two trees on opposite banks of a river. Monkeys on each side of the river use this rope to cross the river. The rope can be used by monkeys crossing from only one side of the river at any given time. At any given time, any number of monkeys are allowed to use the rope as long as they are all crossing the river in the same direction. Write a monitor-based solution for synchronizing the Monkeys access to the rope. Your solution has to ensure mutual exclusion (i.e, it should not allow monkeys to cross in both directions simultaneously) and progress property. It does not have to satisfy the bounded waiting property. If you like, you can use the following incomplete code: monitor M { enum direction {Left_To_Right, Right_To_Left Procedure GrabTheRope(direction d){ Procedure ReleaseTheRope (direction d){ Initialization: Monkey crossing Left To Right Monkey crossing Right To Left { { M.GrabTheRope(Left_To_Right) M.GrabTheRope(Right_To_Left) Cross the river; Cross the river; M.ReleaseTheRope(Left_To_Right) M.ReleaseTheRope(Right_To_Left) 6
4. For each of the following statements, state whether it is true or false. (5 points) (a) A CPU scheduling algorithm should strive for minimizing CPU utilization. (b) A CPU scheduling algorithm should strive for maximizing turnaround time. (c) FCFS scheduling algorithm has convoy effect. (d) Shortest job first (SJF) scheduling algorithm is a priority based scheduling algorithm (e) Priority based scheduling algorithm can cause starvation. 5. Load balancing counteracts the benefits of processor affinity. Explain why? (5 points) 7
6. (10 points) Consider the following set of processes that arrive at the times indicated, with the length of CPU-burst time given (all in milliseconds). Processes Burst Time Arrival time Priority ========== ========== ============ ======== P1 30 0 5 P2 15 5 10 P3 20 10 15 Calculate the average waiting time and average turn-around time under each of the following scheduling algorithms: (i) Non-Preemptive Shortest Job First scheduling. (Note that priority is not used in this) (ii) Round Robin (with 20 ms time slice). (Note that priority is not used in this) (iii) Priority based scheduling (higher number means higher priority; if a higher priority process arrives while a lower priority process is executing, the lower priority process will be preempted). 8