Skip to content
GATEiit

GATE CSE · Operating System

System calls, processes and threads

User and kernel mode, system calls and privileged instructions, the process model (PCB, states, context switch), fork, exec and wait with process-count questions, and threads: what they share and how user and kernel threads differ.

Before you start

  • C programming →

    fork questions are C snippets; you must trace return values, loops and short-circuit && and ||.

  • Interrupts →

    System calls and context switches work through the same trap and interrupt mechanism.

This topic comes before: CPU and I/O scheduling, I/O interface: interrupt and DMA, Inter-process communication, concurrency and synchronization, Memory management and virtual memory

Concept notes

1. Dual mode and system calls

The CPU runs in user mode or kernel mode, set by a mode bit. Only kernel mode may run privileged instructions, so a user program cannot take over the machine.

Privileged Not privileged
I/O instructions, setting the timer, disabling interrupts, changing the mode bit, loading page-table or base/limit registers reading the clock, arithmetic, normal loads and stores, the trap instruction itself

A system call is how a user program asks the kernel for a service. The program executes a trap (software interrupt); the hardware switches to kernel mode and jumps to a fixed handler, which uses the system-call number to run the right routine, then returns to user mode.

Types of system calls:

  • Process control: fork, exec, exit, wait
  • File management: open, read, write, close
  • Device management, information (getpid, time), communication (pipe, shmget)

A library call like printf is not itself a system call; it may call write inside.

A trap is caused by the running program (system call, divide by zero, page fault). An interrupt comes from outside the program (timer, I/O device). Both enter kernel mode.

2. Processes, the PCB and process states

A process is a program in execution. It has its own address space: code (text), data, heap and stack.

The kernel keeps a process control block (PCB) per process: process id, state, program counter, CPU registers, scheduling information, memory-management information (page table), open files and accounting data.

The five-state model:

  • New → Ready when admitted.
  • Ready → Running when the scheduler dispatches it.
  • Running → Ready on a timer interrupt or preemption.
  • Running → Waiting (blocked) on I/O or an event.
  • Waiting → Ready when the I/O or event completes. It never goes straight to Running.
  • Running → Terminated on exit.

A context switch saves the running process's state in its PCB and loads another's. It is pure overhead.

Schedulers: the long-term scheduler decides which jobs enter memory (degree of multiprogramming); the short-term (CPU) scheduler picks the next ready process and runs very often; the medium-term scheduler swaps processes out and in (suspended states).

NewReadyRunningTerminatedWaitingadmitteddispatchinterruptI/O or event waitI/O doneexit
Five-state process model

3. fork, exec, wait: counting processes

fork() creates a child that is a copy of the parent. Both continue from the statement after the call. The return value tells them apart:

  • 0 in the child,
  • the child's pid (positive) in the parent,
  • −1 in the parent if creation fails.

The child gets a copy of the address space (in practice via copy-on-write), so a variable changed in one process is unchanged in the other.

exec() replaces the calling process's program with a new one; on success it does not return. wait() makes a parent block until a child ends. exit() ends the process.

  • Zombie: a child that has exited but whose parent has not yet called wait; its PCB entry remains.
  • Orphan: a child whose parent exited first; it is adopted by init (pid 1).

Counting rules:

  • n unconditional fork() calls in sequence (or in a loop run n times by every process) give 2n processes, of which 2n−1 are children.
  • With && and ||, short-circuiting matters. In fork() && fork(), the child of the first fork gets 0 and skips the second; in fork() || fork(), the parent skips it.
  • Trace with a tree: each fork splits one process into a parent branch (return > 0) and a child branch (return 0).

4. Threads

A thread is a unit of CPU execution inside a process. Threads of one process share most resources, so creating and switching threads is cheaper than for processes.

Shared by all threads of a process Private to each thread
code, global data, heap, open files, signal handlers thread id, program counter, registers, stack

User-level threads (ULT) are managed by a library; the kernel sees only the process. Kernel-level threads (KLT) are managed and scheduled by the kernel.

User-level Kernel-level
Switch cost low, no kernel entry higher, kernel mode switch
One thread blocks in a system call whole process blocks (many-to-one) only that thread blocks
Run in parallel on multiple CPUs no (many-to-one) yes
Scheduling by library, can be custom by kernel

Mapping models: many-to-one (many ULTs on one KLT), one-to-one (each ULT on its own KLT; Linux and Windows), many-to-many (multiplexed).

Since threads share data, they need synchronization for shared variables. Each thread's local variables live on its own stack and are not shared.

Formulas to remember

Processes after n forks in sequenceOf which 2n−1 are newly created children
2n
fork return value

Common mistakes

  • Counting processes instead of children (or the reverse): n forks give 2n processes but 2n−1 children.
  • Ignoring short-circuit evaluation in fork() && fork() and fork() || fork().
  • Thinking a variable changed in the child is seen by the parent; after fork they have separate copies.
  • Saying threads share their stacks; each thread has its own stack and registers.
  • Moving a process from Waiting directly to Running.
  • Calling every library function a system call; printf is a library routine that may call write.

Worked examples

Try each step yourself, then reveal the working.

Example 1: fork in a loop

numerical

How many times is "hi" printed, and how many child processes are created?

for (i = 0; i < 3; i++)
  fork();
printf("hi\n");
  1. Step 1: Track the number of processes after each iteration.

    Try it on paper first.

Example 2: fork with short-circuit &&

numerical

How many processes exist in total (including the original) after this runs?

if (fork() && fork())
  fork();
  1. Step 1: Evaluate the first fork in both processes.

    Try it on paper first.

Example 3: A variable after fork

numerical

What values are printed?

int x = 5;
if (fork() == 0) x = x + 5;
else x = x - 5;
printf("%d\n", x);
  1. Step 1: Decide which branch each process takes.

    Try it on paper first.

Example 4: One thread blocks: what happens to the others?

conceptual

A process has four user-level threads under the many-to-one model. One thread makes a blocking read system call. What happens to the other three? What if the threads were kernel-level?

  1. Step 1: What does the kernel see in the many-to-one model?

    Try it on paper first.

Previous year questions

15 official GATE CSE questions on this topic, newest first.

Practise these 15 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.