Exercise Set One (Chapters 1/2/3)

Problem: Define the essential properties of the following types of operating systems:

         a. Batch b. Interactive c. Time sharing d. Real-Time e. Distributed

Answer:

  1. Batch: Jobs with similar needs are batched together and run through the computer as a group by an operator or automatic job sequencer. Performance is increased by attempting to keep CPU and I/O devices busy at all times through buffering, off-line operation, spooling, and multiprogramming. Batch is good for executing large jobs that need little interaction; it can be submitted and picked up later.
  2. Interactive: Composed of many short transactions where the results of the next transaction may be unpredictable. Response time needs to be short(seconds) since the user submits and waits for the result.
  3. Time sharing: Uses CPU scheduling and multiprogramming to provide economical interactive use of a system. The CPU switches rapidly from one user to another. Instead of having jobs defined by spooled card images, each program reads its next control card from the terminal, and output is normally printed immediately to the screen.
  4. Real-Time: A Real Time system must complete certain processes within a fixed time frame else the system will fail. Certain operations such as information retrieval with an unknown time to complete must be bounded by some known number in order to provide such guarantees. This often eliminates the option of using storage other than fast memory. In a soft real-time system, such deadlines are not guaranteed, but certain processes are given a priority over others with out any notion of a deadline to provide for faster completion.
  5. Distributed Systems: These are systems which do not share common disks memory or clocks. Communication and collaboration is achieved through an inter-connected network. Distributed systems can be autonomous and act as an independent entity as in the case of Network operating systems or can appear as a small part of a much larger whole made up of many separate computers collaborating on the same task.

Problem: What are the differences between a trap and an interrupt? What is the use of each function?

Answer:

Problem: Which of the following instructions should be privileged?

  1. Set value of timer
  2. Read the clock
  3. Clear memory
  4. Turn off interrupts
  5. Switch form user to monitor mode

Answer:
The following instructions should be privileged

Problem: When are caches useful? What problems do they solve ? What problems do they cause? If a cache can be made as large as the device for which it is caching ( for instance, a cache as large as a disk), why not make it that large and eliminate the device?

Answer:

Problem:      What are the five major activities of an operating system in regard to process management?

Answer:

  1. The creation and deletion of both user and system processes.
  2. The suspension and resumption of processes
  3. The provision of mechanisms for process synchronization
  4. The provision of mechanisms for process communication
  5. The provision of mechanisms for deadlock handling
  6. What are the three major activities of an operating system in regard to memory management?

Problem:      What are the three major activities of an operating system in regard to memory management?

Answer:

  1. Keep track of which parts of memory are currently being used and by whom
  2. Decide which processes are to be loaded into memory when memory space becomes available
  3. Allocate and deallocate memory space as needed

Problem:     List five services provided by an operating system. Explain how each provides convenience to the users. Explain also in which cases it would be impossible for user-level programs to provide these services.

Answer:

  1. Program Execution: The operating system loads the contents (or sections) of a file into memory and begins its execution. A user-level program could not be trusted to properly allocate CPU time.
  2. I/O operations: Disks, tapes, serial lines, and other devices must be communicated with a very low level. The user need only specify the device and the operation to perform on it, while the system converts that request into device or controller specific commands. User-level programs cannot be trusted to only access devices they should have access to, and to only access them when they are otherwise unused.
  3. File-system manipulation: There are many details in file creation, deletion, allocation, and naming that users should not have to perform. Blocks of disk space are used by files and must be tracked. Deleting a file requires removing the name file information and freeing the allocated blocks. Protections must also be checked to assure proper file access. User programs could ensure neither adherence to protection methods nor could they be trusted to allocate only free blocks and deallocate blocks on file deletion.
  4. Communications: Message passing between systems requires messages be turned into packets of information, sent to the network controller, transmitted across a communications medium, and reassembled by the destination system. Packet ordering and date correction must take place. Again, user programs might not coordinate access to the network device, or they may receive packets destined for other processes.
  5. Error detection: Error detection occurs at both the hardware and software levels. At the hardware level, all data transfers must be inspected to ensure that data have not been corrupted in transit. All data on media must be checked to be sure they have not changed since they were written to the media. At the software level, media must be checked for data consistency; for instance, do the number of allocated and unallocated process- independent (for instance, the corruption of data on a disk), so there must be a global program(the operating system) that handles all types of errors. Also, by having errors processed by the operating system, processes need not contain code to catch and correct all the errors possible on a system.


Chapter 3

Problem 3.1: Describe the differences among short-term, medium-term, and long-term scheduling.

Answer:

Problem 3.2: Describe the actions taken by a kernel to switch context

         a. Among threads  b. Among processes

Answer:

  1. The thread context must be saved(registers and accounting if appropriate), and another thread's context must be loaded.
  2. The same as (a), plus the memory context must be stored and that of the next process must be loaded.


Project 1 Homework
1. Under threads directory if we change the function ThreadTest() in threadtest.cc to the following, can you predict the result of running ``nachos'' without actual executing?


ThreadTest()
{
    DEBUG('t', "Entering SimpleTest");
    Thread *t = new Thread("forked thread");
    Thread *tt = new Thread("forked thread");
    t->Fork(SimpleThread, 1);
    tt->Fork(SimpleThread, 2);
}


Answer: For each iteration of the 'for' loop in SimpleThread(), no matter how many threads are running, the value of the shared variable would only be incremented by one. This because the read/write process is interrupted by a Yield() (each time). The final value of the SharedVariable will be 5.

2. When we run 'nachos' under threads directory with option '-rs 20' option, when does the clock advance? When does context switch actually occur? Describe the main functions involved during this process.



Answer:

The clock advances after each MIPS assembly function is executed. The context switch actually occurs when SWITCH(oldThread, nextThread); is called from Scheduler::Run. Scheduler::Run, Thread::setStatus, Thread::CheckOverflow and SWITCH are the main functions involved.

3. Assume that we need to solve the following problem in Project 1. The local laundromat has just entered the computer age. As each customer enters, he or she puts coins into slots at one of two stations and types in the number of washing machines he/she will need. The stations are connected to a central computer that automatically assigns available machines and outputs tokens that identify the machines to be used. The customer puts laundry into the machines and inserts each token into the machine indicated on the token. When a machine finishes its cycle, it informs the computer that it is available again. The computer maintains an array available[NMACHINES] whose elements are non-zero if the corresponding machines are available (NMACHINES is a constant indicating how many machines there are in the laundromat), and a semaphore nfree that indicates how many machines are available. The code to allocate and release machines is as follows:

int allocate() /* Returns index of available machine. */ { int i; P(nfree); /* wait until a machine is available */ for (i=0; i < NMACHINES; i++) if (available [i] != 0 { available[i] = 0; return i; } } release(int machine) /* Release machine */ { available[machine] = 1; V(nfree); } The available array is initialized to all ones, and nfree is initialized to NMACHINES. It seems that if two people make request at two stations at the same time, they will occasionally be assigned the same machine. This has resulted in several brawls in the laundromat, and you have been called in by the owner to fix the problem using locks and condition variables. Assume that one thread handles each customer station. What is wrong with the following code for Ex4 of Project 1:

int allocate()  {
   lock_machine->Release();
   int i = 0;

   lock_machine->Acquire();
   for (i=0; i < NMACHINES; i++) {
      if (available [i] != 0) {
         available[i] = 0;
         lock_machine->Release();
         return i;
      }
   }

   return -1;
}

void release(int machine, int cus_num) {
   lock_machine->Acquire();
      available[machine] = 1;
   lock_machine->Release();
}


Answer:

If all the laundry machines were allocated and a thread attempted to call allocate(), it would Acquire() the lock then exit the function with the lock still acquired. This would prevent any other threads from checking the status of the machines.