Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 55 ยท Operating Systems

Processes, Threads and Scheduling

Learn what a process is, how threads differ, how the operating system switches between tasks with round-robin scheduling, and what a deadlock looks like.

A program is a file on disk: instructions waiting to be used, like a recipe in a book. A process is that recipe being cooked right now: the program loaded into memory, with its own data, its own place in the code and its own resources. You can start the same program twice and get two separate processes, each unaware of the other.

Your computer runs dozens or hundreds of processes at once, yet it might have only four or eight CPU cores. The operating system makes this work by sharing the cores very quickly among all the processes. This lesson explains how a process lives and dies, what a thread is, how the scheduler decides who runs next, and how processes can get stuck waiting for each other. It continues from What an Operating System Does.

Program versus process

When you double-click an application, the operating system (OS) creates a process. It gives the process:

  • a private address space: the memory it may use, which other processes cannot read by accident (see Memory Management);
  • a process ID (PID), a number that identifies it;
  • the CPU state it needs to continue later: the next instruction to run and the register values from The CPU;
  • a list of open resources, such as files and network connections.

The OS keeps all of this in a record often called a process control block. Because every process has its own private memory, a crash in one program does not corrupt another. That isolation is one of the most valuable things an OS provides.

Processes do not only come from double-clicks. Almost every process is started by another one, its parent, so the processes on a machine form a tree. A shell starts the programs you type, your browser starts helper processes for tabs, and the OS starts background services at boot.

The life of a process

A process is not simply "running". At any moment it is in one of a few states:

stateDiagram-v2
  [*] --> New
  New --> Ready: admitted
  Ready --> Running: scheduler picks it
  Running --> Ready: time slice used up
  Running --> Waiting: needs input or disk
  Waiting --> Ready: the wait is over
  Running --> Terminated: finished
  Terminated --> [*]

Process states. Only one process per core can be Running; the others are Ready or Waiting.

  • Ready means the process could use a core right now but no core is free.
  • Running means it is executing on a core.
  • Waiting (or blocked) means it cannot continue until something happens, such as a key press, a network reply or a disk read. A waiting process uses no CPU time, which is why an idle program costs almost nothing.

The important transitions are the arrows between Ready, Running and Waiting. The following stand-in encodes the allowed moves and rejects an illegal one, so you can see that, for instance, a waiting process cannot jump straight to running.

JS A process state machine

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Threads: tasks inside a process

A process can contain several threads. A thread is one path of execution: its own next instruction and its own small stack for function calls. All threads of a process share the same address space and open files.

Think of a process as a kitchen and threads as cooks. The cooks share the pantry and the stove, so they can cooperate cheaply, but they can also get in each other's way. A browser tab might use one thread to run JavaScript and another to decode a video, both working on the same page data.

Process Thread
Memory Own private address space Shares the process's memory
Creating one Relatively expensive Cheaper
Isolation Strong: a crash stays inside Weak: a bug can corrupt shared data
Communication Needs special channels Reads and writes shared variables

More threads help in two ways: they let a program use several cores at the same time, and they let it keep working while one thread waits (the user interface stays responsive while another thread downloads a file). They also cause a classic class of bugs: when two threads change the same data without coordination, the result depends on timing. Languages offer locks to prevent this, and locks lead us to deadlock, below.

Scheduling and context switches

The scheduler is the part of the OS that chooses which Ready process or thread runs on which core. The simplest idea is first come, first served, but one long job would make everyone else wait. Interactive systems use preemption: the OS lets a process run for a short time slice (also called a quantum, often a few milliseconds) and then forcibly takes the core away.

Round-robin scheduling does this fairly. Ready processes sit in a queue; the first one runs for at most one quantum; if it is not finished it goes to the back of the queue; the next one runs.

Taking a core from one process and giving it to another is a context switch. The OS saves the first process's CPU state, loads the second one's, and resumes it exactly where it stopped. This takes real time and also disturbs the CPU caches, so the quantum cannot be too small. Too long a quantum makes the machine feel sluggish; too short wastes time switching. Because switches happen thousands of times per second, you perceive the processes as running simultaneously even on one core.

Schedulers are judged by several measures. Turnaround time is the time from arrival to completion. Waiting time is the time spent Ready but not Running. Response time is the time until a process first gets the CPU, which is what makes an interface feel quick. The simulator below compares first-come-first-served (FCFS) with round-robin for three jobs that arrive together.

JS Scheduler simulator: FCFS versus round-robin

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice the trade-off in the output. Round-robin gives a better average response time (everyone gets the CPU early) but a worse average waiting time here, because jobs are interleaved instead of finishing one by one. Real schedulers also use priorities, favour processes that wait a lot for input, and spread work across cores. This is a simplified model; it ignores the cost of context switches.

Deadlock

Sometimes processes wait for something that will never arrive. A deadlock happens when each process in a group holds a resource and waits for one held by another, so none can proceed. The classic picture: process P1 holds a lock on the printer and wants the scanner, while P2 holds the scanner and wants the printer. Both wait forever.

flowchart LR
  P1(("P1")) -- "waits for" --> S["Scanner"]
  S -- "held by" --> P2(("P2"))
  P2 -- "waits for" --> PR["Printer"]
  PR -- "held by" --> P1

A cycle in the wait chain means deadlock.

Deadlock needs all of these at once: resources that cannot be shared, processes that hold one resource while waiting for another, no way to forcibly take a resource back, and a circular chain of waiting. Remove any one condition and deadlock cannot occur. A common fix is to make everyone request resources in the same fixed order. The stand-in below builds the wait-for relationships and looks for a cycle.

JS Finding a deadlock cycle

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In the first case P3 is also stuck, waiting on P1, even though it is not part of the cycle. One deadlock can freeze more of the system than just the processes involved. Most desktop systems rarely deadlock visibly. Instead, a hung application is usually one stuck thread, and the OS lets you end that process with the task manager without affecting the others.

What is real here and what is a stand-in? Process states, preemption, round-robin and deadlock are real OS concepts. The code above only models them with plain JavaScript objects; a real kernel schedules actual CPU cores using timer interrupts, and its schedulers (such as Linux's) are much more elaborate than round-robin.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the difference between a program and a process?

    A program is passive instructions on disk. Starting it creates a process with an address space, a PID and CPU state.
  2. 2. Which state does a process enter when it waits for a key press or a disk read?

    A blocked process uses no CPU until the event it needs happens, then it moves back to Ready.
  3. 3. What do threads of the same process share?

    Threads have their own stack and registers but share memory and resources, which makes cooperation cheap and bugs easier.
  4. 4. What is a context switch?

    The OS stores the registers and next instruction of the outgoing process and restores those of the incoming one.
  5. 5. Which situation describes a deadlock?

    A deadlock is a circular wait: nobody can proceed because each is waiting on another member of the cycle.

Summary

  • A program is a file; a process is a running program with its own address space, PID and CPU state.
  • Processes move between New, Ready, Running, Waiting and Terminated; only Running ones use a core.
  • Threads are paths of execution inside a process that share its memory, which is cheap but needs care.
  • The scheduler gives each Ready task a short time slice; round-robin rotates fairly, and a context switch swaps CPU state.
  • Good scheduling balances waiting time, response time and the overhead of switching.
  • A deadlock is a circular wait; ordering resource requests consistently prevents it.

Next, see how each process gets its own view of memory: Memory Management.