Processes and Threads
The Process Model
A process is a program in execution.
Process has three main components:
Address space
The memory that the process can access
Consists of various pieces: the program code, static variables, heap, stack, etc.
Processor state
The CPU registers associated with the running process
Includes general purpose registers, program counter, stack pointer, etc.
OS resources
Various OS state associated with the process
Examples: open files, network sockets, etc.
Conceptually, each process has its own virtual CPU. In reality, the physical CPU switches between
processes, a concept known as multiprogramming.
The Process Model
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Process Creation
Processes can be created in four principal ways:
System Initialization: When the OS boots, several processes are created, including
foreground and background (daemon) processes.
Process Creation System Call: A running process can issue a system call to create a new
process. In UNIX, this is done via the `fork` system call, which creates an exact clone of the
parent process.
User Request: A user can start a new process by typing a command or clicking an icon.
Initiation of a Batch Job: In batch systems, the OS creates a new process to run the next
job from the input queue.
Process Creation
After a process is created, the parent and child have their own distinct and private memory address spaces.
Changes made by one are invisible to the other.
UNIX / Linux Approach
❖ The child's initial address space is a copy of the parent's.
❖ It uses a copy-on-write (COW) strategy:
Initially, parent and child share the same memory pages.
The moment either process tries to *write to* (modify) a memory page, a private copy of that page is created for
it.
❖ Result: No writable memory is ever truly shared.
❖ Other resources, like open files, can be shared.
Windows Approach
❖ The parent and child address spaces are completely separate from the start.
❖ There is no initial sharing or copying of the parent's memory space for the child.
Process Creation
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Process Termination
Processes can terminate for several reasons:
Normal Exit (Voluntary): The process has completed its task and calls `exit` (or
`ExitProcess` in Windows).
Error Exit (Voluntary): The process discovers a fatal error (e.g., a file that doesn't exist) and
terminates.
Fatal Error (Involuntary): An error caused by the process, often a bug (e.g., illegal
instruction, referencing nonexistent memory).
Killed by Another Process (Involuntary): A process executes a system call to kill another
process (e.g., `kill` in UNIX).
Process Hierarchies
❖ In some systems, like UNIX, processes can form hierarchies where a parent process has one
or more child processes.
❖ In UNIX, a process and all its descendants form a process group. Signals from the keyboard
are delivered to all members of the group.
❖ The entire system can be viewed as a single tree of processes with `init` at the root.
❖ Windows does not have the concept of a process hierarchy; all processes are considered
equal.
Process States
A process can be in one of three states:
Running: Actually using the CPU at that moment.
Ready: Runnable, but temporarily stopped to let another process run.
Blocked: Unable to run until some external event happens (e.g., waiting for I/O).
There are four possible transitions between the states:
Process blocks for input: From Running to Blocked.
Scheduler picks another process: From Running to Ready.
Scheduler picks this process: From Ready to Running.
Input becomes available: From Blocked to Ready.
Process State Transitions
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Implementation of Processes
The OS maintains a process table (an array of structures) with one entry for each
process.
Each entry, sometimes called a Process Control Block (PCB), contains all the
information about a process, including:
Registers, program counter, stack pointer
Process state, priority, scheduling parameters
Memory management information
File management information (open files, etc.)
Process Table
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Context Switch
The act of swapping a process state on or off the CPU is a context switch.
Context switches are not cheap:
Generally have a lot of CPU state to save and restore
Also must update various flags in the PCB
Picking the next process to run – scheduling – is also expensive
Threads
Threads are a way to have multiple threads of control within the same address
space. They are useful for several reasons:
❖ Simpler Programming Model: In applications with multiple, concurrent activities,
decomposing them into threads simplifies programming.
❖ Faster to Create/Destroy: Threads are "lightweight" and are much faster to create
and destroy than processes.
❖ Performance Gains: Threads can overlap I/O and computation, which can speed up
applications.
❖ Real Parallelism on Multi-CPU Systems: Threads can run in parallel on different
CPUs.
Example: A Multithreaded Word Processor
Consider a word processor with three threads:
❖ Interactive Thread: Interacts with the user, accepting keyboard and mouse input.
❖ Reformatting Thread: Handles reformatting of the document in the background.
❖ Backup Thread: Periodically saves the document to disk.
All three threads share the same memory, allowing them to work on the same
document simultaneously without interfering with the user's experience.
Threads
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
The Classical Thread Model
A process groups resources together, while a thread is the entity
scheduled for execution on the CPU.
Threads within the same process share:
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
POSIX Threads (Pthreads)
To create portable threaded programs, IEEE defined a standard for threads called
Pthreads (IEEE 1003.1c).
Most UNIX systems support Pthreads, which defines over 60 function calls for
thread management.
Key Pthreads calls include:
`pthread_create`: Creates a new thread.
`pthread_exit`: Terminates the calling thread.
`pthread_join`: Waits for a specific thread to exit.
`pthread_yield`: Releases the CPU to let another thread run.
Implementing Threads in User Space
The threads package is implemented entirely in user space, and the kernel is unaware of them.
Advantages:
❖ Can be implemented on an OS that doesn't support threads.
❖ Thread switching is very fast, as it doesn't require a trap to the kernel.
❖ Each process can have its own custom scheduling algorithm.
Disadvantages:
❖ Blocking system calls will block the entire process.
❖ Page faults block the entire process.
❖ No clock interrupts within a single process, making round-robin scheduling impossible.
Implementing Threads in the Kernel
The kernel knows about and manages the threads.
Advantages:
When a thread blocks, the kernel can run another thread from the same process.
No new non-blocking system calls are needed.
Disadvantages:
The cost of a system call is substantial, making thread creation and destruction slower.
Threads
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
Hybrid Implementations
Combines the advantages of both user-level and kernel-level threads.
The kernel is aware of kernel-level threads, and user-level threads are multiplexed
on top of them.
This model provides flexibility, allowing the programmer to control the number of
kernel threads and user-level threads.
Threads
Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.