CS205 - Operating Systems - Spring 2019 - Final - Solution
CS205 - Operating Systems - Spring 2019 - Final - Solution
Question Your
no. answer
1.1 a
1.2 b
1.3 b
1.4 F
1.5 T
1.6 F
1.7 F
1.8 a,b
1.9 c,d
1.10 a
1.11 c
1.12 a
Question 2: [4 marks]
Consider the following code snippet that creates multiple processes. Assume that the sleep(x) method puts the process in the
waiting queue for x seconds.
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
pid t pid;
pid = fork();
if (pid == 0) { /* Lets call this process 1 */
sleep(5);
}
else {
pid_t pid1;
pid1 = fork();
if(pid1 == 0)
{ /* Lets call this process 2 */
Sleep(20);
}
else
{
sleep(10);
Wait(Null);
}
return 0;
}
For how much time? _______ Until Init process calls the wait ______________
Draw a Gantt Chart for the above mentioned processes using the following scheduling algorithm.
"Scheduler assigns each process a time quantum and a priority. The initial value of a time quantum is 5. However, every time a
process has been allocated the CPU and uses its entire time quantum (does not block for I/O), 1 is added to its time quantum,
and its priority level is incremented. When a process blocks before using its entire time quantum, its time quantum is reduced by
1, but its priority remains the same."
Use the following table to show your gantt chart. Describe the running process inside the cell while mentioning the scheduling
time below it as shown for the P1.
Option 1: Non-Pre-emptive Scheduler (100 % correct solution – deduct 1 mark for using non-pre-emptive while the question
asked for pre-emptive solution)
P1 P4 P4 P5 P1 P3 P2
1 6 11 17 19 25 30 32
P1 P4 P5 P4 P1 P3 P2
1 6 11 13 19 25 30 32
P1 P4 P4 P5 P1 P1 P3 P2
1 4 9 15 17 21 25 30 32
P1 P4 P4 P5 P3 P1 P1 P2
1 4 9 15 17 22 26 30 32
P1 P4 P4 P5 P1 P3 P1 P2
1 4 9 15 17 21 26 30 32
P1 P4 P5 P4 P1 P1 P3 P2
1 4 9 11 17 21 25 30 32
Rough Work:
A tribe of savages eats communal dinners from a large pot that can hold M servings of stewed missionary1. When a savage wants
toeat, he helps himself from the pot, unless it is empty. If the pot is empty, the savage wakes up the cook and then waits until
the cook has refilled the pot.
while(true) {
getServingFromPot();
eat (); }
And one cook thread runs this code:
while(true){
putServingsInPot(M); }
Puzzle: Add code for the savages and the cook that satisfies the synchronization constraints.
Hint: you need to use the following variables to complete the write-up of this puzzle:
putServingsInPot(M); GetServingFromPot();
Eat();
class Stack {
private:
int* a; // array for stack
int max; // max size of array
int top; // stack top
Semaphore full = 0;
Semaphore empty = MAX;
Semaphore mutex = 1;
public:
Stack(int m) {
a = new int[m];
max = m;
top = 0;
}
void push(int x) {
wait(empty);
wait(mutex);
a[top] = x;
++top;
signal(mutex);
signal(full);
}
int pop() {
wait(full);
You can see from the code that a process does busy waiting if it calls push() when the stack is full, or it calls pop() when the stack
is empty.
Consider running the functions push and pop concurrently, synchronize the code using semaphores with the following
requirements
Just modify the code clearly in the box above. No need to rewrite.
Question 6: [8 marks]
Assume an architecture where every virtual address is of 16-bits and it is translated to a physical address using a 2-level paging
hierarchy. Each virtual address has the format:
3-bits 3-bits 10-bits
Page Dir(outer Page Table (inner page Page Offset
page table) offset table) Offset
Each page-table (inner page-table) entry and page-directory (outer page table) entry is of 12-bits. Both have the same format
given below:
6-bits 3-bits 1-bit 1-bit 1-bit
Base address of Page-Table/Page Reserved Read Write Valid
Read and Write bits, when set, indicate that reading and writing is enabled respectively. If the valid bit is set, it indicates that the
page is in physical memory otherwise it is not present. Three bits are reserved and are ignored for all practical purposes.
Note: Give answers in bytes, KB, or MB when asked about memory size.
a. What is the size of a single page table? (1 mark) 12 bits x 2^3 = 12 bytes.
b. What is the size of a single physical page? (1 mark) 2^10 = 1 KB
c. What is the maximum amount of virtual memory available to any 2^16 = 64 KB
process? (1 mark)
d. What is the size of a physical address in number of bits?(1 mark) 16 bits
e. What is the maximum physical memory that can be utilized by a 64 KB
user process in this system? (1 mark)
Using the same architecture, you are given the following state of page 12 KB
directory (outer page table) and page tables (inner page table) for a
particular process as shown in Figure 1. . All values are in hex. Addresses
increase from bottom to top of each table.
f. What is the total amount of physical memory currently being used
by the process?(3 mark)
Question 7: [7 marks]
The malloc() library call allocates a 4 MB chunk of virtual memory from the heap portion of a process and returns the virtual
address of the first byte (which is stored in buf). Assume that NONE of this 4MB virtual memory is mapped to physical memory
(i.e. as the virtual pages are accessed they are mapped to physical memory and page table entries are setup). Also assume that
the OS and hardware are using paging with a page size of 4KB. Assume that you have enough physical memory, so that you never
need to replace a page. Please note that a char consists of one byte.
a. Will the OS be able to run this code without actually using 4MB of
Yes
physical memory for buf? Answer “yes” or “no”. (1 mark)
b. How much physical memory (for buf array) will actually be in use once
512 * 4 KB = 2 MB
this loop ends? Provide answer in MB’s. (1 mark)
c. How many page faults does the “for loop” cause?(1 mark) 512 page faults
d. Assume now that the page size in your hardware is 8KB instead of 4KB.
Write the total page faults and total physical memory used (in MBs) in Total Page Faults 512
the right box. (2 marks)
Total Physical
Memory Usage (in 4MB
MB’s)
e. Assume that we change the “for loop” in the above code to the Total Page Faults
following. Fill the table in the right. (2 marks)
for(p = buf + 2048; p < end; p += 4096)
Page-size = 4 KB 1024
*p = random();
Page-size = 8 KB 512
a. If we are currently at logical block 8 and want to access logical block 4, how many physical blocks must be read from the disk
for each of the following methods?
b. Consider a file system that used Combined Indexed Allocation method with following details
1. Index table contains 12 direct block addresses
2. Index table contains single, double and triple indirect block addresses
3. Disk blocks are 1 KB in size
4. Pointer to a disk block requires 4 bytes
What is the maximum size of a file that can be stored in this file system? Show your complete calculations below. [Answer should
be in Kilobyte units. No need for unit conversions].
Calculations:
w = Data stored through direct block addresses: 12 * 1KB
1 block can contain block addresses = 1 KB / 4B = 2 ^ 8 = 256 addresses
x = Data stored through single indirect block addresses:
256 * 1 KB
y = Data stored through double indirect block addresses:
256 * 256 * 1KB
z = Data stored through double indirect block addresses:
256 * 256 * 256 * 1KB
Maximum File size = w + x + y + z