Blog
Operating System CMP-3621 BSCS 5th Mid Past Paper 2016 – UOS Main Campus
University of Sargodha main campus Operating System (CMP-3621) mid-term paper for BSCS 5th, 2016 (30 marks, 1 hour). Retyped from the original paper, with solved answers below.
Solved answers
Q1 – Short questions
1. Context switch: saving the state (PC, registers, memory-management info in the PCB) of the running process and loading the saved state of the next process so the CPU can switch between them. It is pure overhead: no useful work is done during the switch.
2. Trap vs interrupt: an interrupt is an asynchronous signal from hardware (timer, I/O device) that can arrive at any time. A trap (exception) is a synchronous, software-generated interrupt caused by the running program itself, either by an error (divide by zero, invalid memory access) or by a system call request.
3. Kernel: the core of the operating system that runs in privileged (kernel) mode all the time. It manages processes, memory, files and devices, and provides system calls to user programs.
4. Virtual machine: a software abstraction of a complete computer. A hypervisor (VMM) lets several guest operating systems run on one physical machine, each believing it has its own CPU, memory and devices (e.g. VMware, VirtualBox, the Java VM).
5. Time-sharing systems: multitasking systems where the CPU switches between users/jobs so quickly (using a short time slice) that every user can interact with their program at the same time. Response time is usually under a second.
6. DMA (Direct Memory Access): a controller that moves a whole block of data between an I/O device and main memory without the CPU. The CPU only sets up the transfer and receives one interrupt per block instead of one per byte.
Q2 – Threads
Thread [2]: the basic unit of CPU utilisation. It has its own thread ID, program counter, register set and stack, and shares the code, data and open files of its process.
Process vs thread [3]:
| Process | Thread |
|---|---|
| Independent program in execution with its own address space | Lightweight unit inside a process; shares the address space |
| Creation and context switch are expensive | Creation and switching are cheap |
| Communication needs IPC (pipes, message passing, shared memory) | Threads communicate through shared variables |
| A crash in one process does not affect others | A crash in one thread can stop the whole process |
How threads improve IPC [4]: threads of one process already share memory, so they exchange data by reading and writing shared variables instead of asking the kernel to copy data through pipes or message queues. No system call is needed for each message, so communication is faster. Synchronisation (mutexes, semaphores) keeps the shared data consistent.
User-level vs kernel-level threads [4]: user-level threads are managed by a thread library in user space; switching is fast, but the kernel does not know about them, so one blocking system call blocks the whole process. Kernel-level threads are managed by the OS; they can run on several CPUs and block independently, but operations are slower. Threading models:
- Many-to-one: many user threads mapped to one kernel thread (no true parallelism; e.g. Green threads).
- One-to-one: each user thread has its own kernel thread (true parallelism; Windows, Linux).
- Many-to-many: many user threads multiplexed onto an equal or smaller number of kernel threads (flexible; older Solaris).
Working of threads with pthreads [5]:
#include <stdio.h>
#include <pthread.h>
int sum = 0; /* shared by all threads */
void *runner(void *param) {
int n = *(int *)param;
for (int i = 1; i <= n; i++)
sum += i;
pthread_exit(0);
}
int main() {
pthread_t tid; /* thread identifier */
int n = 10;
pthread_create(&tid, NULL, runner, &n); /* create thread */
pthread_join(tid, NULL); /* wait for it to finish */
printf("sum = %d\n", sum); /* prints 55 */
return 0;
}main() creates a new thread with pthread_create(), which starts executing runner() in the same address space. The thread updates the shared variable sum and ends with pthread_exit(). main() waits with pthread_join() and then prints the result. Compile with gcc prog.c -lpthread.
