A program is a set of instructions stored on disk. When it is loaded and starts to run, it becomes a process, which the operating system must manage.

Process Definition

A process is a program in execution. A program is a passive entity, a file on disk, while a process is an active entity with a program counter, registers and resources. The same program can run as several processes at once, such as two copies of a browser. A process in memory has a text section (the code), a data section (global variables), a heap (memory allocated at run time) and a stack (function parameters, local variables and return addresses).

Definition: Process. A program in execution, consisting of its code, data, stack, heap and current activity such as the program counter and register values.

Process States

As it runs, a process changes state. New: the process is being created. Ready: it is in memory and waiting to be given the CPU. Running: its instructions are being executed. Waiting (blocked): it is waiting for some event, such as the completion of I/O. Terminated: it has finished. On a single CPU, only one process can be running at a time, while many may be ready or waiting.

State Transitions

New → Ready: the process is admitted by the OS. Ready → Running: the scheduler dispatches it to the CPU. Running → Waiting: it requests I/O or waits for an event. Waiting → Ready: the event occurs, such as I/O completion. Running → Ready: it is preempted, for example when its time slice ends or a higher-priority process arrives (an interrupt). Running → Terminated: it finishes or is killed. Some systems add suspended states, when a process is swapped out of memory to disk: suspended ready and suspended waiting.

Process Creation and Termination

A process can create child processes, forming a process tree. In UNIX, fork() creates a child, and exec() loads a new program into it. A process ends by calling exit(), and its parent can collect its status with wait(). A child whose parent has not yet waited for it is a zombie, and one whose parent has ended is an orphan.

Table 2.1: Process state transitions

Transition Cause Example
New → Ready Admitted Program loaded
Ready → Running Dispatched Scheduler picks it
Running → Waiting I/O or event wait Reads from disk
Waiting → Ready Event done Disk read complete
Running → Ready Preempted Time slice ends
Running → Terminated Exit Program ends

Example 2.1: Tracing states

Problem: A process P is created, waits for the CPU, runs, reads a file, gets the data, waits for the CPU again, runs and ends. List its states in order.

Solution:

  1. Created: New, then admitted to Ready.
  2. Given the CPU: Running.
  3. Reads a file: Waiting.
  4. Data arrives: Ready.
  5. Given the CPU again: Running, then Terminated.

Result: New → Ready → Running → Waiting → Ready → Running → Terminated.

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void) {
    pid_t c = fork();
    if (c == 0) {
        printf("child: running, then terminating\n");
        return 7;
    }
    int st;
    wait(&st);
    printf("parent: child ended with status %d\n", WEXITSTATUS(st));
    return 0;
}

Output

child: running, then terminating
parent: child ended with status 7

Common mistakes

  • Treating a program and a process as the same thing.
  • Allowing a direct Waiting → Running transition. A waiting process must first become ready.
  • Thinking that many processes can be running at once on a single CPU.

Key takeaways

  • A process is a program in execution.
  • The five states are new, ready, running, waiting and terminated.
  • A waiting process goes to ready, not straight to running.
  • fork() creates a child, exec() loads a program, exit() ends it.

Practice questions

  1. Define a process.
  2. When does a process move from running to waiting?
  3. What is a zombie process?

Answers to the practice questions

  1. A program in execution, with its code, data, stack, heap and current activity.
  2. When it requests I/O or waits for an event.
  3. A child that has terminated but whose parent has not yet collected its status.

Sources

  • Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit II