Prerequisites for Understanding Processes
Von Neumann Architecture
Most modern computers, from laptops to servers, follow the Von Neumann architecture. This fundamental hardware structure defines how components interact within a computing system.
Key components include:
- Input devices: keyboards, disks, network cards, graphics cards, microphones, cameras
- Output devices: monitors, disks, network cards, graphics cards, speakers, printers
- Memory: RAM
- Central Processing Unit (CPU): combines arithmetic and logic units with control functions
An important distinction is that disks are considered peripherals, not memory. Peripherals function as both input and output devices.
The architecture is designed around the memory as the central component. All peripherals interact with memory, and the CPU only accesses memory directly. This design addresses the speed mismatch between fast CPUs and slower peripherals.
Data flows as follows: input devices send data to memory, the CPU processes data from memory, and results are sent to output devices.
Key principles:
- Memory refers to RAM, not storage devices
- CPUs can only directly access memory, not peripherals
- Peripherals can only exchange data with memory
- All components interact through memory
Operating System Fundamentals
An operating system (OS) is a fundamental program collection that manages hardware and software resources. The OS consists of:
- Kernel: handles process management, memory management, file management, and driver management
- Other programs: libraries, shells, etc.
Simplistically, the OS is software specifically designed to manage hardware and software resources, providing users with a stable, efficient, and secure computing environment.
The OS manages hardware resources through a "describe first, then organize" approach:
- Hardware characteristics are described using data structures (structs in C)
- These structures are organized using appropriate data structures like linked lists
For user interaction, the OS provides multiple layers:
- System call interfaces (complex C functions)
- User operation interfaces (GUIs, libraries, commands)
- User behaviors
Process Concepts
Process Definition
A process is more than just a program loaded into memory. While this is a common definition, it's incomplete. A process is an executing instance of a program that includes:
- The program code
- Data
- System resources
- Execution context
The operating system manages processes through Process Control Blocks (PCBs). In Linux, this is implemented as the task_struct data structure.
Process Control Block
The PCB contains all information needed to manage a process:
- Process ID (PID): unique identifier
- Parent process ID (PPID)
- Process state
- Priority
- Program counter
- Memory pointers
- Context data
- I/O status information
- Accounting information
Example code to display PID:
#include <stdio.h>
#include <unistd.h>
int main() {
printf("Process ID: %d\n", getpid());
printf("Parent Process ID: %d\n", getppid());
return 0;
}
Process Context
When processes are switched, the CPU saves the current process's context (registers) to its PCB and restores the next process's context. This "context switching" allows multiple processes to appear to run simultaneously on a single CPU.
Process Creation with fork()
The fork() system call creates a new process:
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid < 0) {
printf("Fork failed\n");
return 1;
} else if (pid == 0) {
printf("Child process\n");
} else {
printf("Parent process with child PID: %d\n", pid);
}
return 0;
}
fork() returns:
- Negative value if creation fails
- Child's PID to the parent process
- 0 to the child process
The child process inherits the parent's resources but operates independently through "copy-on-write" memory management.
Process States
Processes can exist in various states:
- Running (R): Currently executing or in the run queue
- Sleeping (S): Waiting for an event (interruptible sleep)
- Disk Sleep (D): Waiting for I/O completion (uninterruptible sleep)
- Stopped (T): Suspended by signal
- Zombie (Z): Terminated but not yet reaped by parent
- Dead (X): Process has been completely terminated
Orphan Processes
When a parent process terminates before its child, the child becomes an orphan and is adopted by the init process (PID 1).
Process Priorities
Process priorities determine execution order:
- PRI: Actual priority (lower values = higher priority)
- NI: Nice value (-20 to 19) that modifies PRI
The relationship is: PRI(new) = PRI(old) + NI
Priority can be adjusted with the nice command or system calls, but extreme values can cause "starvation" where some processes never get CPU time.
Process Characteristics
- Competition: Multiple processes compete for limited CPU resources
- Independence: Processes operate independently with isolated resources
- Parallelism: True simultaneous execution on multiple CPUs
- Concurrency: Apparent simultaneous execution on a single CPU through rapid switching
Environment Variables
Environment variables are parameters that define the operating environment:
- PATH: Command search paths
- HOME: User home directory
- SHELL: Current shell program
Common commands:
echo $VAR: Display varialbe valueexport VAR=value: Set environment variableenv: Display all enivronment variablesunset VAR: Remove variable
Environment variables are inherited by child processes and provide a mechanism for configuration across processes.
Process Address Space
Each process has its own virtual address space, which maps to physical memory through page tables. This provides:
- Memory protection between processes
- Efficient memory management through delayed allocation
- Uniform memory access model for applications
The address space includes regions for:
- Code segment
- Data segment
- Stack
- Heap
Virtual addresses allow processes to operate as if they have exclusive access to memory, while the OS manages the actual physical mapping through page tables.