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:
- 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.
- 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.
- 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.
- 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.
- 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:
An interrupt is a hardware-generated
change-of-flow within the system. An interrupt handler is summoned to deal
with the cause of the interrupt; control is then returned to the interrupted
context and instruction. A trap is a software-generated interrupt. An interrupt
can be used to signal the completion of an I/O to obviate the need for
device polling. A trap can be used to call operating system routines or
to catch arithmetic errors.
Problem: Which of the following instructions should be privileged?
- Set value of timer
- Read the clock
- Clear memory
- Turn off interrupts
- Switch form user to monitor mode
Answer:
The following instructions should
be privileged
- Set value of timer
- Clear memory
- Turn off interrupts
- Switch form user to monitor mode
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:
Caches are useful when two or more components
need to exchange data, and the components perform transfers at differing
speeds. Caches solve the transfer problem by providing a buffer of intermediate
speed between the components. If the fast device finds the data it needs
in the cache, it need not wait for the slower device. The data in the cache
must be kept consistent with the data in the components. If a component
has a data value change, and the datum is also in the cache, the cache
must also be updated. This is especially a problem on multiprocessor systems
where more than one process may be accessing a datum. A component may be
eliminated by an equal-sized cache, but only if :
- the cache and the component have equivalent state-saving
capacity ( that is, if the component retains its data when electricity
is removed, the cache must retain data as well) , and
- the cache is affordable, because faster storage
tends to be more expensive.
Problem: What are the five major activities
of an operating system in regard to process management?
Answer:
- The creation and deletion
of both user and system processes.
- The suspension and resumption
of processes
- The provision of mechanisms
for process synchronization
- The provision of mechanisms
for process communication
- The provision of mechanisms
for deadlock handling
- 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:
- Keep track of which parts
of memory are currently being used and by whom
- Decide which processes are
to be loaded into memory when memory space becomes available
- 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:
- 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.
- 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.
- 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.
- 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.
- 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:
- Short-term:(CPU scheduler)-
selects from jobs in memory, those jobs which are ready to execute, and
allocates the CPU to them.
- Medium-term - used especially
with time-sharing systems as an intermediate scheduling level. A swapping
scheme is implemented to remove partially run programs from memory and
reinstate them later to continue where they left off.
- Long-term (job scheduler)
- determines which jobs are brought into memory for processing.
The primary difference is in the frequency of their execution. The short-term
must select a new process quite often. Long-term is used much less often
since it handles placing jobs in the system, and may wait a while for a
job to finish before it admits another one.
Problem 3.2: Describe the actions taken by a kernel to switch context
a. Among threads b. Among processes
Answer:
- The thread context must
be saved(registers and accounting if appropriate), and another thread's
context must be loaded.
- 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.
Note: the '-rs' option passes a known random seed to the NACHOS random number
generator. This interrupts the current running threads at random but deterministic
events.
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.