OS Sample Problems What Is a Context Switch? in a Multitasking Operating
Total Page:16
File Type:pdf, Size:1020Kb
OS sample problems What is a context switch? In a multitasking operating system, under what circumstances will a context switch occur? When a process terminates; when the timer elapses indicating that the CPU should switch to another process, when the current process suspends itself, when the current process needs time consuming I/O, when an interrupt arises from some source aside from the timer. In your own words, define a deadlock. How can deadlock be detected? What do most operating systems do about deadlock? A deadlock is a situation where multiple processes are running a concurrent fashion and each process is holding onto a different resource that another of the processes requires so that it can continue. For instance, P0 and P1 are running, P0 is using R0 and P1 is using R1. If P0 wants access to R1, it must wait for P1 to finish. If P1 wants access to R0, it must wait for P0 to finish. If neither have freed up their held resource, then neither can make progress because the resource they need is being held by the other process and neither process will release its resource because it has not yet reached a point where it can free it up. Deadlocks can be detected by creating a graph of processes and resources being held or requested. Any cycle in the graph indicates a deadlock. Most operating systems do nothing about deadlocks. Its up to the user to kill off one or more processes to resolve the problem. What is the difference between a linker, loader and a dynamic linker/loader? When a program references library functions (as with C and FORTRAN), the compilation process also requires linking of a subroutine call to the compiled function in the library. This is handled by the linker. The result is a compiled program which includes all of the pre-compiled functions copied into the executable program. A loader is a program which retrieves the executable program and loads it into memory. A dynamic linker/loader handles linking and loading at run-time so that only functions that are actually called by the running process are linked and loaded. This saves memory space in that functions that are not called (for instance because they are in if clauses that never execute) are not loaded into memory. Answer the following two questions about synchronization. a. Without proper synchronization, which is possible, a deadlock, corrupted data, neither or both? b. One way to implement synchronization is with a semaphore. We might have a semaphore S for access to a datum x where to access x we do wait(S), which is implemented as follows. while(S>0); S--; S is initialized to 1 but if another process is accessing x, then this appears to be an infinite loop. Why is not an infinite loop? a. Corrupted data because a shared datum could be accessed by multiple processes without mutual exclusive access. b. The wait code is called by many multitasking processors. So, while S may be set from 1 to 0 by the first process, the next process wanting to access x will be forced to wait because S is 0. But eventually the operating system switches back to the first process which will release x cause S to become 1 again. When the OS switches back to the waiting process, S is now 1 and it no longer is stuck in the while loop. Given the following run-time stack below, answer the questions below. a. What function is currently active? b. In the active function, what variables can be accessed? c. Which of the functions of this program are void functions? a. foo b. a, b and c c. foo and main Given the skeleton of the program below, draw the run-time stack at point 1. void main() { int x, y; … foo(x); … } void foo(int a) { int b; … bar(); dumb(b); } void bar () { int a, x; … } void dumb(int z) { … point 1 } Notice that bar is called, runs and ends before dumb is called, so bar will not appear on the run-time stack at point 1. dumb: z return to foo foo: a b return to main main: x y .