You are on page 1of 4

Homework 1 Solutions

May 12, 2008


1. A computer has a pipeline with four stages. Each stage takes the same time time to do its work0.4 ns (nanoseconds). How many instructions can this machine execute per second? The machine can execute 109 instructions per 0.8 seconds. 0.8 + 0.2 = 1 second. So the formula is 109 + 109 /4 = 109 + 109 /4 = 1,000,000,000 + 250,000,000 = 1,250,000,000 instructions per second. 2. What is a trap instruction? What is its use in operating systems? What is the key dierence between a trap and an interrupt? A trap instruction switches from user mode to kernel mode. An interrupt is used to save the state of a process during a context switch. 3. Which of the following operations should be allowed only in kernel mode? Briey explain your decision for each one. (a) Cause a trap: This can be done by user programs. Its the only way that they can enter the kernel to do a system call. Its often useful to allow traps in the OS kernel as well. (b) Disable all interrupts: This can only be done in kernel mode. Interrupts are used in context switches or to notify the system when retrieving data from the hard disk. (c) Write the memory map for the current process: This can only be done in kernel mode. If you do it in user mode, a process could overwrite another process (or the kernels) memory. (d) Read the time-of-day clock: User mode can do this. You have the clock displayed on your desktop, right? (e) Receive a packet of data from the network: This can only be done in kernel mode. The kernel has to decide which process gets the packet, and needs to keep the contents secret from processes that arent going to get the packet. (f ) Shutdown the computer: This can only be done in kernel mode. You dont want user processes to be able to halt the computer and destroy everyone elses work. (Imagine what this could do if someone shutdown unix.ic.ucsc.edu while you were working on a program.) 4. What is the dierence between the following two function calls? Assume that str is a properly null-terminated string. Also, le descriptor 2 (in Unix) is the same as the stdio le stderr. HINT: what does the operating system do for each function? fprintf (stderr, %s, str); write (2, str, strlen (str)); write() is a system call, while fprintf() is a library function. This fact can lead to major dierences in the process which is triggered on either of the above calls. When write() is called, a trap is always made to kernel mode and the output is written without delay. On the other hand, fprintf() is not a system call and may be writing to a buer, which would be ushed via a write() call once it is full. Thus, many

fprintf() calls could lead to only one write() call. 5. While there are many dierent processor architectures, most desktop computers and servers use the Intel x86 architecture. From the point of view of operating systems, what are the advantages to this approach? Are there any disadvantages? Advantages: - Hardware development costs are spread over as many systems as possible. - Byte ordering is the same on every system. - Machine code may be somewhat portable between dierent computers. - The OS and drivers are portable between computers (booting XP or Linux on a MacBook Pro). Disadvantages: - Problems in the x86 architecture will aect nearly all computers. This includes hardware issues as well as (possibly) viruses. - The architecture may not be well-optimized for dierent types of systems (low-power, high-performance, etc.). - The x86 architecture tends to sacrice performance for backwards compatibility, and legacy features make certain OS implementation details harder. 6. Why is the process table needed in a timesharing system? Is it also needed in personal computer systems in which only one process exists, that process taking over the entire machine until it is nished? A process table is needed in a timesharing system because we need to know what information went along with a process when we swap it in and out to run it and other processes. In a system where only run process runs at a time, and it runs to completion, a process table is not needed. 7. For each of the following system calls, give a condition that causes it to fail: (a) read(): Read can fail on a closed le handle, end of le already read, a bad string (buer) pointer, or a le not opened for reading. Problems reading the le itself can also cause failure (disk failure, for example). (b) write(): Write can fail on a closed le handle, le not opened for writing, bad string pointer, lack of space on the device, or I/O error. (c) close(): You give close an incorrect le handle/descriptor. (d) fork(): Fork can fail if there are too many processes (either for this user for the whole operating system) or if theres not enough memory. (e) execvp(): Execvp can fail if the path name to the le you want to execute is incorrect, there are too many symbolic links in translating the path or le, or your argument list can be too long. (f ) waitpid(): Waitpid will fail if the process id given to it no longer exists.

8. There are several dierent approaches to operating system design. (a) Describe one advantage the monolithic modular approach has over the microkernel approach. Monolithic kernels are typically higher performance, and may be easier to write. (b) Describe one advantage the microkernel approach has over the monolithic modular approach. When one portion of the microkernel has a critical error, it doesnt necessarily bring down the rest of the microkernel with it. This can make microkernels more reliable. (c) If security is a bigger concern than speed (as it perhaps is today), which operating system design approach would you choose? Either answer is ne, as long as there is a good defense. Having said that, a microkernel makes it harder for rogue kernel processes to mess up the system. Compromising a single server process wouldnt necessarily compromise others. On the other hand, it may be easier to debug a monolithic kernel... 9. Here are some questions for practicing unit conversions: (a) How long is a microyear in seconds? We know that micro = 106 . We also know that a year is 3.155815296 * 107 seconds (source: ask.com). Then, a microyear is 106 * 3.155815296 * 107 31.558 seconds. This is one of those cases where you dont need 8 digits of precision. (b) If a car is traveling 25200 miles per fortnight, fast is it traveling in inches per second? A fortnight is 2 weeks = 14 days = 336 hours = 20,160 minutes = 1,209,600 seconds. 25,200 miles = 133,056,000 feet = 1,596,672,000 inches. 1,596,672,000 inches / 1,209,600 seconds = 1,320 inches/second (equivalent to 75 mph). (c) Micrometers are called microns. How long is a gigamicron? Again, micro = 106 . Giga = 109 . A gigamicron is 106 * 109 meters = 103 meters = 1 kilometer (km). (d) How many bytes are there in 1 terabyte of memory? Memory is measured in powers of 2, not 10, though a gigabyte in disk drive terms is typically a billion bytes. We want to know how many bytes are in a terabyte of memory. A terabyte is 240 bytes, which is 1099511627776 bytes. You get half credit for saying that a terabyte is 1012 bytesits approximately right, and the point of these questions is to get the answers right. (e) The mass of the earth ia 6000 yottagrams. What is that in kilograms? The prex yotta means 1024 . 6000 yottagrams is 6 * 103 * 1024 = 6 * 1027 grams. Since 1 kg = 103 grams, the earth weighs 6 * 1024 kg. 10. On all current computers, at least part of the interrupt handlers are written in assembly language. Why? There are actions (such as saving registers) that happen while an interrupt is being handled. These actions need to be written in assembly because they cannot be expressed in higher level languages. 11. When an interrupt or a system call transfers control to the operating system, a kernel stack area separate from the stack of the interrupted process is generally used. Why? Having a separate area reduces in the need to check that user programs dont overwrite data onto the kernel stack and vice versa. 12. What is the biggest advantage of implementing threads in user space? What is the biggest disadvantage?

Implementing threads in user space can allow a program to run on top of any OS, even if it doesnt support threading. The biggest disadvantage is allowing threads to make system calls that normally cause the OS to block to be made without halting the entire process since that would defeat the point of threading in the rst place. 13. Suppose that we have a message-passing system using mailboxes. When sending to a full mailbox or trying to receive from an empty one, a process does not block. Instead, it gets an error code back. The process responds to the error code by just trying again, over and over, until it succeeds. Does this scheme lead to race conditions? Why or why not? The message-passing system itself will not lead to race conditions, in the worst case it will lead the process starvation. You can argue about what the system does with messages once they are placed in the mailbox, but that is a dierent part of the problem.

You might also like