Solutions-Mid Term

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Solutions : Mid term ECE397A OS, April 10,2003

A. Short questions (30 %)


1. Difference between synchronous and asynchronous IO methods
In synchronous IO methods, control returns to user program only upon IO
completion while in asynchronous IO, control returns to user program
without waiting for IO completion.
2. Advantage of shared memory communication versus message passing
Shared memory communication is relatively faster than message passing
as there is no kernel interference in case of shared memory.
3. Main advantages of microkernel based OS compared to a monolithic OS
structure
Main advantages of microkernel based OS over monolithic OS are :
1. microkernel based OS is easier to extend
2. microkernel based OS is easier to port to new architecture
3. microkernel based OS is more reliable and secure
4. What happens during a context switch
During context switch, kernel saves the state of the old process in its PCB
(Process Control Block) and loads the saved state of the new process from
PCB.
5. Describe very shortly the two ways of thread creation in Java
In Java, threads can be created either by extending the Thread class or
implementing the runnable interface.
6. What is a TLB and where it is used ?
TLB or Translation Lookaside Buffer is special hardware to cache
fragments of the page table entries. It is used for fast address translation.
B. Processes (20%)
(a) Develop a small program in C using system calls (e.g. fork(), waitpid(), exit(),
kill(), ..) that does the following:
* a parent process creates a child process that prints its PID
* the child creates a grandchild and sleeps 120 seconds

* the grandchild immediatly exits


* the parent sleeps for 60 seconds and then kills all the processes

#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <signal.h>
main()
{
int child_id,grandson_id,myID;
child_id = fork();
if(child_id == 0) /*child process*/
{
myID=getpid();
printf("the child process ID is: %d\n", myID);
grandson_id =fork();
if(grandson_id ==0) /*grandchild process*/
{
printf("grandchild process \n");
exit();
}
sleep(120);
}
sleep(60);
kill(0, SIGKILL);
/* Kill all the child process and itself */
}
(b) Show the memory layout for parent and child after the parents first fork() call
Please refer to lecture notes for processes.
C. Synchronization (20%)
(a) The following algorithm is used to synchronize two processes. Please
comment if it works and if it meets all the requirement of correct synchronization.
Shared Variables :
//Boolean flag[2]; //retain state about each thread
// initially flag[0] = flag[1] = false.
flag[i] = true then P(i) ready to enter its critical section
Process P(i)

do {
flag[i] = true;
While (flag[j]);

Critical section
flag [i] = false;
remainder section
}while (1)
The algorithm provides mutual exclusion but does not meet the progress
requirement of correct synchronization.
Consider the following execution sequence :
T(0): P(i) sets flag[i] = true
T(1): P(j) sets flag[j] = true
Now P(i) and P(j) are looping forever in their respective while statements. Thus
this algorithm is dependent on the exact timing of the two processes.

(b) Change the following implementation of mutual exclusion to an


implementation using the test-and-set synchronization instruction. The test-and-set
instruction tests and modifies the content of a word atomically. The swap instruction
atomically swaps two variables.
do {
key =true ; //Key is local
while (key == true)
swap(lock,key);
Critical section
lock = false;
Remainder section
}
Implementation of mutual exclusion using test-and-set:
do {
while (test-and-set(lock));
Critical section
lock = false;
Remainder section
}
D. Deadlocks (20%)
Show an example of a resource allocation graph where the system is in an unsafe
state. Show that it is possible for the process to complete their execution without
entering a deadlock state.
P1

R2

P2

R1

P1

The figure shows a resource allocation graph in an unsafe state.


If P1 does not request R2, then P1 and P2 can complete their execution without
entering a deadlock state.
E. Scheduling (10%)
Explain the differences in the degree to which FCFS, RR, and Multilevel feedback
queues discriminate in favor of short processes.
FCFS : FCFS as such does not discriminate between short and long processes but short
processes have disadvantage in FCFS scheduling as any short process arriving after long
process will have a longer waiting time.
RR : RR treats all processes equally by giving them equal bursts of CPU time so short
processes can finish faster as compared to FCFS scheduling.
Multi-Level Feedback queue: This scheme discriminates favorably toward short
processes as their chances of going to a lower priority queue is lesser compared to long
processes. Thus short processes can leave the system faster.

You might also like