Sie sind auf Seite 1von 10

SCHOOL OF ELECTRICAL ENGINEERING AND COMPUTER SCIENCE

COURSE:

CSI3131

INSTRUCTOR:
Stojmenovic
DATE:
2013
TIME:

SEMESTER: SPRING/SUMMER 2013

Ivan
February 12,
16:00 to 17:00

MIDTERM EXAMINATION

NAME and STUDENT NUMBER:

/
_

Mid-Term Exam
This is closed book exam. There are three (3) types of questions in this examination.
Part 1
Part 2
Part 3
Total

Multiple choice
Short answer
Problem solving

9 marks
21 marks
20 marks
50 marks

Answer briefly and to the point. The space allocated for each question is limited. In case
of necessity you may use the back side of the pages to continue and indicate as such.

Page 1 of 10

Multiple choice questions [1 mark each]:


1.

One of the following is true about using interrupt disabling for protecting critical sections:
a)
b)
c)
d)
e)

It guarantees mutual exclusion on single processor machines only


It guarantees mutual exclusion on single and multi processor machines
It guarantees the absence of starvation on single processor machines only
It guarantees the absence of starvation on single and multi processor machines
None of these answers

2. The following proposed solution for the critical section problem with several processes uses
the exchange machine instruction:
Shared variable b is initialized to 1
Process Pi:
repeat
k:=0
repeat {xchg(k,b)} until k=1;
critical section
b:=1;
remaining section
forever
One of the following is true:
a)
b)
c)
d)
e)

It guarantees mutual exclusion but does not guarantee progress and bounded waiting
It is a valid solution to the critical section problem
It does not guarantee mutual exclusion
It may lead to deadlock
None of these answers

3. Consider the following processes that are synchronized with two semaphores:
Initialization: S1.count := S2.count := 0;
Process P1
Statement1;
signal(S1);

Process P2
wait(S1);
Statement2;
signal(S2);

Process P3
wait(S2);
Statement3;

One of the following is true:

Page 2 of 10

a) Statement3 in P3 must be executed after Statement1 in P1 and after Statement2 in


P2
b) Statement3 in P3 could be executed before Statement1 in P1
c) Statement2 in P2 could be executed before Statement1 in P1
d) Statement3 in P3 must be executed before Statement1 in P1 and before Statement2 in
P2
e) None of these answers
4. One of the following is true concerning monitors and condition variable c:
a)
b)
c)
d)
e)

Mutual exclusion is assured only by using cwait(c) and csignal(c)


A call to cwait(c) is always blocking
A call to csignal(c) always unblocks a process (or thread)
Mutual exclusion needs to be programmed explicitly
None of these answers

5. One of the following statements is true about processes and threads:


a) Threads share the same address space, including code segment, data segment, stack
pointer and program counter
b) User threads do not share memory, only kernel threads do
c) Thread library implementing many-to-one model can make use of at most 4 CPUs.
d) When one thread of a process makes exec() call, all threads are terminated.
e) The thread library is responsible for thread scheduling, regardless of which model
(many-to-one, one-to-one, many-to-many) is used.
6. A process switch (when P was running) does not necessarily involve one of the following:
a)
b)
c)
d)
e)

Save the context of the processor.


Update the Process Control Block of P.
Move the Process Control Block of P to the appropriate queue.
Suspension of P.
Selecting another process for execution, assuming that there are others.

7. One of the following statements about scheduling is false:


a) Preemptive scheduler lets the process finish its CPU burst, but preempts the
process when it starts blocking I/O operation.
b) Round Robin is a preemptive scheduler and does not suffer from starvation.
c) High values (near 1) for the coefficient used to predict the length of CPU bursts
allows the Shortest Job First scheduler to better follow rapidly changing processes.
d) In Round Robin, time quantum q must be large with respect to context switch time.
e) Famine is not possible with the Multilevel Feedback Queue with three queue levels.

Page 3 of 10

8. For each of the following statements, mark the appropriate answer (True or False):
(2 points)
a) When a thread calls exec(), the new process gets a copy of each thread of the parent
process. [F] (that might be the case with fork() ,but exec() replaces all threads with a
new process, starting with a single thread)
b) Deferred cancellation is used to postpone the cancellation of a process until its parent is
ready to accept the exit value. [F] (deferred cancellation tells the thread/process to
terminate, it is up to the thread to do that, the parent has nothing to do with that)

Page 4 of 10

Short-answer questions:
1. (5 points) For the following transitions in the 5-state process diagram, identify if it is
possible, and if, so, list how it occurs.
New to running _____________false___________________
Ready to terminated __________false________________________________________
Waiting to ready ______________I/O or event completion ______________________
Ready to running _____________scheduler dispatch ___________________________
Running to ready ____________ interrupt____________________________________
2. (3 points) Describe the three multithreading models.
Many-to-one model: maps many user-level threads to one kernel thread. Thread
management is done by the thread library in user space. The entire process will block if a
thread makes a blocking system call. Because one thread can access the kernel at a time,
multiple threads are unable to run on multiprocessors.
One-to-one model: maps each user thread to a kernel thread. It provides more
concurrency than the many-to-one model by allowing another thread to run when a
thread makes a blocking call; it also allows multiple threads to run in parallel on
multiprocessors.
Many-to-many model: multiplexes many user-level threads to a smaller or equal number
of kernel threads. Developers can create as many user threads as necessary, and the
corresponding kernel threads can run in parallel on a multiprocessor.
3. (3 points) Describe three requirements a solution to Critical Section Problem must satisfy.
Mutual exclusion: only one process can be in the critical section at any moment of time
Progress: if some processes want to enter the CS and there is no process in the CS,
eventually one of the processes will enter the CS.
Bounded Waiting: (starvation free) a process waiting to enter the CS can be overtaken
only finite number of times before being give access to the CS
4. (2 points) The following algorithm proposes an approach for dealing with the critical
section problem (for two tasks Ti and Tj; a task can represent either a process or a

Page 5 of 10

thread). Does it provide a suitable solution? Justify your answer (argue why it is a
solution, or identify which requirements of the CSP are violated and show how).

Task Ti:
while
{
flag[i]=true
while(flag[j]!=true){};
Critical Section
flag[i]=false;
Remainder Section
}

Task Tj:
while
{
flag[j]=true
while(flag[i]!=true){};
Critical Section
flag[j]=false;
Remainder Section
}

There is no mutual exclusion.


5. (2 points) What is the main difference between a process and a thread?
A process has its own memory space, runtime environment and process ID. A thread
which is limited to the concept of line of execution runs inside a process and shares its
resources with other threads. A process can have many threads.
6. (2 points) Briefly explain what is Direct Memory Access and how it works. Why is it
used?
DMA stands for direct memory access. It allows the devices (disk, ) to write/read a
block of data directly to/from memory without involving the CPU. The CPU just tells the
DMA controller (or devices DMA controller) what it wants to have done, and then
receives an interrupt only when the transfer has been finished (or an error occurred). This
offloads the interrupt handling and piece-vice data copying from the CPU.
7. (2 points) What is busy waiting? Under what conditions might it be desirable and
undesirable?
Busy waiting means that a process maintains the CPU while it is waiting for some I/O
operation to complete (rather than giving up the CPU). This is generally undesirable
because the processor could be doing useful work otherwise. This behavior may be
desirable under conditions when we know that the process (usually a kernel process) will
not be waiting for a long time for the I/O operation to complete and that the process must
respond to the completion quickly (within a few processor instructions).
8. (2 points) Why we have two modes: user mode and kernel mode?

Page 6 of 10

In Kernel mode, the executing code has complete and unrestricted access to the
underlying hardware. It can execute any CPU instruction and reference any memory
address. Kernel mode is generally reserved for the lowest-level, most trusted functions of
the operating system. Crashes in kernel mode are catastrophic; they will halt the entire
PC.
In User mode, the executing code has no ability to directly access hardware or reference
memory. Code running in user mode must delegate to system APIs to access hardware or
memory. Due to the protection afforded by this sort of isolation, crashes in user mode are
always recoverable. Most of the code running on your computer will execute in user
mode.

Page 7 of 10

Problem solving part:


1. (10 points) Consider the following set of processes, with the length of the CPU burst
given in milliseconds.
Process
Burst Time
P1
5
P2
1
P3
1
P4
2
P5
10
The processes are assumed to have arrived in the order P1, P2, P3, P4, P5, at times 0,1,2,3,4,
respectively.
a) Draw three charts that illustrate the execution of these processes using FCFS, SJF
(non-preemptive), and RR (quantum = 2) scheduling algorithms.

10

15

20

25

30

FCFS: p1 (0, 5), p2 (5, 6), p3 (6, 7), p4 (7, 9), p5(9,19)

10

15

20

25

30

SJF: p1 (0, 5), p2 (5, 6), p3 (6, 7), p4 (7, 9), p5(9,19)

10

15

20

25

30

RR: p1(0,2), p2(2,3), p3(3,4), p4(4,6), p5(6,8), p1(8,10), p5(10,12), p1(12,13),


p5(13,15), p5(15,17), p5(17,19)
b) What is the average turnaround time (amount of time from job submission to its
completion) of P4 for each of the scheduling algorithms in part (a)?
FCFS: 6
SJF: 6
RR: 3
c) What is the average waiting time (amount of time the process has been waiting in the
ready queue) of P4 for each of the scheduling algorithms in part (a)?
FCFS: 4
SJF: 4

Page 8 of 10

RR: 1
2. (10 points) In the solution presented in class, a philosopher eats if his/her neighbors do not eat
and he/she is hungry. This assumes that the philosopher is attempting to grab only two chopsticks
which are to his/her left and the right side. Solve the same problem where philosopher looks for
any two chopsticks on the table. In other words, at most two philosophers could eat
simultaneously, and they could be neighbors.
Thus 5 philosophers think, eat, think, eat, think . In the center is a bowl of rice. Only 5
chopsticks are available. Philosopher requires any two chopsticks for eating.
Define the monitor dp (dining philosophers). It has shared variables. Each
philosopher has its own state that can be: (thinking, hungry, eating). Philosopher i can
only have state[i] = eating if and only if (s)he picks up two chopsticks that become
available.
Conditional variables: each philosopher has a condition self
the philosopher i can wait on self[i] if (s)he wants to eat but cannot obtain 2
chopsticks
Four functions in the monitor
pickup(i) philosopher i wants to pick up chopsticks
putdown(i) philosopher i puts down his/her chopsticks
test(i) test the state of philosopher i
Initialization_code() - initialization
Philosopher i executes the loop:

while(true)
{
think
dp.pickup(i)
eat
dp.putdown(i)
}

Define corresponding functions:


void test(int i) /*(a philosopher eats)
void pickUp(int i) /*(pickup chopsticks)
void putDown(int i) /*(put down chopsticks)
Your solution should satisfy three CSP requirements, if possible. Please argue why or why
not they are met, for each of them.

void test(int i) {
if((state[i]==hungry) && (Chopsticks>=2))
{

Page 9 of 10

State[i]=eating;
Chopsticks--;
Chopsticks--;
Self[i].signal();
}
}
void pickUp(int i){
state[i]=hungry;
test(i);
if(state[i] !=eating)
Self[i].wait();
}
void putDown(int i){
state[i]=thinking;
test((i+1)%5);
test((i+2)%5);
test((i+3)%5);
test((i+4)%5);
}
Mutual exclusion: Yes, though two philosophers eat simultaneously, only one modified
chopsticks at one time.
Progress: No deadlock.
Bounder waiting: It is possible for a philosopher to starve to death.

Page 10 of 10

Das könnte Ihnen auch gefallen