Review Problems for CS 30200 / ECE 46810 Exam 1 Spring 2015 Version 1.1. (In case I need to modify something.) The exam is over Chapters 2, 4, 6, 7, and 8 from the textbook. http://pages.cs.wisc.edu/~remzi/OSTEP/ http://pages.cs.wisc.edu/~remzi/OSTEP/intro.pdf http://pages.cs.wisc.edu/~remzi/OSTEP/cpu-intro.pdf http://pages.cs.wisc.edu/~remzi/OSTEP/cpu-mechanisms.pdf http://pages.cs.wisc.edu/~remzi/OSTEP/cpu-sched.pdf http://pages.cs.wisc.edu/~remzi/OSTEP/cpu-sched-mlfq.pdf In addition, you should know the parts of the C programming language described in Chapters 3, 4 and 5 from https://classes.soe.ucsc.edu/cmps012a/Winter06/OnToC.pdf 1. This question is about I/O redirection and pipes on the (cmd.exe) command line. Explain what each of the following possible command lines mean. In each problem, you need to associate an appropriate meaning to the symbols a, b and c (for example "a is the name of a program, b and c are the names of files" or "a and b are the names of programs and c is the name of a file" or "a is the name of a program, b and c are parameters to the program"). z:\> a > b < c z:\> a < b > c z:\> a | b > c z:\> a < b | c z:\> a ; b ; c z:\> a ; b > c z:\> a ; b | c z:\> a & b < c z:\> a < b ; c z:\> a < b & c z:\> a & b | c z:\> a & b ; c z:\> a & b & c You might find the following pages useful as a reminder of the meaning of the special symbols &<>|;. http://ss64.com/nt/syntax-redirection.html http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx 2. What problem is there with the following two command lines? z:\> a | b < c z:\> a > b | c 3. If you can only run one program at a time, is it still useful to have an operating system? Why or why not? 4. Name three major resources controlled by an operating system. 5. Which of the following machine instructions should be privileged? Explain why. a) Set the value of the timer b) Set the value of the system clock c) Switch from user to supervisor mode 6. Which of the following operations should be privileged? Explain why. a) Read from any sector of the system disk drive b) Read from any sector of a removable flash drive c) Write directly to a printer 7. What is the difference between an interrupt and a system call? What is the relationship between system calls and mode switches? 8. What is a buffer? Why does the operating system need buffers in the kernel space? Why does an application need buffers in user space? 9. What is the difference between user mode and kernel mode? Why is the difference important to an operating system? As part of your explanation, give an example of something that can be done in kernel mode but not in user mode and explain why it is important that it cannot be done in user mode. 10. The kernel keeps a data structure for each process called the Process Control Block (PCB). When a process is not running, its PCB contains the information that is necessary to restart the process on a CPU. Give examples of information that the PCB must have. 11. What are the different states a process can be in? What are the possible transitions between states? (Draw a picture.) What events can cause each of those transitions to happen? 12. What is a context switch? 13. What is a time quantum? What is the time quantum used for? How should the time quantum be related to the context switch time? 14. What is an advantage of a large time quantum? What is a disadvantage? 15. What is the difference between a preemptive scheduler and a non­preemptive scheduler? 16. How do I/O-bound and CPU-bound programs differ? 17.Give four causes for a running process to relinquish the CPU. In each case, what state does the scheduler put the process in? 18. List three things that a running process might do that would cause the scheduler not to move it to the ready state when the process stops running. 19. Use process states to describe the difference between the two function calls Sleep(0) and Sleep(1). 20. Suppose that an operating system has a preemptive priority scheduler with a time quantum of 50 (and no scheduling overhead). Consider the following set of processes. Process Arrival-Time Run-Time Priority 0 0 150 2 1 50 30 3 2 100 130 4 3 120 80 1 4 170 90 4 a) Create a time line illustrating the execution of these processes assuming that scheduling decisions are only made whenever a process terminates or a time quantum expires. b) Create a time line illustrating the execution of these processes assuming that scheduling decisions are only made whenever a process terminates, a time quantum expires, or a process becomes ready (that is, it arrives in the queue). 21. For the following parts, use the following set of processes (all the processes have the same arrival time). Process Run-Time Priority 0 150 2 1 30 3 2 130 5 3 80 1 4 90 4 a) Using FCFS, what is the average wait time for the example process set? b) Using FCFS, what is the average turnaround time for the example process set? c) Using SJF, what is the average wait time for the example process set? d) Using SJF, what is the average turnaround time for the example process set? e) Using priority scheduling, what is the average wait time for the example process set? f) Using priority scheduling, what is the average turnaround time for the example process set? g) Using RR scheduling with a time quantum of 40 and no scheduling overhead, what is the average wait time for the example process set? h) Using RR scheduling with a time quantum of 40 and no scheduling overhead, what is the average turnaround time for the example process set? i) Using RR scheduling with a time quantum of 40 and 10 units of time for scheduling overhead (dispatching, context switching, etc.) what is the average wait time for the example process set? j) Using RR scheduling with a time quantum of 40 and 10 units of time for scheduling overhead (dispatching, context switching, etc.) what is the average turnaround time for the example process set? k) For each of FCFS, SJF, priority scheduling, and RR (with a time quantum of 40) scheduling policies, create a time line illustrating the execution of the processes in the example process set. 22. When a process runs, there are three measures of run time that are useful, wall-clock-time, user-time, and kernel-time. Define each of these. Explain why wall-clock-time will almost never equal user-time plus kernel-time. Explain what kind of relationship you would expect to see between the three times for an "I/O bound" process. Explain what kind of relationship you would expect to see between the three times for a "CPU bound" process. 23. It is possible for a process to be completely CPU bound in user space (it makes no system calls) and yet the process may accumulate some kernel-time. How can that be? (Hint: Think about what some other processes might be doing.) 24. Assume the following program. int a[300]; int b = 25; void c() { int d; } int main() { static int e; int * p = new int; } Write the memory section for the running program that corresponds to each of the following memory addresses (the memory sections are: TEXT, DATA, BSS, STACK, HEAP). Memory Address | Section --------------------------- &a | &b | c | &d | &e | &p | p | main | 25. A function in C can have two kinds of local variables, automatic and static (the default for local variables is automatic). What is the lifetime of an automatic local variable? What is the lifetime of a static local variable? What is the usual way for a compiler to implement these two different lifetimes for these two kinds of local variables? 26. Explain in detail the difference between the following three functions. int functionA() { static int i; i++; return i; } int functionB() { static int i = 0; i++; return i; } int functionC() { static int i; i = 0; i++; return i; } 27. In what way is a static local variable like a global variable? In what way are they different? (Hint: Lifetime and scope.) 28. What does the following functionA() do? int functionA(int i) { static int j = 0; int k; k = j; j = i; if (k > j) return k; else return j; } 29. Determine which of the following C expressions are equivalent to a[i]. The type of a[0] is denoted by . Expression a + i a + i * sizeof() &a + i *(a + i) *(a[0] + i) *(&a[0] + i) *(&a[i]) &a[i] *( *) ((char *) &a[0] + i ) *( *) ((char *)&a[0] + i * sizeof(a[0])) 30. In the declaration int i[10]; the type of i is an array of 10 ints. Describe in words the type if i in each of the following declarations. int *i[10]; int (*i)[10]; int **i[10]; int *(*i)[]; int *(i[]); 31. (a) Write a declaration for a function named f that takes one integer as input and returns a pointer to an array of integers. (b) Write a declaration for an array named holyCow of pointers to functions that take as input an array of char and return a pointer to an int. (There won't be anything this hard on the exam.) 32. Suppose you write a C module myStuff.c and in that module you call the function herStuff() but the definition of the function is not in your module. Explain three different places where the definition of the function herStuff() might be. For each place where the definition might be, explain which program, the compiler, the linker, or the loader, will find the definition.