GATE Computer Science & IT · Operating Systems
Threads
Threads share a process's address space; user-level versus kernel-level scheduling.
maya is still PID 1420. This lesson splits her into a UI thread and a save thread: same heap, same open files, two stacks. Then it asks who schedules those threads — a user-space library (fast, one block stalls all) or the kernel (true parallelism).
- GATE Computer Science & IT
- Medium level
- 3 concepts
1A thread is not a second process
A second process would be a new PID with its own address space. A second thread of maya is still PID 1420. kill(1420) hits every thread. An open file table is shared: if the save thread closes essay.txt, the UI thread sees it closed.
Use a process when you want isolation — Spell should not be able to scribble on maya's heap. Use a thread when you want cheap concurrency inside one program — UI and save taking turns on one PID.
Figure. One process, two stacks. The heap and PID are shared; each thread has its own PC.
What is shared
- SharedCode, global data, heap, open files, PID 1420.
- PrivateEach thread's stack, registers, and program counter.
- Not a PIDThreads are not processes. The kernel may still schedule them, but the PID is the process's.
| Thing | New process | New thread of maya |
|---|---|---|
| PID | New number | Still 1420 |
| Address space | Cloned or new | Shared |
| Stack / PC | Its own | Its own |
| Open files | Usually copied | Shared table |
Which resource do threads of maya NOT share?
- Their stacks and program counters
- The process's open file table
- The process's global data
Stacks and PCs are per thread. Files and globals belong to PID 1420.
3User-level versus kernel-level threads
Who schedules the UI thread and the save thread? Two answers, and they are not the same speed.
User-level threads are switched by a library inside PID 1420. The kernel sees one schedulable entity — the process. A switch is a function call, so it is fast. The cost: if the save thread issues a blocking disk read, the kernel blocks the whole process. The UI thread freezes too, because the kernel does not know it exists.
Kernel-level threads are known to the OS scheduler. Each thread is a schedulable entity. The save thread can block on disk while the UI thread keeps Running. That is true parallelism on a multi-core machine, and a slightly more expensive switch, because it is a real context switch.
Figure. Kernel-visible threads can overlap I/O; pure user threads share one kernel schedulable entity.
How each kind is scheduled
- User-levelThe library maps many user threads onto one (or a few) kernel entities; the kernel is unaware of the rest.
- Kernel-levelEach thread is a kernel scheduling unit, so blocking and multiprocessor dispatch are per-thread.
- Trade-offUser-level wins on switch cost; kernel-level wins on blocking behaviour and parallelism.
| Property | User-level | Kernel-level |
|---|---|---|
| Who schedules | User library | OS kernel |
| Switch cost | Low (no kernel trap) | Higher (kernel involved) |
| One blocking call | Can block all threads of the process | Blocks only that thread |
| True parallelism | No (one kernel entity) | Yes, across cores |
How many threads run at once
A pure user-level library maps 8 of maya's user threads onto 1 kernel-scheduled entity, running on a 4-core CPU. At most how many of maya's threads execute in true parallel at one instant?
- cores available4
- kernel-scheduled entities the library exposes1 (the kernel sees one process)
- true parallelism = min(kernel entities, cores) = min(1, 4)1 thread at a time
Pro tip. Pure user-level threads share one kernel entity, so extra cores cannot help; parallelism is capped by the number of kernel-scheduled units, not by the core count.
A process uses purely user-level threads. One thread blocks in a read() system call. What happens to the others?
- They keep running on other cores
- They are also stalled, because the kernel scheduled the whole process
- The library preempts the blocked thread automatically inside the kernel
The kernel sees one process. When that process blocks in the kernel, every user-level thread sharing it stalls until the blocking call returns.
Notes
- Threads of one process share code, data and open files; each has its own stack, registers and PC.
- User-level threads are fast to switch and invisible to the kernel; one blocking call blocks all.
- Kernel-level threads are scheduled by the OS and can run in parallel on several cores.
Formulas
- A thread context switch is cheaper than a process switch when the address space (and TLB) need not change.
Exam traps & shortcuts
- If the question says 'one blocking call blocks all threads', the package is user-level.
- Shared heap is the race: two threads, one global, need a lock.
Reference tables
Save thread reads the disk. What happens to the UI thread?
| Thread package | Kernel sees | UI thread during a blocking read |
|---|---|---|
| User-level | One process, PID 1420 | Stalls — the process is blocked |
| Kernel-level | Two schedulable threads | Can keep Running |
Recap
A thread is a stack inside a PID. User-level packages hide extra threads from the kernel.
- Share
- Code, heap, files, PID. Private: stack, regs, PC.
- Not a process
- No new PID. Isolation wants a process; cheap concurrency wants a thread.
- User-level
- Fast switch. One blocking syscall stalls every thread in the process.
- Kernel-level
- OS schedules each thread. One can block while another runs.
Practise Threads
Reading is free and needs no account. Practice, mocks and progress live in the app.
- A 2-question practice set that ends the chapter
- 3 quick checks with worked explanations
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device