Test1 For Spring 05

advertisement
Test 3
CS 3230
Solution
Spring '06
70 Points (5/5/6) Circle Section Number
1 sheet of notes. (Coverage of Chapters 3 – 7)
01
02
1.
Draw the complete finite state machine (picture of all 5 states and 6 transitions) of
the five process state model. You must give the correct name of the transition and
correct name of the states. (6 points)
2.
Name the two states from your textbook that were added to the 5 state model of problem
1. (2 points)
Ready-suspend
3.
and Blocked-Suspend
Define the following terms:
(4 points)
Monitor
A monitor is a high-level software construct to enforce mutual exclusion of shared
resources.
Condition variable
A condition variable is a specialized monitor variable upon which a thread/process can wait
or signal.
Microkernel (indicate what it contains)
A microkernel is the core of the operating system that contains only the essential operating
system functions.
thread
A thread is a dispatchable unit of work [unit of dispatch].
4.
Name and describe the 2 major characteristics associated with a process that are
treated independently by most operating systems. (2)
Resource Ownership is the characteristic that involves a process owning several resources
which are allocated to it and kept track of in the process image.
Scheduling-execution is the characteristic that indicates that a process follows an
execution path that may be interleaved with other processes or threads.
5.
Name only (don’t describe) any 2 of the seven benefits of a microkernel organization of
an operating system. (2 points)
Uniform Interface, Extensibility, flexibility, portability, reliability, distributed-systemsupport, and support for object-oriented operating system [only 2 should be listed].
6.
Name and describe the two sets associated with Java objects.
indicate how threads get on and off these sets. (6 points)
In your description,
The Java wait set is the set of all threads that called the Java wait() method. Threads are
taken off the Java wait set when some other thread calls notify() or notifyAll() method.
The Java entry set is the set of threads waiting for the lock of an object. Threads get onto
the entry set in one of two ways.
Method 1 is where a thread calls a synchronized
method of an object, but some other thread possesses the lock of the object. The second
method is when notify/notifyAll is called and thread(s) are put from the wait set onto
the entry set. Finally a thread is taken off the entry set once the lock is given up by
a thread exiting a synchronized method and the JVM dispatcher chooses it to get off the
entry set and be the next runnable thread.
7.
The following are questions pertaining to Chapter 5 on Mutual Exclusion (ME). (12
points)
a.
Name the 3 software solutions to ME. Indicate which one works for 2 or more processes.
(4)
Dekker’s Algorithm, Peterson’s Algorithm and Lamport’s Bakery Algorithm (this one works for
number of processes >= 2)
b.
Name the 4 remaining solutions to ME discussed in chapter 5.
(4)
Hardware instruction (testset), Semaphores, Monitors & Message Passing.
c.
With respect to monitors, Mr. Scanlan discussed two implementations of the signal
statement. They were “signal & exit” and “signal & continue”. Describe both of these
approaches. (4 of the 12 points)
The signal/exit implementation of monitors is when the signaler immediately exits from the
monitor once it has given a signal and then the signaled thread immediately enters the
monitor.
The signal/continue implementation of monitors is when the signaler continues executing code
in the monitor after it has issued a signal; the signaled thread is placed on an entry
queue to enter the monitor.
8.
Complete the following code for a semaphore (general/counting). (4)
struct semaphore {
int count;
queueType queue;
};
void wait(semaphore& s)
{
// I have added the & to make s an in/out parameter
s.count--;
if (
s.count < 0
)
{
place this process in s.queue;
block this process
}
}
void signal(semaphore& s)
{
s.count++;
// I have added the & to make s an in/out parameter
if (
s.count <= 0
)
{
remove a process P from s.queue;
place process P on ready list;
}
}
9.
Using the functions from problem #8 and given that a call to the function CS() (a void
function) is a call to a critical section for a process. And given that Mutex is a
semaphore so that this process has access to its critical section. Write 3 lines of C++
code to allow this process access to its critical section using the semaphore Mutex.
You may assume that Mutex.count is set to 1 initially. (3)
wait(mutex);
CS();
signal(mutex);
10.
Name and describe the 4 necessary and sufficient conditions for deadlock. (8 points)
Mutual exclusion is a condition in which only one process at a time can use a resource.
Hold and wait is a condition in which a process holding at least one resource is waiting to
acquire additional resources held by other processes.
No preemption is a condition in which a resource can be released only voluntarily by the
process holding it, after that process has completed its task.
Circular wait is a condition in which there exists a set of waiting processes such that the
first process is waiting for a resource that is held by a second process, the second
process is waiting for a resource that is held by third process etc. Up to the last
process is waiting for a resource that is held by the first process.
11.
Given the following state for the Banker’s Algorithm.
•
5 processes P0 through P4; 4 resource types A (13 instances),
B (13 instances), C (9 instances), and D (13 instances).
•
Snapshot at time T0:
P0
P1
P2
P3
P4
a.
Allocation
A B C D
1 0 2 0
0 3 1 2
2 4 5 1
3 0 0 6
4 2 1 3
Max
A B
3 2
3 5
2 7
5 5
6 2
C
4
1
7
0
1
D
2
2
5
8
4
Available
A B C D
3 4 0 1
Verify that the Available array has been calculated correctly.
number of A’s
number of B’s
available = 13 – (1 + 0 + 2 + 3 + 4) = 3
available = 13 – (0 + 3 + 4 + 0 + 2) = 4
(1 point)
number of C’s
number of D’s
b.
Calculate the Need matrix. (2 points)
P0
P1
P2
P3
P4
c.
available = 9 – (2 + 1 + 5 + 0 + 1) = 0
available = 13 – (0 + 2 + 1 + 6 + 3) = 1
A
2
3
0
2
2
B
2
2
3
5
0
C
2
0
2
0
0
D
2
0
4
2
1
Show that the current state is safe, that is, show a safe sequence of processes. In
addition, to the sequence show how the Available (working array) changes as each
process terminates. (2 points)
There are multiple answers for this problem.
A safe sequence is
After P4 finishes,
A B
7 6
P4, P1, P0, P2 and P3
Available is
C D
1 4
After P1 finishes,
A B
7 9
After P0 finishes,
A B
8 9
After P2 finishes,
A B
10 13
After P3 finishes,
A B
13 13
Available
C D
2 6
Available
C D
4 6
Available
C D
9 7
Available
C D
9 13
is
is
is
is
d.
Given the request (2,0,0,0) from Process P0. Should this request be granted? Why is
it safe or why not safe? (3 points) ANSWER THE QUESTION ON THE BACK SIDE OF THE LAST
PAGE.
Assume to grant the request of (2, 0, 0, 0) and one would have the following situation:
Allocation
Max
Available
A B C D
A B C D
A B C D
P0
3 0 2 0
3 2 4 2
1 4 0 1
P1
0 3 1 2
3 5 1 2
P2
2 4 5 1
2 7 7 5
P3
3 0 0 6
5 5 0 8
P4
4 2 1 3
6 2 1 4
Now the new Need Matrix would be:
P0
P1
P2
P3
P4
A
0
3
0
2
2
B
2
2
3
5
0
C
2
0
2
0
0
D
2
0
4
2
1
This state is UNSAFE!!! Do NOT grant this request. There are NOT enough Cs & Ds available
for P0 and P2. There not enough As available for P1. There are not enough As, Bs and
Ds available for P3. There are not enough As available for P4.
12.
Given a logical address for simple paging is given by two numbers, pageNum (a
page number) and the offset (offset within the page) and given PT (the page table) and
SZ (size of page), show a formula for the physical/absolute address that would
correspond to the logical address of (pageNum, offset). (3 points)
PT[pageNum] * SZ + offset == absolute address
13.
Define simple paging and simple segmentation.
(4 points)
Simple paging is the process of dividing memory into equal-size chunks and divide a process
image into the same size chunks.
Simple segmentation is the process of dividing memory into different size chunks and process
is divided into different size chunks. Process chunk sizes vary depending on the size
of modules/classes and data structures.
14.
Complete the following wantToCut method in the slba.java (Sleeping Barber) file from
mp70. Don’t forget that the methods on semaphores were named .P() and .V()(6 points)
class Salon {
private
private
private
private
private
private
private
// the constructor is NOT shown, BUT READ COMMENTS ON INITIAL VALUES OF
//
SEMAPHORES!!!
int numChairs = 0;
Semaphore customers = null; // Set to 0 initially in constructor
Semaphore barber = null;
// Set to 0 initially in constructor
Semaphore mutex = null;
// Set to 1 initially in constructor
Semaphore cutting = null;
// Set to 0 initially in constructor
String name = null;
int waiting = 0;
public void wantToCut() {
System.out.println("age=" + SmallScheduler.age()
+ ", Barber free, waiting for a customer");
customers.P();
// wait on a customer
mutex.P();
//
//
//
//
//
//
waiting--;
barber.V();
grab the mutex so as to update waiting
variable
decrement waiting since we are
servicing a customer
signal the barber to synchronize with
the customer
System.out.println("age=" + SmallScheduler.age()
+ ", Barber has customer, waiting=" + waiting);
mutex.V();
// give up the mutex
System.out.println("age=" + SmallScheduler.age()
+ ", Barber cutting hair");
cutting.P();
}
// cut hair (hint: use cutting)
Download