Review Problems for CS 30200 / ECE 46810 Exam 1 Spring 2016 Version 2.0. (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"). 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.) Draw a picture that illustrates all the processes and their (possibly shared) streams after the following code has executed. PROCESS_INFORMATION processInfo; STARTUPINFO startInfo; ZeroMemory(&startInfo, sizeof(startInfo)); startInfo.cb = sizeof(startInfo); HANDLE h1, h2, h3, h4; h1 = CreateFile("one.txt", GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); h2 = CreateFile("two.txt", GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); CreatePipe(&h3, &h4, NULL, 0); /* h3 is pipe's output handle, h4 is its input handle */ SetHandleInformation(h1, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); SetHandleInformation(h4, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); startInfo.dwFlags |= STARTF_USESTDHANDLES; startInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE); startInfo.hStdOutput = h4; startInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE); CreateProcess(NULL,"child.exe",NULL,NULL,TRUE,0,NULL,NULL,&startInfo,&processInfo); CloseHandle(h1); CloseHandle(h4); 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 supervisor 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 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.) What is the difference between a preemptive scheduler and a non­preemptive scheduler? 22.) How do I/O-bound and CPU-bound programs differ? 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.) 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. 25.) Use process states to describe the difference between the two function calls Sleep(0) and Sleep(1). 26.) Suppose that an operating system has a round robin preemptive priority scheduler with a time quantum of 50 (and no scheduling overhead). Consider the following set of processes. (Assume that lower numbers denote lower priority and higher numbers denote higher priority). 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). 27.) 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. 28.) 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. 29.) 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.) 30.) 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])) 31.) 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/ 32.) (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.)