Review Problems for CS 30200 / ECE 46810 Exam 1 Spring 2019 Version 1.0. (In case I need to change something.) The exam is over Chapters 2, 4, 6, 7 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 In addition, you should know the parts of the C programming language described in Chapters 6, 7, 12 from http://ee.hawaii.edu/~tep/EE160/Book/PDF/Book.html and Chapters 5, 6, from https://publications.gbdirect.co.uk//c_book/ 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"). Also give a specific example of a runnable command-line with the given format (using programs like dir, sort, find, etc.). 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 z:\>(a & b)> c z:\> a & b c z:\> a & b & c You might find the following web pages useful as a reminder of the meaning of the special symbols &<>|;. http://ss64.com/nt/syntax-redirection.html https://technet.microsoft.com/en-us/library/bb490954.aspx https://technet.microsoft.com/en-us/library/bb490982.aspx 2.) What problem is there with each of the following two command lines? z:\> a | b < c z:\> a > b | c 3.) What three streams are open when every process starts? 4.) How would you draw a picture illustrating the processes and streams in this command-line? z:\> a < b | c 2> d | e > f 2> d 5.) Give an explanation of how the ideas of "standard in", "standard out", "I/O redirection", "pipes", "processes", and "files" are being used in the following command-line. (If you want, you can draw a picture that clearly illustrates each of the mentioned ideas.) z:\> b < a | c > d 6.) Draw a picture that illustrates all the processes and their (possibly shared) streams in the following situation. A process p1 opens the file a.txt for input and it opens the file b.txt for output. Then process p1 creates a pipe. Then p1 creates a child process p2 with p2 inheriting a.txt, the pipe's input, and p1's stderr as its stdin, stdout and stderr. Then p1 creates another child process p3 with p3 inheriting the pipe's output, b.txt and p1's stderr as its stdin, stdout and stderr. Then p1 closes its handles to a.txt and the pipe's output. 7.) For the Windows cmd.exe shell, the "dir" command is a builtin command so the cmd.exe process does the work for the directory listing (there is no dir.exe program). On the other hand, the "sort" and "find" commands are not builtin (so there are sort.exe and find.exe programs). For each of the following cmd.exe command-lines, draw a picture of all the relevant processes that shows the difference between a pipeline with a buitin command and a pipeline with non builtin commands. a) z:\> dir | find "oops" b) z:\> sort /? | find "oops" 8.) If you can only run one program at a time, is it still useful to have an operating system? Why or why not? 9.) Name three major resources controlled by an operating system. 10.) 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 kernel mode d) Switch from kernel to user mode 11.) 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 d) Read a packet of information from the network connection e) Turn off the Wi-Fi connection 12.) What is the difference between an interrupt and a system call? How are they related to mode switches? 13.) What is a buffer? Why does the operating system need buffers in the kernel space? Why does an application need buffers in user space? 14.) Suppose a program has successfully opened a file object, fp, for reading a file from a disk drive and the program has created a buffer, my_buf, of an appropriate size. Consider the following C function call. fread(my_buf, 1, BUFFERSIZE, fp); a) Explain why this function call may, or may not, cause a system call to the operating system. b) Suppose that the above function call causes a system call. Explain why the operating system may, or may not, initiate an I/O operation to read data from the file on the disk drive. 15.) 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. 16.) 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. 17.) 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? 18.) a) What is a context switch? b) What is a mode switch? c) How are context switches and mode switches related? d) What causes mode switches? e) What causes context switches? 19.) What is a time quantum? What is the time quantum used for? How should the time quantum be related to the context switch time? 20.) What is an advantage of a large time quantum? What is a disadvantage? 21.) How do I/O-bound and CPU-bound programs differ? 22.) Use process states to describe the difference between the two function calls Sleep(0) and Sleep(1). 23.) Give four causes for a running process to relinquish the CPU. In each case, what state does the scheduler put the process in? 24.) 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.) 25.) 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. 26.) 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. 27.) 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[0]) &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])) 28.) 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[]); You can check your answers at the following web site. http://cdecl.org/ 29.) (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.) 30.) 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 | 31.) 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? 32.) Explain in detail the difference between the following three functions. (Hint: Use the C Visualizer http://pythontutor.com/c.html#mode=edit to observe these functions while they execute. 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; } 33.) 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; } 34.) In the following C program there are two assignments to the entity **x. For each of these assignments, draw a picture that would help someone visualize the state, at that point in the program, of all of the variables and their memory locations being used by this program. Also, what does this program print? #include int main() { int **x; int *y; int z; x = (int**) malloc(sizeof(int*)); y = (int*) malloc(sizeof(int)); z = 1; *y = 2; *x = y; **x = z; printf("%d\n", *y); z = 3; printf("%d\n", *y); **x = 4; printf("%d\n", z); return 0; }