UNIHIVE

Study Hub UNIPORT CSC 394

CSC 394 · Operating Systems

Processes and threads

What a process is, process states and the PCB, context switching, creating processes, threads and their benefits, and inter-process communication.

Our notes

2 min read

Process

A process is a program in execution. A program is passive (a file on disk); a process is active, with:

  • code (text), data, heap and stack;
  • a program counter and register values;
  • open files and other resources.

Process states

StateMeaning
Newbeing created
Readywaiting for the CPU
Runninginstructions being executed
Waiting (blocked)waiting for an event (I/O, a signal)
Terminatedfinished

Transitions: New → Ready (admitted); Ready → Running (scheduler dispatch); Running → Ready (interrupt, time slice used up); Running → Waiting (I/O request); Waiting → Ready (I/O done); Running → Terminated (exit).
Only one process per CPU core can be running at a time.

Process control block (PCB)

The OS keeps a PCB for each process: process ID, state, program counter, CPU registers, scheduling information (priority), memory-management information, I/O status (open files), accounting information.

Context switch

Switching the CPU from one process to another: save the state of the old process in its PCB, load the state of the new one. It is pure overhead (no useful work), so it must be fast.

Process creation and ending

A parent creates child processes, forming a tree. In UNIX: fork() makes a copy of the parent (returns 0 in the child, the child's PID in the parent); exec() replaces the child's program with a new one; wait() lets the parent wait for the child to finish. A child that has ended but not been "waited for" is a zombie; a child whose parent has died is an orphan (adopted by init/systemd).

Worked example. How many processes after this C code runs? fork(); fork(); fork(); Each fork doubles the count: 2³ = 8 processes (the original plus 7 new).

Threads

A thread is a unit of execution inside a process; threads of one process share code, data and open files but each has its own program counter, registers and stack.
Benefits: responsiveness (one thread waits while another works), resource sharing, cheaper to create and switch than processes, use of multiple cores.

  • User-level threads: managed by a library; fast, but one blocking call can block all.
  • Kernel-level threads: managed by the OS; can run on separate cores.
  • Models: many-to-one, one-to-one (Linux, Windows), many-to-many.

Inter-process communication (IPC)

Processes that cooperate need to exchange data:

  • Shared memory: a region both can read and write (fast, but needs synchronisation).
  • Message passing: send() and receive() through the kernel (pipes, message queues, sockets); simpler for distributed systems.
  • UNIX pipes: ls | grep txt sends the output of ls into grep.
  • Signals: short notifications (SIGKILL, SIGINT from Ctrl+C).

Exams usually ask: program vs process; draw and explain the five-state diagram; contents of a PCB; context switching; fork() counting; process vs thread; shared memory vs message passing.

Common mistake: drawing a transition from Waiting straight to Running. A process that finishes waiting goes to Ready and must be scheduled again.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All CSC 394 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.