Basic Concepts
Process
Definition: A process is one execution of a program on a computer; it is the basic unit for resource allocation and scheduling in the system.
Characteristics:
- Dynamism: a process is the execution of a program and therefore has a life cycle
- Concurrency: many processes can exist and run at the same time
- Independence: a process is an independent unit of resource allocation
- Structure: a process consists of a program, its data, and a process control block
Composition of a process:
- Program segment: holds the program code
- Data segment: holds the data the program works on while running
- Process Control Block (PCB): holds the management information for the process
Process Control Block (PCB)
Role of the PCB: the PCB is the core data structure through which the operating system manages a process; it contains all of the management information for that process.
Main contents of the PCB:
-
Process identification information:
- Process ID (PID)
- Parent process ID (PPID)
- User ID (UID)
-
Processor state information:
- Contents of the general-purpose registers
- Program counter (PC)
- Program status word (PSW)
- Stack pointer
-
Process scheduling information:
- Process state
- Process priority
- Scheduling queue pointers
- Other scheduling parameters
-
Process control information:
- Program and data addresses
- Synchronization and communication mechanisms of the process
- Resource inventory
- Link pointers
Thread
Definition: A thread is an execution unit inside a process; it is the basic unit the CPU schedules.
Characteristics:
- Lightweight: creating, terminating, and switching threads is cheap
- Sharing: threads of the same process share the process’s resources
- Concurrency: many threads can execute concurrently
- Independence: every thread has its own program counter and stack
Relationship between threads and processes:
Differences Between Processes and Threads
| Aspect | Process | Thread |
|---|---|---|
| Resource allocation | Own address space | Shares the process’s address space |
| Creation overhead | High (memory must be allocated) | Low (existing resources are reused) |
| Switching overhead | High (the address space must be switched) | Low (only the registers are switched) |
| Communication | Inter-process communication (IPC) | Shared memory, global variables |
| Concurrency | Process-level concurrency | Thread-level concurrency |
| Independence | Fully independent | Relatively independent |
Multithreading Models
1. User-Level Threads (ULT)
Characteristics:
- Thread management is performed by the user program
- The operating system does not know that the threads exist
- Switching threads does not require the kernel
Advantages:
- Low thread-switching overhead
- Flexible scheduling algorithms
- Good portability
Disadvantages:
- One blocked thread stalls the whole process
- Multi-core CPUs cannot be exploited
2. Kernel-Level Threads (KLT)
Characteristics:
- Thread management is performed by the operating system kernel
- The kernel allocates resources for every thread
- Switching threads requires the kernel
Advantages:
- Multi-core CPUs can be fully exploited
- One blocked thread does not affect the other threads
- Fairer scheduling
Disadvantages:
- High thread-switching overhead
- Heavy consumption of system resources
3. Hybrid Model
Characteristics:
- Combines the advantages of user-level and kernel-level threads
- User-level threads are mapped onto kernel-level threads
- Offers better performance and flexibility
Implementation options:
- Many-to-many model: several user threads are mapped onto several kernel threads
- One-to-one model: each user thread corresponds to one kernel thread
- Many-to-one model: several user threads are mapped onto a single kernel thread
Process States
Basic States
-
Ready state:
- The process has obtained every resource except the CPU
- It is waiting for the CPU to schedule it
- It is queued in the ready queue
-
Running state:
- The process is executing on the CPU
- It occupies the CPU
- Only one process can be in the running state at any moment
-
Blocked state:
- The process is waiting for an event or a resource
- It cannot be scheduled by the CPU
- It waits in the blocked queue
State Transitions
Transition conditions:
- Ready → Running: the CPU scheduler selects the process for execution
- Running → Ready: the time slice expires, or a higher-priority process preempts it
- Running → Blocked: the process requests I/O or waits for a resource
- Blocked → Ready: the awaited event occurs, or the resource becomes available
Process Control
Process Creation
Ways a process is created:
- System initialization: the system creates system processes at boot time
- User request: a user creates a process through a command or a program
- Process derivation: an existing process creates a child process
Creation procedure:
- Allocate a PCB
- Allocate the memory space
- Initialize the PCB
- Place the process in the ready queue
Process Termination
Ways a process terminates:
- Normal termination: the process finishes its execution
- Abnormal termination: the process fails while running
- External intervention: it is killed by another process or by a user
Termination procedure:
- Release the resources held by the process
- Release the PCB
- Notify the parent process
Process Switching
When a switch happens:
- The time slice expires
- A higher-priority process becomes ready
- The running process blocks
- The running process terminates
Switching procedure:
- Save the context of the current process
- Update the PCB information
- Select the next process to run
- Restore the context of the new process
- Jump to the new process
| Term | Abbreviation | Explanation |
|---|---|---|
| Process | - | One execution of a program |
| Thread | - | An execution unit inside a process |
| Process Control Block | PCB | The data structure that stores information about a process |
| Process State | - | The current state of a process |
| Ready State | - | The state of waiting to be scheduled onto the CPU |
| Running State | - | The state of executing on the CPU |
| Blocked State | - | The state of waiting for an event |
| New State | - | The state of being created |
| Terminated State | - | The state of having finished execution |
| Process Creation | - | Creating a new process |
| Process Termination | - | Terminating a process |
| Process Blocking | - | A process entering the blocked state |
| Process Wakeup | - | A process moving from the blocked state to the ready state |
| Context Switch | - | Switching the execution environment from one process to another |
| Process Context | - | The environment information of a process’s execution |
| User-Level Thread | ULT | A thread managed in user space |
| Kernel-Level Thread | KLT | A thread managed by the kernel |
| Lightweight Process | LWP | A user thread supported by the kernel |
| Process Scheduling | - | Choosing which process executes |
| Job Scheduling | - | Choosing which jobs enter memory |
| Medium-Term Scheduling | - | Swapping processes into and out of memory |
| Primitive | - | An operation that cannot be interrupted |
