December Code

Process vs thread interview questions, with answers

Processes and threads are where operating systems interviews begin, because everything else, scheduling, synchronisation and memory, is about managing them. Interviewers expect precise answers: what a process owns, what threads share, and how a new process is created, rather than the textbook definition alone.

The answers below cover processes and their memory, processes versus threads, the process control block, thread models, inter-process communication, and fork and exec. Then take the free Operating Systems diagnostic — ten questions across every OS topic in the bank — to see which of these you can explain but not yet apply.

The questions, with answers

  1. 1.What is a process in an operating system?

    In short: A program in execution: its code plus its own address space, CPU state and resources, managed by the OS.

    A program is a file of instructions; a process is that program running, with everything the operating system tracks for it. Each process has its own virtual address space, laid out as the code (text), initialised and uninitialised data, a heap that grows upward for dynamic allocation, and a stack for function calls. It also has CPU state, such as the program counter and registers, and resources such as open files. Because address spaces are separate, one process cannot overwrite another's memory, which makes processes the unit of isolation and protection.

  2. 2.What is the difference between a process and a thread?

    In short: A process is an isolated program instance with its own address space; threads are units of execution inside a process sharing its memory.

    A process has its own address space and resources, so processes are isolated from each other and communicate only through the operating system. A thread is a single sequence of execution within a process, and all threads of a process share its code, heap, global data and open files while each keeps its own stack, registers and program counter. Threads are cheaper to create and switch between than processes, and they communicate through shared memory, which is fast but needs synchronisation. A crash in one thread can bring down the whole process, while a crashed process leaves the others running.

  3. 3.What is a process control block (PCB)?

    In short: The kernel's record of a process: its ID, state, saved CPU registers, memory information, open files and scheduling data.

    The process control block is the data structure the kernel keeps for every process. It holds the process ID and parent ID, the process state, the saved program counter and registers used to resume it, scheduling information such as priority, memory-management information such as the page table pointer, and accounting data and the list of open files. The scheduler moves PCBs between queues, and when a process stops running, its CPU state is saved into its PCB so that it can later continue exactly where it left off. Linux calls its PCB task_struct.

  4. 4.What is the difference between user-level and kernel-level threads?

    In short: User-level threads are managed by a library without the kernel knowing; kernel-level threads are scheduled by the OS itself.

    User-level threads are created and scheduled by a library in user space, so switching between them is very fast, but the kernel sees only one schedulable entity: if one thread makes a blocking system call, the whole process blocks, and the threads cannot run on several cores at once. Kernel-level threads are known to the operating system, which schedules each on any core and blocks only the thread that waits, at the cost of system calls for thread operations. The mapping models are many-to-one, one-to-one, used by Linux and Windows, and many-to-many; Go's goroutines are a modern many-to-many scheme.

  5. 5.How do processes communicate with each other?

    In short: Through inter-process communication (IPC): pipes, message queues, shared memory, sockets and signals, all mediated by the OS.

    Because processes have separate address spaces, they need the operating system to exchange data. Pipes carry a byte stream between related processes, as in a shell pipeline, and named pipes (FIFOs) work between unrelated ones. Message queues pass discrete messages. Shared memory maps the same physical pages into several processes, which is the fastest method but requires synchronisation such as semaphores. Sockets work between processes on the same machine or across a network. Signals deliver simple notifications, such as asking a process to terminate. The two basic models are message passing and shared memory.

  6. 6.What do fork() and exec() do?

    In short: fork() creates a child process as a copy of the caller; exec() replaces a process's program with a new one.

    On Unix-like systems, fork() creates a new process that is a copy of the calling one: same code, a copy of its memory (shared copy-on-write until either writes) and copies of its open file descriptors. Both processes continue from the instruction after the call; fork() returns the child's PID in the parent and 0 in the child, which is how the code tells them apart. exec() loads a new program into the current process, replacing its code and memory while keeping its PID. A shell runs a command by forking and then calling exec in the child, while the parent calls wait() for it to finish.

How the diagnostic asks it

One question from the Operating Systems bank, exactly as a sitting would show it. The bank has 5 on processes & threads and 30 across Operating Systems.

Processes & Threads · mediumOS-002

Which of these is NOT shared between two threads of the same process?

  1. 1Each thread's stackcorrect
  2. 2The heap
  3. 3Global variables
  4. 4Open file descriptors

Threads of one process share its address space: code, global and static data, the heap and resources such as open files, which is why they can communicate through memory and also why they need locks. What each thread must have for itself is its own execution state: a stack holding its local variables and return addresses, a program counter and a register set. The heap, globals and file descriptors are all shared, so the stack is the one not shared, although a thread can still reach another's stack through a pointer.

Measure it

Reading answers tells you what’s true. A diagnostic tells you what you get wrong.

10 Operating Systems questions across its topics, easy to hard, about fifteen minutes. You get a readiness figure with the arithmetic shown, the topics you missed named, and a practice set sized for today. Free: 1 diagnostic a month and 15 problems a day. No card.

What the readiness test measures · how the score is computed

By Harshit · updated