Time-sharing
1. Definition
A time-sharing (multitasking) operating system lets many users or processes share the same CPU by dividing time into short slices called time quanta, switching rapidly among them. Each user gets an illusion of having the whole machine, because the switch happens tens or hundreds of times per second.
It is an interactive system: the user types a command or clicks, and expects a reply within a fraction of a second. Time-sharing became practical only when CPUs gained hardware timers and interrupts — without a timer, a runaway program would hold the CPU forever.
2. How It Works
time quantum q = 10 ms
CPU: | P1 | P2 | P3 | P1 | P2 | P1 | P3 | ... ← round-robin rotation
▲ ▲ ▲
│ │ └── each process runs at most q, then a TIMER INTERRUPT fires
│ └─────── the OS saves registers of the running process (context save)
└──────────── the OS restores registers of the next process (context restore)
── this save/restore pair is one CONTEXT SWITCH
Requirements for time-sharing: hardware timer and clock interrupts · memory protection (so processes cannot overwrite each other) · a scheduler (usually round robin or priority round robin) · virtual memory (so many processes can be "resident") · a user interface (terminal/GUI) and authentication for multiuser access.
3. Key Terms
| Term | Meaning |
|---|---|
| Time quantum (slice) | Maximum CPU time a process gets before pre-emption; typically 10-100 ms |
| Context switch | Saving the state (PC, registers, memory map) of one process and loading another's — pure overhead, no useful work |
| Pre-emption | Forcibly taking the CPU away from a running process when its slice expires |
| Round-robin scheduling | Processes served cyclically, each for one quantum |
| Response time | Time from a request to the first response — the key metric here |
| Multiprogramming vs multitasking vs time-sharing | Multiprogramming = keep CPU busy with several jobs; multitasking = one user running several tasks; time-sharing = many users/tasks sharing slices with rapid switching |
4. Round-Robin Scheduling Example
Processes A (burst 5), B (4), C (3), arriving at time 0, quantum = 4 ms:
| Time | Running | Notes |
|---|---|---|
| 0-4 | A | A needs 1 more |
| 4-8 | B | B finishes (completion 8) |
| 8-11 | C | C finishes (completion 11) |
| 11-12 | A | A finishes (completion 12) |
| Job | Burst | Completion | Turnaround | Waiting | Response |
|---|---|---|---|---|---|
| A | 5 | 12 | 12 | 7 | 0 |
| B | 4 | 8 | 8 | 4 | 4 |
| C | 3 | 11 | 11 | 8 | 8 |
Average turnaround = 31/3 ≈ 10.33, average waiting = 19/3 ≈ 6.33, average response time = 12/3 = 4.0.
Note the trade-off visible in the numbers: a very small quantum improves response time but increases the number of context switches (overhead); a very large quantum degenerates into FCFS with poor interactivity.
5. Advantages and Disadvantages
| Advantages | Disadvantages |
|---|---|
| Interactive — quick response to every user | Context-switch overhead — CPU time and cache pollution |
| Efficient CPU usage; no user monopolises the machine | Complexity in scheduling, memory protection and accounting |
| Fair sharing; each process makes progress | Security concerns with many simultaneous users |
| Short jobs finish quickly, long jobs still progress | Deadlock and thrashing are possible under memory pressure |
| Users can share data and programs; teaching/terminal labs work well | Continuous storage of every process state costs memory |
| Good for servers with many simultaneous sessions | Not suitable where guaranteed deadlines are needed (use RTOS) |
6. Examples
CTSS and MULTICS (pioneering time-sharing), UNIX/Linux (the classic multiuser time-sharing design: fork, exec, round-robin/CFS scheduling), Windows (pre-emptive multitasking, ~15-20 ms quantum), macOS, VMS, and any modern server in which hundreds of users share one machine. Cloud VMs extend the idea to virtual CPUs shared over many tenants.
7. Exam Points
- Define a time-sharing operating system and explain how it works — 5-7 marks, very frequent.
- What is a time quantum and a context switch? What is the effect of the quantum size? — 5 marks, frequent.
- Differentiate multiprogramming, multitasking and time-sharing — 5 marks.
- Numerical: apply round-robin scheduling with a given quantum and find average turnaround/waiting/response times — 5 marks.
- Advantages and disadvantages of time-sharing over batch processing — 5 marks.
- Which hardware features make time-sharing possible and why? — 3-5 marks.