! CS 322 Operating Systems 1. Processes go through the following states in their lifetime. time slice ends Consider the following events and answer the questions that follow. Assume there are 5 processes, all either in the read or running states initially. Assume the processes are using a single processor. At time 5: P1 executes a command to read from disk 3. At time 15: P3 s time slice ends. At time 18: P4 executes a command to write to disk 3. At time 20: P2 executes a command to read from disk 2. At time 24: P3 executes a command to join with P5. At time 33: An interrupt occurs indicating that P2 s read is complete. At time 36: An interrupt occurs indicating that P1 s read is complete. At time 38: P5 terminates. At time 48: An interrupt occurs indicating that P4 s write is complete. For each time 22, 37 and 47, identify which state each process is in. If it is waiting, indicate what it is waiting for. T22: waiting on disk 2: P2; waiting on disk 3: P1, P4; ready/runnning: P5, P3 T37: waiting for P5 to finish: P3; waiting on disk 3: P4; ready/running: P1, P2, P5 T47: finished: P5; waiting on disk 3: P4; ready/running: P1, P2, P3 2. An operating system s services can be described in terms of a mechanism that enables the service and a policy that determines how the service gets used. Briefly describe this policy vs. mechanism distinction as it applies to CPU scheduling.!1
Mechanism: Context switch are the steps that must be taken to switch which process is using the processor. It involves saving the state of the process currently running (program counter, stack pointer, page table, register contents) and restoring the state associated with the process about to run. Policy: Determine when to switch the running process. This is done by the scheduler. Sample schedulers are FIFO, Round Robin, Shortest Job First and Priority schedulers. Some schedulers try to optimize throughput, others try to optimize response time, etc. 3. One of the things that an operating system does to simplify the programmer s job is to give the illusion that an individual program is the only program running on the computer. Briefly describe how the OS provides this illusion with respect to the computer s memory. The memory inside a process uses a virtual address space. The addresses are generated by the compiler and are independent from the amount of physical memory. When a process is executed, the operating system allocates physical memory to use and stores information about where that physical memory is inside the process s PCB. When instructions are executed, the hardware translates from virtual addresses to physical addresses. This mechanism allows multiple processes to share physical memory, with each process getting some portion of the physical memory. The program, however, is completely unaware of this sharing and refers to memory using its virtual addresses, which are completely independent of the physical addresses, from the program s perspective. 4. For the following C program, which of the following are possible outputs of the program? There may be more than one possible output. You can assume the program compiles and is able to produce at least one of the outputs shown. Briefly explain your choices. int main(int argc, char **argv) { int a = 12; int b = 5; pid_t pid = fork(); if (pid > 0) { b = b + 2; /* What does this output? */ fprintf(stdout,"in parent, a = %d\n",a); fprintf(stdout,"in parent, b = %d\n",b); else { a = a + 3; fprintf(stdout,"in child, a = %d\n",a); fprintf(stdout,"in child, b = %d\n",b);!2
a. In parent, a = 12 In parent, b = 7 In child, a = 15 In child, b = 5 b. In parent, a = 15 In parent, b = 7 In child, a = 15 In child, b = 7 c. In parent, a = 12 In parent, b = 7 In child, a = 15 In child, b = 7 a is the only valid output. The child gets a copy of the parent s memory on fork. They do not share memory. Therefore, only the child sees the update to a and only the parent sees the update to b.!3
5. Answer the following questions about this C program: int main (int argc, char *argv[]) { char *string1 = "cat"; char string2[4]; char *string3 = malloc (strlen (string1) + 1); strcpy (string2, string1); strcpy (string3, string1); The relevant parts of the man page for strcpy say: char *strcpy (char *dest, char *src); The strcpy() function copies the string pointed to by src, including the terminating null byte ( \0 ), to the buffer pointed to by dest. The destination string dest must be large enough to receive the copy. a. (5 points) Will both strcpy calls succeed as they are written? If not, what is the problem? They both work. b. (5 points) What calls to free should you add to avoid all the memory leaks in this function? free (string3)!4
6. Fork / wait a. (3 points) What output would the following C code print? int main (int argc, char *argv[]) { pid_t forked_proc = fork(); if (forked_proc == 0) { printf ("Forked_proc is 0\n"); else if (forked_proc > 0) { wait(null); printf ("Forked_proc is not 0\n"); Forked_proc is 0 Forked_proc is not 0 b. (2 points) If the wait call is omitted, is it possible that the same output would be printed? If not, why not? It is still possible to get the same output. The parent might give up the CPU after forking and before reaching the printf, allowing the child to go first. It is also possible that they will come out in the opposite order.!5
7. Indicate the schedule, average turnaround time, and average response time for the following jobs and scheduling algorithms (show the equations, you do not need to do the arithmetic): Arrival time Job length Job 1 0 9 Job 2 1 9 Job 3 2 5 a. For FIFO i. (3 pts) Schedule: ii. (1 pt) Average turnaround time Job 1 : 0-8 Job 2: 9-17 Job 3: 18-22 ((8-0)+(17-1)+(22-2))/3 = (8 + 16 + 20) / 3= 44/3 = 14.67 iii. (1 pt) Average response time ((0-0)+(9-1)+(18-2))/3 = (0 + 8 + 16) / 3= 24/3 = 8 b. For Shortest-Time-to-Completion-First i. (3 pts) Schedule: ii. (1 pt) Average turnaround time Job 1 : 0-1 Job 3: 2-6 Job 1: 7-13 Job 2: 14-22 ((13-0)+(22-1)+(6-2))/3 = (13 + 21 + 4) / 3= 38/3 = 12.67 iii. (1 pt) Average response time ((0-0)+(14-1)+(2-2))/3 = (0 + 13 + 0) / 3= 13/3 = 4.33 c. For Round Robin with a quantum of 3 i. (3 pts) Schedule: ii. (1 pt) Average turnaround time Job 1 : 0-2 Job 2: 3-5 Job 3: 6-8 Job 1: 9-11 ((19-0)+(22-1)+(16-2))/3 = (19 + 21 + 14) / 3= 54/3 = 18 iii. (1 pt) Average response time Job 2: 12-14 Job 3: 15-16 Job 1: 17-19 Job 2: 20-22!6 ((0-0)+(3-1)+(6-2))/3 = (0 + 2 + 4) / 3= 6/3 = 2
8. Multilevel feedback queue a. (4 pts) What is the purpose of the priority boost? To keep low priority jobs from starving b. (2 pts) What happens if the boost is done too frequently? High priority jobs will not get enough time on the CPU and will become less responsive. The scheduler will behave more like a round robin scheduler. c. (2 pts) What happens if the boost is done infrequently? Low priority jobs will get very little time on the CPU and will take long to complete!7
9. Address translation. For the following addresses, assume that we have 4 segments and an address space of 1024 bytes. Assume that both segments use addresses that grow positive and have the following values for the base and bounds. Your answers should be in hex. Base Bounds Segment 0 0x1adf 0xef Segment 1 0x0505 0xe8 Segment 2 0x23f2 0xa3 Segment 3 0x1cda 0x90 a. (1 pt) How many bits are needed for the segment id? 2 b. (1 pt) How many bits are needed for the offset within a segment? Total address needs 10 bits, so offset will be 8 bits. c. 0x0ed i. (1 pt) Segment #: ii. (1 pt) Offset: 0 0xed = 14*16 + 13 = 237 iii. (1 pt) Is the address valid? yes iv. (2 pts) If the address is valid, show the equation you would use to calculate the physical address, showing the hexadecimal values you would use. You do not need to do the arithmetic. 0x1adf + 0xed d. 0x3f5 i. (1 pt) Segment #: ii. (1 pt) Offset: 3 0xf5 = 15*16 + 5 = 245 iii. (1 pt) Is the address valid? no, the offset is bigger than the bounds iv. (2 pts) If the address is valid, what is the corresponding physical address? not valid!8
!9
10. Mechanism vs. policy. a. (5 pts) Briefly explain the difference between "mechanism" and "policy" in operating systems in general. Mechanism tells us how something is accomplished. Policy tells exactly when and how the mechanism is used. b. (5 pts) Provide an example illustrating the difference with respect to process management. A context switch is the mechanism needed to switch which process is running. Scheduling is defined by a policy that determines when to change which process is running and which process to switch to. 11. (1 point each) For the following CPU instructions, indicate which should only be executed in kernel mode: a. Loading memory into a register ok in user mode b. Setting the address of the timer interrupt handler only in kernel mode c. Setting the values of the base and bounds registers only in kernel mode d. Comparing the value of a register to a constant ok in user mode e. Trap instruction ok in user mode, used to make a system call!10
12. (4 points each) Briefly define the following terms: a. Context switch Mechanism used by the OS to switch which process is running. It saves the register values and memory information of the running process in its PCB. The loads the registers of the process it wants to run from that process s PCB. When it returns from the trap, the program counter will be set to be the process just switched to. b. Preemptive scheduling Preemptive scheduling is any scheduling algorithm that forces a process off the CPU before it voluntarily gives up the CPU. Round Robin is a good example where a process is not allowed to use the CPU for longer than 1 timeslice. c. Internal fragmentation Internal fragmentation is memory that is allocated to a process but the process never uses. This is a big problem with contiguous memory allocation. d. Process control block The PCB contains information about a process that the OS needs to manage it. It also holds information that the CPU needs to resume its execution, like register values, program counter, and where its memory is. e. Heap The heap is the portion of memory used for data. It is used when we make malloc calls and freed up with free calls. It tends to grow over time as the program executes.!11