Computer Science and IT‹›⌂ 0:00 studied

Module 9 · 13 min read · Free chapter

System calls, processes and threads

In short

A system call is a trap from user mode into the kernel. A process is a running program with a PCB and five states: new, ready, running, waiting and terminated. fork() returns 0 to the child and the child's ID to the parent, so n forks give 2ⁿ processes. Threads share code and data but keep their own stack.

What are you preparing for? We show the practice and previous year questions of your exam.

Start from zero: the reservation counter

Think of a railway reservation counter. Hundreds of passengers want tickets, but none of them is allowed to walk behind the glass, open the computer and print a ticket for themselves. Each passenger fills a form and pushes it through the window, and the clerk checks it, uses the printer and the database, and hands back the ticket.

A computer works the same way. Your browser, your music player and your compiler are the passengers, and the operating system is the clerk behind the glass. Programs never touch the disk, the network card or another program's memory directly. They ask, and the operating system acts for them.

This one idea explains the whole module. The operating system shares one CPU among many programs, keeps their memory apart, lets them talk safely and stores their files. In this chapter we see how a program asks the kernel for help, what a running program looks like to the kernel, and why one program can have many threads.

Why it matters. If any program could write to the disk directly, one buggy app could wipe out everyone's files. The window between programs and the kernel is what keeps a shared computer safe.

Kernel mode, user mode and system calls

The CPU itself enforces the glass window. It has a mode bit: in kernel mode every instruction is allowed, and in user mode a set of privileged instructions is blocked. Privileged instructions include direct I/O, setting the timer, turning interrupts off and changing memory-protection registers.

If a user program tries a privileged instruction, the hardware does not run it. Instead it raises a trap, the CPU switches to kernel mode, and the kernel decides what to do, usually ending the program. The kernel is the part of the operating system that always runs in kernel mode.

So how does a program legally ask for a service? It makes a system call. The program puts a system call number and its arguments in agreed places and runs a special trap instruction. The CPU switches to kernel mode, jumps to a fixed handler, the handler does the work, and a return instruction switches back to user mode.

char msg[] = "hello\n";
write(1, msg, 6);   /* library wrapper puts the call number in a register,
                       then traps into the kernel */

Most programmers never write the trap instruction. They call a library function such as printf() or write(), and the library makes the system call. Arguments travel in registers, on the stack, or in a block of memory whose address is put in a register.

Group Typical Unix calls
Process control fork(), exec(), exit(), wait()
File management open(), read(), write(), close()
Device management ioctl(), read(), write()
Information maintenance getpid(), alarm(), sleep()
Communication pipe(), shmget(), socket()

A system call is costlier than an ordinary function call, because the CPU must change mode, save registers and check every argument. That is why programs read files in large chunks rather than one byte at a time.

Why it matters. Mode switching is the foundation of protection. Without the mode bit, the timer could be switched off by any program and the operating system could never take the CPU back.

Process, PCB and process states

A program is a passive file of instructions on disk. A process is that program in execution, with its own registers, program counter and memory. Two people running the same editor create two processes from one program.

Each process sees its own address space. At the bottom is the text section holding the code, above it the data section for global variables, then the heap, which grows upward as the program asks for dynamic memory. At the top is the stack, which grows downward as functions are called and holds local variables and return addresses.

The kernel keeps one record per process called the process control block (PCB). It holds the process ID, the current state, the program counter and other registers, the priority and scheduling queues, memory information such as the page-table base, accounting data such as CPU time used, and the list of open files. When a process is stopped, its PCB is all the kernel needs to restart it exactly where it left off.

A process moves through five basic states. It is new while being created, ready when it waits only for the CPU, running when its instructions are executing, waiting (or blocked) when it waits for I/O or an event, and terminated when it has finished.

Transition Cause
new → ready admitted by the OS
ready → running dispatched by the CPU scheduler
running → ready timer interrupt or preemption
running → waiting I/O request or wait for an event
waiting → ready I/O or event completed
running → terminated exit() or a fatal error

On a single CPU at most one process is running at any instant. Many may be ready and many may be waiting, each in its own queue. Systems that swap processes to disk add two more states, ready-suspended and blocked-suspended.

Why it matters. Every scheduling and synchronisation question starts from this diagram. Knowing exactly which arrows exist lets you reject wrong options at once.

Creating processes: fork, exec and wait

In Unix-like systems a new process is made by fork(). The child is an almost exact copy of the parent: same code, a copy of the data, heap and stack, and the same open files. The only visible difference is the return value: fork() returns 0 in the child, the child's process ID in the parent, and −1-1 if no child could be made.

The child usually wants to run a different program, so it calls exec(). Exec replaces the memory image of the calling process with the new program and starts it from its first instruction. On success exec never returns, because the code that called it no longer exists.

pid_t pid = fork();
if (pid == 0) {                 /* child */
    execlp("ls", "ls", NULL);   /* replaced by ls */
} else if (pid > 0) {           /* parent */
    wait(NULL);                 /* sleep until the child ends */
    printf("child done\n");
}

Worked example 1. How many processes exist after this code, and how many times is "hi" printed?

fork(); fork(); fork();
printf("hi\n");

The first fork turns 1 process into 2. Both run the second fork, giving 4, and all four run the third, giving 8. So there are 23=82^3 = 8 processes, 23−1=72^3 - 1 = 7 of them new, and "hi" is printed 8 times.

Worked example 2. In if (fork() == 0) fork(); only the child of the first fork runs the second fork. The parent stays, the first child creates one more child, and the total is 3 processes. The general rule of 2n2^n holds only when every process reaches every fork.

When a child exits, the kernel keeps a small entry so the parent can read its exit status through wait(). Until the parent calls wait(), the dead child is a zombie. If the parent ends first, the child becomes an orphan and is adopted by init, the first process, with process ID 1, which collects it.

Why it matters. Every shell command you type runs through fork and exec. Zombies left by careless servers can fill the process table and stop new processes from starting.

Schedulers and the context switch

Three schedulers decide who gets what. The long-term scheduler (job scheduler) decides which jobs are admitted into memory, and so controls the degree of multiprogramming. The short-term scheduler (CPU scheduler) picks the next ready process many times a second. The medium-term scheduler swaps a process out to disk when memory is tight and brings it back later.

Good long-term scheduling keeps a mix of I/O-bound processes, which spend most time waiting for devices, and CPU-bound processes, which spend most time computing. If all are I/O-bound, the CPU sits idle; if all are CPU-bound, the devices sit idle.

When the CPU scheduler picks a new process, the dispatcher performs a context switch. It saves the registers, program counter and state of the old process into its PCB, loads the saved values of the new process from its PCB, switches the memory map, and jumps to user mode. No user work happens during this time, so the context switch is pure overhead.

Worked example 3. A system gives each process a time slice of 9 ms, and each context switch costs 1 ms. In every 10 ms only 9 ms is useful work, so CPU efficiency is 910=90%\dfrac{9}{10} = 90\% and overhead is 10%. If the slice is cut to 1 ms with the same switch cost, efficiency falls to 12=50%\dfrac{1}{2} = 50\%.

A switch between processes also loses the contents of the cache and the address-translation buffer, which were filled for the old process. That hidden cost often exceeds the direct cost of saving registers.

Why it matters. The ratio of switch time to time slice decides how much of the CPU is wasted. This is exactly the trade-off you meet when choosing a round-robin quantum in scheduling questions.

Threads and multithreading models

A thread is the smallest unit the CPU scheduler can run. A process can have many threads. All threads of a process share its code, global data, heap and open files, but each thread has its own thread ID, program counter, registers and stack.

Think of a word processor. One thread reads your keystrokes, another checks spelling, and a third saves the file in the background. If the program were a single thread, the whole window would freeze while a large file was being saved.

Threads give four benefits. They keep a program responsive, they share memory without any special setup, they are cheaper to create and switch than processes, and they let one program use several cores at once. The cost is that shared data must now be protected from simultaneous updates.

User-level threads are created and switched by a library in user space, so the kernel sees only one process. Switching is very fast, but if one thread makes a blocking system call, the kernel blocks the whole process. Kernel-level threads are known to the kernel, which schedules each one separately, so one blocked thread does not stop the others.

Model Mapping Strength Weakness
Many-to-one many user threads on one kernel thread fast switching in user space one blocking call blocks all; no use of multiple cores
One-to-one each user thread has its own kernel thread true parallelism; blocking affects one thread creating many threads loads the kernel
Many-to-many m user threads on n≤mn \le m kernel threads flexible and parallel complex to implement

Linux and Windows use the one-to-one model. Each thread you create in a program becomes a thread the kernel can schedule on any core.

Why it matters. Modern phones and laptops have many cores, and only kernel-visible threads can use them together. Choosing the threading model decides whether a program really runs in parallel or only appears to.

Descriptive lens: how written papers ask this

Written papers for IT officer posts, state Assistant Programmer exams and university-level tests ask about this topic in four ways:

  1. Define and distinguish pairs such as program and process, process and thread, or user mode and kernel mode.
  2. Draw and explain the process state diagram with every transition and its cause.
  3. Trace a short C program with fork() calls and state how many processes run and what each prints.
  4. Compare threading models and argue which one suits a given server or desktop application.

Practice questions (our own, not previous-year questions).

  1. What is a system call? Explain, with a diagram, how control passes from a user program to the kernel and back, and list the five groups of system calls with one example each. (10 marks)
  2. Draw the five-state process diagram and explain each transition. Why can a process not move directly from waiting to running? (10 marks)
  3. Compare user-level and kernel-level threads and explain the many-to-one, one-to-one and many-to-many models with their advantages and drawbacks. (15 marks)

Answer plan for question 3.

  • Define a thread as a unit of CPU use that shares code, data and files with other threads of its process but has its own stack and registers.
  • Explain that user-level threads are managed by a library, switch without entering the kernel, and are invisible to the kernel scheduler.
  • Explain that kernel-level threads are created and scheduled by the kernel, so each can block or run on a core on its own.
  • Describe the many-to-one model and state its two drawbacks, that one blocking call stops all threads and that it cannot use several cores.
  • Describe the one-to-one model, note that Linux and Windows use it, and state that its cost is a kernel thread for every user thread.
  • Describe the many-to-many model as a flexible middle path and conclude with a short table comparing the three models.

Quick revision

  • The mode bit separates kernel mode, where privileged instructions run, from user mode, where they trap.
  • A system call is a trap into a fixed kernel handler, followed by a return to user mode.
  • The five groups of system calls are process control, file, device, information and communication.
  • A process is a program in execution, with text, data, heap and stack in its address space.
  • The PCB stores the ID, state, program counter, registers, scheduling, memory, accounting and file information.
  • The five states are new, ready, running, waiting and terminated, and there is no arrow from waiting to running.
  • fork() returns 0 to the child and the child's ID to the parent, and n forks in sequence give 2n2^n processes.
  • A zombie has ended but has not been waited for, while an orphan's parent has ended.
  • A context switch saves one PCB and loads another, and it is pure overhead.
  • Threads share code, data, heap and files but keep their own stack, registers and program counter.
  • In the many-to-one model one blocking call stops every thread.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 15 answered

  1. Which of these instructions must be privileged?

  2. A user program executes a privileged instruction. What happens?

  3. A system call is best described as:

  4. getpid() belongs to which group of system calls?

  5. Local variables of a function are stored in the process's:

  6. Which item is NOT normally kept in a process control block?

  7. A process in the waiting state gets its I/O completed. It moves to:

  8. A timer interrupt occurs while process P is running. P moves to:

  9. How many times is 'hi' printed? fork(); fork(); fork(); printf("hi");

  10. How many processes exist in total after: if (fork() == 0) fork();

  11. A child process has exited but its parent has not called wait(). The child is:

  12. Time slice is 9 ms and each context switch takes 1 ms. CPU efficiency is:

  13. The degree of multiprogramming is controlled mainly by the:

  14. Which is NOT shared among threads of the same process?

  15. In the many-to-one model, a thread makes a blocking read() call. Then:

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

What is the difference between a process and a thread?

A process has its own address space, while threads of one process share code, data and files but have their own stack, registers and program counter.

What does fork() return?

It returns 0 in the child, the child's process ID in the parent, and −1 if no child could be created.

How many processes do three fork() calls create?

If every process runs all three calls, there are 2³ = 8 processes in total, of which 7 are new.

What is a zombie process?

It is a child that has finished but whose parent has not yet called wait(), so its entry stays in the process table.

More in this module: Operating systems

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Operating Systems (IIT Madras, 106106144): processes, system calls and threads