Review Problems for CS 30200 / ECE 46810 Exam 1 Spring 2022 Version 1.0. (In case I need to change something.) The exam is on Wednesday, March 30. The exam is over Chapters 2 (pages 1-6), 4, 6 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 In addition, you should know the parts of the C programming language described in Chapters 6, 7, 12 from http://cs.pnw.edu/~rlkraft/cs30200-2021/for-class/book/Programming_in_C.pdf and Chapters 5, 6, from https://publications.gbdirect.co.uk//c_book/ and in the zip files http://math.pnw.edu/~rlkraft/cs30200/for-class/array-struct-examples.zip http://math.pnw.edu/~rlkraft/cs30200/for-class/data-structures-and-functions.zip and used in programming assignments http://cs.pnw.edu/~rlkraft/cs30200/homework/hw1.html http://cs.pnw.edu/~rlkraft/cs30200/homework/hw2.html http://cs.pnw.edu/~rlkraft/cs30200/homework/hw3.html 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. Each symbol can represent either a program, a file, or a command-line argument, 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 arguments to the program". Also give a specific example of a runnable command-line with the given format (using Windows command-line programs like dir, more, sort, find, echo, 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 The following web pages are useful for finding the meaning of the special symbols & < > | ; used in the above command-lines. https://ss64.com/nt/syntax-redirection.html https://ss64.com/nt/syntax.html https://ss64.com/nt/ https://technet.microsoft.com/en-us/library/bb490954.aspx https://technet.microsoft.com/en-us/library/bb490982.aspx 2.) What three streams are open when every process starts? 3.) Draw a picture illustrating the processes and streams in this command-line. z:\> a < b | c 2> d | e > f 2> d See the following page for example pictures of command-line pipelines. https://github.com/kennyyu/bootcamp-unix/wiki/stdin,-stdout,-stderr,-and-pipes Also, see ReadMe.txt in the "stdin, stdout, filters, examples" subfolder of hw1. http://cs.pnw.edu/~rlkraft/cs30200/homework/hw1.zip 4.) What problem is there with each of the following two command lines? Hint: Try to draw a picture of all the associated processes and streams. z:\> a | b < c z:\> a > b | c 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.) Name three major resources controlled by an operating system. 8.) Which of the following machine instructions should be privileged? Explain why. a) Set the value of the scheduling timer b) Set the value of the system clock c) Switch from user to kernel mode d) Switch from kernel to user mode e) Initiate a write command to the hard drive 9.) 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 10.) What is the difference between an interrupt and a system call? How are they related to mode switches? 11.) 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. 12.) 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. 13.) 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? 14.) 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? 15.) What is a time quantum? What is the time quantum used for? How should the time quantum be related to the context switch time? 16.) What is an advantage of a large time quantum? What is a disadvantage? 17.) How do I/O-bound and CPU-bound programs differ? 18.) Use process states to describe the difference between the two function calls Sleep(0) and Sleep(1). 19.) Give three causes for a running process to relinquish the CPU. In each case, what state does the scheduler put the process in? 20.) 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.) 21.) 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. 22.) What is a buffer? Why does the operating system need buffers in the kernel space? Why does an application need buffers in user space? 23.) 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. 24.) 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])) 25.) 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/ 26.) (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.) 27. Assume the following program. int a[300]; int b = 25; void c() { int d; } int main() { static int e; int * p = (int*)malloc(4); char s[] = "cat"; } 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 | &s | s | main | 28. 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? 29. 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; } 30. What does the following program do? Explain why. (Hint: Run this program in the C Visualizer.) int functionA(int i) { static int j = 0; int k = j; j = i; if (k > j) return k; else return j; } int main() { int x = functionA(2); int y = functionA(3); int z = functionA(1); return 0; } 31.) 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; }