[OS] Process Abstraction
What is a Process?
A process is a running program in a computer.
Process - Address Space
Each process has its own address space. The address space consists of 4 main parts.
- Stack: Stores local variables, arguments to be passed to a function, return address.
- Heap: Stores dynamically allocated memory.
- Data: Stores static and global variables.
- Code: Stoes the code of the program.
Process - Process States
There are 5 essential process states in a process execution life cycle.
- Initial: process is created and memory is allocated.
- Ready: process is in the ready queue waiting to be executed.
- Running: process is being executed by cpu.
- Blocked: process is waiting for an I/O operation to finish.
- Terminated (Zombie): process has finsihed execution, but waiting for parent process to read the returned code.
Process Control Block (PCB)
In order for the OS to control each process, it needs a process control block to maintain infomration about a process.
Below are some important parts that the PCB includes:
- Process Id (PID): Unique Id is assigned to each process.
- Program Counter: Pointer pointing to the next instruction (address) that needs to be executed.
- Process State: The current state of the process.
- Register Context: Stores the content of the latest executed code before being moved out from running state.
- Scheduling Information: Process priority, pointers to scheduling queues.
Process Table
The OS needs a process table to manage all the PCB. The process table is a hash table where the key is the PID and the value is a pointer pointing to the PCB for that key. When a process finish execution, the PCB for that process will be removed from the process table.
Process List Structures
The OS also has a ready queue and block queue that stores the processes that are waiting to be executed.
Process Creation
In general, a parent process spawns a new process. This new process is called a child process and it can also create a new process, thus forming a tree.
In modern operating systems, when a parent process is terminated, the child process can proceed independently of the terminated parent process.
Here are the specific steps:
- Assign a unique PID and allocate memory.
- Put the process in the ready queue.
- Initialize the process control block.
- Put the process in the ready queue.
Process Termination
A process can be terminated involuntarily:
- Parent process terminates child process.
- A number of error and faults leading to termination of process.
After termination, the child process returns the termination status to its parent process. The terminated process’ resources are de-allocated afterwards.
Zombie Process
A process that has terminated but the OS keeps the PCB for that process, so that the parent process can read information later. We call this a zombie process.
Suspending a Process
Unlike blocking where the process is blocked by internal activity of a process, suspending comes from external activity.
Suspending occurs to a process when:
- User request.
- Request by parent process.
- OS suspends a blocked process to free up memory for another ready process.
Signals
Signals also known as software interrupt is used to notify a process that some event has occured.
- Synchronous Signal: Triggered by current running process.
- Illegal memory access, division by zero
- Asynchronous Signal: Triggered by external events.
- Timer interrupt, parent process killing the child process
A process can decide whether to catch, ignore, and mask a signal.
- Catch: Use OS’s default action to handle the signal.
- Ignore: Inform OS that it does not want to handle the signal.
- Masking: Instruct the OS not to deliver signals of that type until the process clears the signal mask.
To respond to a particular signal, a process’ PCB contains a pointer to a vector of signal handlers. Each entry corresponds to the handler function for that signal.