Solutions for Problem Set 1 - Operating Systems | COMP 310, Assignments of Operating Systems

Material Type: Assignment; Class: Operating Systems; Subject: Computer Science; University: University of San Diego; Term: Unknown 1989;

Typology: Assignments

Pre 2010

Uploaded on 08/19/2009

koofers-user-783
koofers-user-783 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMP 310: Problem Set 1: Answers
All problems from OS Concepts by Silberschatz, Galvin, and Gagne
4.3 A DECSYSTEM-20 computer has multiple register sets. Describe the actions of a context
switch if the new context is already loaded into one of the register sets. What else must happen if
the new context is in memory rather than in a register set, and all the register sets are in use?
During a context switch, all the information about the current process (P1) must be saved to that
process’s PCB, and the information about the next process (P2) must be loaded. If P2’s state is
already loaded into registers, then the values of P1’s registers can remain in place, but P1 needs to
record which register set it was using (if it hasn’t done so already). P1’s state should be changed
to Ready (assuming it got swapped out because of a scheduler interrupt). Then, P2’s address space
must be swapped in. This includes restoring access to any files P2 was working with, access to
it’s call stack, etc. Finally, the system simply needs to switch to using P2’s registers as the current
register set and set P2’s state to running.
If P2’s registers are not already loaded in, then P1’s registers must be saved to P1’s memory (P1’s
PCB) before P2’s address space replaces P1’s. Then, P2’s registers must be loaded into this register
set from P2’s PCB before P2 starts running.
Note that in a context switch, the address space swap (not the register copy) is the most time
consuming step (think about the difference between thread context switching and process context
switching). So even though having P2’s data already loaded into registers saves some time, it does
not save that much time relative to the total time of the context switch.
5.1 Provide two programming examples of multithreading that improve performance over a single-
threaded solution.
Some possible answers include:
A text recognition program. The user’s strokes could be captured and rendered as they are
drawn in one thread while the recognition proceeds in the other so that the strokes may be
rendered in real time even if the recognition lags behind.
A database lookup program. Several threads could for information in the database, starting
at different points, to find the information more quickly.
Computer games. Separate input, rendering, calculations.
1
pf3

Partial preview of the text

Download Solutions for Problem Set 1 - Operating Systems | COMP 310 and more Assignments Operating Systems in PDF only on Docsity!

COMP 310: Problem Set 1: Answers

All problems from OS Concepts by Silberschatz, Galvin, and Gagne

4.3 A DECSYSTEM-20 computer has multiple register sets. Describe the actions of a context switch if the new context is already loaded into one of the register sets. What else must happen if the new context is in memory rather than in a register set, and all the register sets are in use?

During a context switch, all the information about the current process (P1) must be saved to that process’s PCB, and the information about the next process (P2) must be loaded. If P2’s state is already loaded into registers, then the values of P1’s registers can remain in place, but P1 needs to record which register set it was using (if it hasn’t done so already). P1’s state should be changed to Ready (assuming it got swapped out because of a scheduler interrupt). Then, P2’s address space must be swapped in. This includes restoring access to any files P2 was working with, access to it’s call stack, etc. Finally, the system simply needs to switch to using P2’s registers as the current register set and set P2’s state to running.

If P2’s registers are not already loaded in, then P1’s registers must be saved to P1’s memory (P1’s PCB) before P2’s address space replaces P1’s. Then, P2’s registers must be loaded into this register set from P2’s PCB before P2 starts running.

Note that in a context switch, the address space swap (not the register copy) is the most time consuming step (think about the difference between thread context switching and process context switching). So even though having P2’s data already loaded into registers saves some time, it does not save that much time relative to the total time of the context switch.

5.1 Provide two programming examples of multithreading that improve performance over a single- threaded solution.

Some possible answers include:

  • A text recognition program. The user’s strokes could be captured and rendered as they are drawn in one thread while the recognition proceeds in the other so that the strokes may be rendered in real time even if the recognition lags behind.
  • A database lookup program. Several threads could for information in the database, starting at different points, to find the information more quickly.
  • Computer games. Separate input, rendering, calculations.
  • A web client that is attempting to load multiple pictures.

5.2 Provide two programming examples of multithreading that do not improve performance over a single-threaded solution.

Some possible answers include:

  • A complex calculation, where each step depends on the result from the previous step.
  • A timer. Multithreading is unnecessary and could slow it down.
  • A program to record (but not recognize) speech. There is only one sequential process, and it is important that it run in real time.

5.3 What are two differences between user-level threads and kernel-level threads? Under what circumstances is one type better than the other?

  • User-level threads are faster to create and require less overhead than kernel-level threads.
  • The kernel knows about kernel-level threads, but not user-level threads, so K/L threads can be scheduled by the kernel’s scheduler.

K/L threads are better than U/L threads when you will have threads that block, for example, on I/O. In these cases, the scheduler can choose another K/L to run so that the whole process doesn’t block.

U/L threads are better when there will be a lot of them created, or when they will be created and destroyed relatively quickly because there is little creation/destruction overhead. They are also better when the threads need to communicate because thread communication is simply a procedure call in U/L threads, which is faster than going through the kernel.

5.6 What resources are used when a thread is created? How do they differ from those used when a process is created?

When a thread is created, it needs its own execution state. This basically involves allocating it memory for its stack, and giving it a PC, SP, and storage for its other registers, all within the processes address space.

When a process is created, it receives an entirely new address space, which means that the system has to create a new mapping from addresses to physical memory, and it has to allocate the process a new block of physical memory. Within that address space, the system must keep track of the open files, the process’s permissions, etc. It must also allocate things that are required for each