100% found this document useful (2 votes)
751 views

OS Midterm Solution

This document contains a midterm exam for an operating systems course, including multiple choice and short answer questions about topics like process scheduling, synchronization, and system calls. The short answer questions cover issues like implementing synchronization primitives on uni-processor vs multiprocessor machines, how multithreading improves performance on uni-processors, hardware operations used for synchronization, and differences between user and kernel mode. Sample code is also provided and analyzed that uses processes and forking.

Uploaded by

RofaelEmil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
751 views

OS Midterm Solution

This document contains a midterm exam for an operating systems course, including multiple choice and short answer questions about topics like process scheduling, synchronization, and system calls. The short answer questions cover issues like implementing synchronization primitives on uni-processor vs multiprocessor machines, how multithreading improves performance on uni-processors, hardware operations used for synchronization, and differences between user and kernel mode. Sample code is also provided and analyzed that uses processes and forking.

Uploaded by

RofaelEmil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Alexandria University

Faculty of Engineering
Computer and Systems Engineering
Fall 2014

Midterm Exam
CS333: Operating Systems
Saturday, November 22, 2014
Allowed Time: 90 Minutes

Circle the correct answer.


1. (3 points) All of the following are tasks performed by the operating system, except:
A.
B.
C.
D.

Providing a friendly interface to the users.


Abstraction of resources.
Resource management.
Collecting statistics about execution of processes.

2. (3 points) Which of the following is expected to be the slowest?


A. Function call. B. Library call. C. System call.
D. They are all expected to execute in similar speeds.
3. (3 points) which of the following is the fastest approach to write a program that multiples two
matrixes in parallel?
A. Serial program. B. Multi-threaded. C. Divide the work on multiple processes.
D. Distribute the work on multiple machines using for instance MapReduce.
4. (3 points) Which of the following is triggered by hardware?
A. Interrupts. B. System calls. C. Process creation. D. None of the above.
5. (3 points) Which of the following resources are not shared by threads of the same process?
A. Program code. B. CPU registers. C. Heap. D. Global variables.
Short answer questions.
6. (5 points) Consider the perspective of a developer who codes a multithreaded application using
existing primitives such as locks and condition variables. Which is harder, writing this application for a uniprocessor or multiprocessor machine?
Solution: Both are the same from an application developer perspective.
7. (5 points) Consider the perspective of a developer who codes an operating system. Which is
harder, implementing synchronization primitives such as locks and condition variables for a
uniprocessor or multiprocessor machine?
Solution: Implementing synchronization primitives are harder on multiprocessors. For example a simple approach such as enabling and disabling interrupts will not be suitable for a
multiprocessor. Special hardware instructions such as compare and swap are needed.
8. (5 points) Consider an environment in which there is a one-to-one mapping between user-level
threads and kernel-level threads that allows one or more threads within a process to issue blocking system calls while other threads continue to run. Explain why this model can make multithreaded programs run faster than their single-threaded counterparts on a uniprocessor computer.

1 of 4

Solution: Multi-threaded programs are executed at very high speed (when compared to
their single threaded counter parts) on a uniprocessor machine, wherein there exist 1-1 mapping between user-level threads and Kernel-level threads. This is because Kernel-level-thread
in multi-threaded program enables at least one thread to issue a block system call independently without influencing other threads and thereby allowing other threads to uninterruptly
continue with their execution. However, in single threaded counter parts of multi-threaded
program, machine generally spends a significant amount of time waiting for I/O operation
to be complete. Therefore on uniprocessor machine, a process that was allowed to be blocked
continues executing its other threads.

9. (5 points) List at least two types of hardware operations that are used to implement higher level
synchronization primitives such as locks, semaphores, and monitors.

Solution:
Load and store of a single word.
Disabling and enabling interrupts.
Atomic instructions such as compare and swap.

10. (5 points) Explain the difference between user mode and kernel mode. Why two execution
modes are important for achieving the goals of an operating system?

Solution: In user mode certain areas of memory are protected from the users use, certain
registers are inaccessible, and certain instructions may not be executed.
The distinction between two modes are important for the OS to maintain control over the
hardware and to protect itself from any possible changes made by the programs.

2 of 4

11. (8 points) Consider the following program.

boolean locked [2];


int turn;
void P (int id){
while (true) {
/* acquiring the lock */
locked[id] = true;
turn = 1 - id;
while (locked[1-id] && turn == (1 - id) ) /* do nothing */;
/* critical section */
/* releasing the lock */
locked[id] = false;
}
}
void main(){
locked[0] = false; locked[1] = false; turn = 0;
parbegin (P(0), P(1));
}
(a) (4 points) Does program provide mutual exclusion for two processes? Justify your answer.
Solution: Yes it guarantees mutual exclusion.
(b) (4 points) If we swap two lines in the code so function p() looks as follows, will the program
provide mutual exclusion for two processes? Justify your answer.
void P (int id){
while (true) {
/* acquiring the lock */
turn = 1 - id;
locked[id] = true;
while (locked[1-id] && turn == (1 - id) ) /* do nothing */;
/* critical section */
/* releasing the lock */
locked[id] = false;
}
}
Solution: No. A counter example is as follows:
P(0)

P(1)
turn = 0

turn = 1
locked[0] = true
(locked[1-id] & turn == (1 - id)) false
, exit busy wait
/*critical section*/
locked[1] = true
(locked[1-id] & turn == (1 - id)) false
exit busy wait
/*critical section*/

3 of 4

locked[0]
false
false
true

locked[1]
true
false
false

turn
0
1
1

true
true
true

false
false
true

1
1
1

true
true

true
true

1
1

12. (12 points) In lab #1, you learned about both the fork and waitpid system calls. Consider the
following program that uses them:

int i;
void forkProcess(){
pid_t pid;
int status;
i++;
pid = fork();
if (pid == 0){ /* child */
printf("%d", i);
return;
} else{ /* parent */
i--;
waitpid(pid, &status, 0);
printf("%d", i);
return;
}
}
void main(){
i = 0;
forkProcess();
forkProcess();
}
(a) (4 points) What is the total number of processes (including the main parent process) that
will be created when this program is run?
Solution: Four processes.
(b) (4 points) Show the output that will be printed when this program is run because of the
statement printf (%d, i);. The output should be a sequence of numbers printed by all the
processes that are created when this program runs.
Solution: Note that the fork creates a clone of the process. The child process will have
its own global variables, yet their value will be the same as the parent at the time of
forking. 1 2 1 0 1 0
(c) (4 points) If the line of waitpid(pid, &status, 0); in the function f orkP rocess() is removed
and the program is re-executed, show the output that will be printed in this case. The output
should be a sequence of numbers printed by all the processes that are created when this
program runs.
Solution: The values printed by each process are the same as in part (b), however, the
order of the printing will change based on the scheduling of the processes. Constraints:
The sequence includes two 0s, three 1s, and one 2.
At least one 1 must precede the 2. This is because the first forked child will not
proceed to fork another child (which will print 2) until it has printed 1.
At least one 0 must precede the last 1. This is to guarantee that the main process
has printed 0 before it has forked its second child.
Possible correct answers: 1 1 0 0 2 1, 0 0 1 1 1 2, 0 1 0 1 2 1

4 of 4

You might also like