Midterm Exam 1
810:143(g)
October 10th, 2001
This exam is closed book. However, you may use one sheet (8 ˝ x 11 inches, both sides) of previously prepared notes. There are 7 questions on this exam totaling 50 points. The time limit is 50 minutes. Notice this correlation and use point totals to warn you if you are spending too much (or too little) time on a particular problem.
1. (2 pts each = 10 points) Explain the major differences between the members of each pair
· Process and Thread
A process is a program in execution. Each process must have its own, complete copies of code, registers, and memory. A thread is often called a "lightweight process" Because it only needs to store a thread ID, program counter, register set and stack. Several threads can share the additional "resources (code, memory, data, file pointers, etc). Thus, a process may consist of multiple threads of execution which work together.
· Multiprogramming and Multiprocessing
Multiprogramming is running several processes simultaneously on a single processor.
Multiprocessing is running one or more processes concurrently across multiple processors.
· Batch and Interactive
Batch involves setting up one or more processes to run on their own. Interactive requires activity from the user during execution.
· Deadlock and Starvation
A process experiences starvation when some factor is denying it access to a system resource. This can be external devices (tape drives, disks, printers, etc) or even the CPU. This typically involves a process recieving a "low" priority while other high priority processes continue to enter the system and get the resource first.
Deadlock involves circular waiting. That is, process A is waiting for a resource held by process B which won't release it until it gets a resource held by process C which won't release it .... and so on until we reach a process waiting for a resource held by process A. In this case there is no way for any of the processes to advance.
Deadlock implies Starvation, but not the reverse.
· Spinlock and Counting Semaphore
A spinlock occurs when you are "doing nothing" while waiting for some event to be true
while (can't go on) {
// do nothing
}
A counting semaphore on the other hand puts itself to sleep when it can't go on, and relies on a signal to wake it up. The disadvantage of the spinlock is that it requires CPU cycles to continue to check the condition to see if it can go on. This "wasteful" checking prevents other processes from using the CPU.
2. (5 points)Draw the resource-allocation graph for the following,
Resources having instances: A=1, B=1, C=2, D=1
Processes holding resources P0/A P1/BC P2/CD
Processes waiting for resources P0/B P1/D P2/A
Explain why there is or isn’t deadlock.
Come talk to me if you want to talk about this problem. I am too lazy to draw the graph for the webpage!
3. (5 points)Fill in the Need matrix for the snapshot below
Available P# Allocated Maximum Need
2 1 3 P0 1 2 3 3 4 6 2 2 3
P1 2 1 3 3 2 5 1 1 2
P2 1 2 1 6 3 4 5 1 3
For each of the following, provide whether the additional request is granted OR denied. If granted, list the valid completion sequence. If denied, list the reason. Assume that each request happens from the current snapshot.
P2: 1 0 1
Grant this request - P1 - P0 - P2
P0: 1 0 1
Grant this request - P1 - P0 - P2
4. (5 points) Suppose that a scheduling algorithm (at the level of short-term CPU scheduling) favors those processes that have used the least processor time in the recent past. Why will this algorithm favor I/O bound programs and yet not permanently starve CPU bound programs?
It will favor the I/O-bound programs because of the relatively short CPU burst request by them; however, the CPU-bound programs will not starve because the I/O-bound programs will relinquish the CPU relatively often to do their I/O.
5. (5 points) Describe the actions taken by a kernel to context switch between kernel-level threads. How is this different from the actions taken to context switch between processes?
Assuming that the threads are part of the same larger process, the kernel needs to write the program counter, register set and stack of the outgoing thread to a backing store, and switch to point to the PCB of the incoming thread (to look at the new values in the PC, register and stack).
When performing a context switch between processes, we must update additional information including the code block, the file pointers, the memory etc.
6. (10 points) Assume 4 processes are ready nearly simultaneously but in the order listed below. For each scheduling algorithm, draw a Gantt chart and calculate average wait time and average turn around time. (Show your work)
Burst Time Priority
(higher # = higher priority)
P1 6 1
P2 3 2
P3 12 3
P4 4 4
|
p1 |
p1 |
p1 |
p1 |
p1 |
p1 |
p2 |
p2 |
p2 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p4 |
p4 |
p4 |
p4 |
0+6+9+21=36/4
6+9+21+25=61/4
|
p2 |
p2 |
p2 |
p4 |
p4 |
p4 |
p4 |
p1 |
p1 |
p1 |
p1 |
p1 |
p1 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
7+0+13+3=23/4
13+3+25+7=48/4
|
p4 |
p4 |
p4 |
p4 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
p2 |
p2 |
p2 |
p1 |
p1 |
p1 |
p1 |
p1 |
p1 |
19+16+4+0=39/4
25+19+16+4=64/4
RR (Quantum=3)
|
p1 |
p1 |
p1 |
p2 |
p2 |
p2 |
p3 |
p3 |
p3 |
p4 |
p4 |
p4 |
p1 |
p1 |
p1 |
p3 |
p3 |
p3 |
p4 |
p3 |
p3 |
p3 |
p3 |
p3 |
p3 |
9+3+13+15=40/4
15+6+25+19=65/4
7. (10 points) Another classical IPC problem takes place in a barber shop. The barber shop has one barber, one barber chair, and n chairs for waiting customers, if any, to sit in. If there are no customers present, the barber sits in the barber chair and goes to sleep. When a customer arrives, he has to wake up the sleeping barber. IF additional customers arrive while the barber is cutting a customer’s hair they either sit down (if there are empty chairs) or leave the shop (if all chairs are full. Below is pseudocode representing the variables and semaphores needed, and the method for the barber. Using the data allready defined, write the method for a customer (customer’s equivalent of “cut_hair()” is “get_haircut()”)
#define CHAIRS 5 /* # chairs for waiting customers */
semaphore customers=0; /* # of customers waiting for service */
semaphore barbers=0; /* # of barbers waiting for customers */
semaphore mutex=1; /* for mutual exclusion */
int waiting=0; /* customers are waiting but not being cut */
void barber(void) {
while (TRUE) {
wait(customers); /* go to sleep if # of customers is 0 */
wait(mutex); /* acquire access to ‘waiting’ */
waiting=waiting-1; /* decrement count of waiting customers */
signal(barbers); /* one barber is now ready to cut hair */
signal(mutex); /* release ‘waiting’ */
cut_hair(); /* cut hair */
}
}
void customer(void) {
wait(mutex);
if(waiting<CHAIRS) {
waiting++;
signal(customers);
signal(mutex);
wait(barbers);
get_haircut();
} else {
signal(mutex);
}
}