COMP 3361: Operating Systems 1
Midterm
Autumn 2009
Name: ______________________________________________________________
Instructions
Please make sure your writing and diagrams are legible.
1 of 12
1.
(10 points) [Virtual Memory]
[Part A] Consider a system with 3 physical frames of memory (RAM) that is given the following virtual
page memory reference sequence:
1, 3, 6, 7, 1, 3, 6, 7, 1, 3, 6, 7
What is the number of page faults that would occur for each of the following page replacement
algorithms?
(a)
FIFO
(b) LRU
(c) Does an optimal page replacement algorithm exist that does not require future knowledge for
cyclical memory reference sequences such as shown above? If so, give a general algorithm. If not,
explain why.
[Part B] Suppose there are 16 virtual pages and 4 page frames. Determine the number of page faults that
will occur with the reference string to the following virtual pages: 0 1 2 3 2 4 1 3 5 6 1 3 2 7 4 8.
Assume the page frames are initially empty, using each of the following page replacement algorithms:
(a) FIFO (b) LRU (c) Optimal. (12 points)
2 of 12
2.
(10 points) [Process Scheduling] Suppose there are four processes (P1 - P4) with respective arrival
times of 0, 10, 20, and 40, priorities 1, 2, 3, and 4, and job times of 30, 20, 50, and 20 ms. These
processes are scheduled from a single run queue to run on a dual-processor machine.
Suppose the context switch overhead is 2 ms. What is the average turnaround time for scheduling the
four processes using the following preemptive schedulers: priority (highest-priority-first, use 1 as the
highest priority), round-robin, or shortest-job-first? For round-robin scheduling, assume a 20 ms time
quantum. Show your work and make a table of your results.
3 of 12
3.
(10 points) [Deadlock Detection] Perform the Banker’s Algorithm on the following example data.
Is the following system of four processes with 2 resources deadlocked? If yes, show the process
sequence.
Current allocation matrix
P1
P2
P3
P4
Current request matrix
P1
P2
P3
P4
Availability Vector
1
1
4
1
2
3
1
2
0
1
4
1
5
2
3
7
1
4
[Part A] If the availability vector is as below, is the system above still deadlocked?
2
3
[Part B] Is the system deadlocked if the availability is
2
4
4 of 12
4.
(10 points) [Resource Allocation Graphs]
[Part A] A system is composed of four processes, p1 through p4, and three types of consumable
resources, R1 through R3. There is one unit each of R1 and R3 available.
• p1 requests a unit of R1 and a unit of R2.
• p2 produces a unit of R1 and a unit of R2 and requests one unit of R3.
• p3 requests a unit of R1 and a unit of R2.
• p4 produces a unit of R2 and requests one unit of R3.
Show the resource allocation graph to represent the system state. Which, if any, of the processes are
deadlocked in this state? Can the deadlock be avoided by some sequence of execution of the processes?
[Part B] P is a set of processes. R is a set of resources. E is a set of request or assignment edges. The
sets P, R, and E are as follows:
P = {P1 , P2 , P3 } R = {R1 , R2 , R3 }
E = {P1 → R2 , P2 → R1 , P2 → R2 , P2 → R3 , R1 → P1 , R2 → P3 , R3 → P3 }
R1 has one instance. R2 has two instances. R3 has one instance.
-Draw the resource-allocation graph.
-Is there any deadlock in this situation? Briefly explain.
5 of 12
5.
(10 points) [Memory Space Partition] Given memory partitions of 100 KB, 500 KB, 200 KB, 300 KB,
and 600 KB (in order), how would each algorithm place processes of size 212 KB, 417 KB, 112 KB,
and 426 KB (in order)? Explain for each algorithm, where each of the processes is placed in the
memory.
a)
First-fit
b)
Best-fit
c)
Worst-fit
Which algorithm makes the most efficient use of memory?
6 of 12
6.
(5
points) [Process Creation] How many processes will be created when the following program is
executed? Assume that all fork system calls are successful. What will be printed?
(Hint: Be careful and draw a picture.)
main()
{
int i = 3;
int ret_val;
while(i > 0)
{
if ((ret_val = fork()) == 0) { /* Child’s code */
printf("In child %d. \n", i);
exit(0);
} else { /* Parent’s code */
printf("In parent %d. \n", i);
i = i - 1;
}
}
}
7 of 12
7.
(10 points) [Lock Structures]
[Part A] What is the meaning of the term busy waiting?
[Part B] Describe the difference between a normal test-and-set spinlock and a read-before-test-and-set
spin-lock. Illustrate your answer with a diagram from each.
[Part C] Explain the circumstance when a read-before-test-and-set would be advantageous over the
former on multiprocessor systems?
8 of 12
8.
(15 points) [Synchronization] Here are three processes. Can they deadlock? If so, give a concurrent
execution (show a combined sequence of the lettered steps of process0 [A,B,C,D,E,F,G,H] and the
numbered steps of process1 [1,2,3,4,5,6,7,8] and symbol steps across process 2 [! @ # $ % ^ & * ? ~] in
which they deadlock and propose a change. There may be any number of deadlocks. There are 3 mutex
variables:
lock *mutex0, *mutex1, *mutex2;
void process0() {
lock_acquire(mutex0); //Step A
/* do something */
lock_acquire(mutex1);
/* do something */
//Step B
lock_release(mutex0);
/* do something */
//Step C
lock_release(mutex1);
/* do something */
//Step D
lock_acquire(mutex2);
/* do something */
//Step E
lock_acquire(mutex0);
/* do something */
//Step F
lock_release(mutex0);
/* do something */
lock_release(mutex2);
//Step G
//Step H
}
void process1() {
lock_acquire(mutex0);
/* do something */
//Step 1
lock_release(mutex0);
/* do something */
//Step 2
lock_acquire(mutex1);
/* do something */
//Step 3
lock_release(mutex1);
/* do something */
//Step 4
lock_acquire(mutex2);
/* do something */
//Step 5
lock_release(mutex2);
//Step 6
}
9 of 12
void process2() {
lock_acquire(mutex0);
/* do something */
//Step !
lock_acquire(mutex1);
/* do something */
//Step @
lock_release(mutex1);
/* do something */
//Step #
lock_release(mutex0);
/* do something */
//Step $
lock_acquire(mutex2);
/* do something */
//Step %
lock_acquire(mutex0);
/* do something */
//Step ^
lock_acquire(mutex1);
/* do something */
//Step &
lock_release(mutex0);
/* do something */
//Step *
lock_release(mutex1);
/* do something */
//Step ?
lock_release(mutex2);
//Step ~
}
Illustrate your answer.
Are there deadlocks that can arise between process0 and process1?
Are there deadlocks that can arise between process0 and process2?
Are there deadlocks that can arise between process1 and process2?
10 of 12
9.
(10 points) [Miscellaneous] Some questions may have more than correct multiple-choice answer.
Which is not able to solve the race condition?
(A) Test and Set Lock (B) Shared memory (C) Semaphore (D) Monitor
CPU Scheduling algorithms are used for:
(A) Picking one of the ready processes in main memory to run next
(B) Putting to sleep and waking up processes in an efficient manner
(C) Allocating memory to the processes in a fair and efficient way
(D) None of the above
Which is not a CPU scheduling criterion?
(A) CPU utilization (B) Throughput (C) Waiting time (D) Burst time
Which is not the necessary condition of a deadlock?
(A) Mutual exclusion (B) Hold and wait (C) Preemption (D) None of the above
A computer provides the user with virtual address space of 2^24 words. Pages of size 4096
(2^12) words If the hexadecimal virtual address is 123456, the page number in hexadecimal would be:
(A) 123 (B) 1234 (C) 456 (D) 3456
The modified (dirty) bit is used for the purpose of:
(A) Dynamic allocation of memory used by one process to another
(B) Implementing FIFO page replacement algorithm
(C) To reduce the average time required to service page faults
(D) None of the above
Which is a preemptive scheduling?
(A) SJF (B) FCFS (C) RR (D) None of the above
Which of the following scheduling algorithms could result in starvation?
(B) SJF (B) FCFS (C) RR (D) None of the above
Which algorithms prevent starvation?
(C) SJF (B) FCFS (C) RR (D) None of the above
List 3 different ways to perform inter-process communication?
11 of 12
9.
(10 points)
The following three interacting processes share two semaphores initialized as follows:
Semaphore S1 = 3;
Semaphore S2 = 0;
P1:
P2:
P3:
while (true) {
wait (S1);
print (‘C’);
signal (S2);
}
while (true) {
wait (S2);
print (‘A’);
print (‘B’);
signal (S2);
}
while (true) {
wait (S2);
print (‘D’);
}
a. Assuming execution is eventually halted, how many C’s are printed when the set of processes runs?
Explain your answer.
b. Assuming execution is eventually halted, how many D’s are printed when the set of processes runs?
Explain your answer.
c. Is CABABDDCABCABD a possible output sequence when this set of processes runs? Explain your
answer?
d. Is CABACDBCABDD a possible output sequence when this set of processes runs? Explain your
answer?
12 of 12
© Copyright 2026 Paperzz