Nowadays every person is interacting with the processes almost daily. As a software developer, you build programs that turn in the process. We treat processes as a black box. Albeit gaining insights into the internals aid in optimizations and efficient use of processes.
Processes on windows (Task Manager)
Processes on Linux
The code you write to accomplish any task is known as a program.
Then you compile your code with a compiler that generates an executable (or lib) file. Code in the executable file is still a program but converted to machine-level code. It is the set of instructions that can be executed by the processor.
CPU executes these instructions (program) to perform the intended operation. Once execution of your program starts it becomes a process. Program is a blueprint for the process which represents how a process will be executed.
Let’s take an analogy to understand it better.
You are a songwriter and wrote a song. That song written in our terminology is a program. Let’s say the singer is your CPU which reads line by line and sings the song. The singer while singing is the process.
- Code at rest: Program
- Code in execution: Process
Process memory layout
Text section: The text section contains the program which is a combination of instructions that will be executed by the CPU.
Data section: Global variables are defined in the data section.
Heap section: It contains the section of the memory that will be dynamically allocated during the execution of the program.
Stack section: Function calls and local variables add an entry to the stack section. A single entry in the stack is known as the activation record.
Heap and stack areas grow towards each other. If there is an overlap between stack area and heap area it is known as a segmentation fault.
A program becomes a process when it gets loaded to the main memory.
A process has the following states:
- Suspended wait
- Suspended ready
Process state diagram
Let’s understand the process diagram
Initially, your program (executable file) resides in the secondary storage. This state is known as the New state. Once your run the process then the process is loaded in the primary memory by the long-term scheduler. It means the process is currently in the ready queue (Ready state) and waiting for the CPU for execution. Short term scheduler then selects the process from the ready queue and assigns it to the CPU for execution (Running state). CPU can execute the process in a preemptive or non-preemptive manner. CPU executes instructions one by one and once all instructions are executed then the process moves to the completion state (Terminated state).
The process can be moved back from the Running state to the ready state if the time quantum of the process has expired or a new priority process is in the ready queue.
Execution of the process performs two types of operations
- CPU bound
For IO-bound operations CPU just informs the controller and becomes idle. As we know CPU is an expansive resource and we don’t want it to be idle.
Hence during IO operation, the process is removed from the running state and moved to the waiting queue. This state is known as the Waiting state. Once IO operation is done then the process is moved back to the ready queue. CPU will be assigned to the process in its turn.
Ready queue and waiting queue are in the main memory. It might be possible these queues might be full then we move the processes to the hard disk. Once the process is moved to the hard disk it's known to be in a suspended state.
OS Ready Queue
Process Control Block
The mechanism used by the operating system to represent an operating system is PCB (Process control block). PCB stores all information required by the OS system to manage and execute the process by the CPU.
Following are the properties of the PCB
- Process ID: Unique ID of the process
- Process State: State of the process which we discussed above.
- Program Counter: PC is a register that stores the address of the instruction that will be expected next.
- CPU registers: There is a mismatch in the speed of the CPU and RAM. If we load data directly from the main memory then the CPU will be idle for a significant amount of time. Hence data is loaded into the registers. PCB points to the registers used by the process during its execution.
- CPU scheduling information
- Memory Management Information
- I/O status information
Goal: Maximum utilization of the CPU
Process scheduler selects a process from the available processes for execution. Only a single process can be executed on one core processor at a time.
The number of processes loaded in the memory for execution is known as the degree of multiprogramming.
Various CPU scheduling algorithms are used to schedule processes to the CPU ( will write a separate article on details of CPU scheduling). Following are the general scheduling algorithms
- First come first serve (FCFS)
- Shortest Job First (SJF)
- Shortest Remaining Time First ( SJF + Preemption)
- Longest Job First (LJF)
- Longest Remaining Time First ( LJF + Preemption)
- Round Robin ( FCFS + Preemption based on time quantum)
As we already know that a process is represented as PCB by the operating system. When a process is preempted from the CPU and next process is assigned to the CPU for execution is known as Context Switching.
Context switch first saves the current process PCB and reload the next process PCB before execution. During context switch CPU remains to idle and its an overhead for us. CPU idle time during context switch is known as dispatch delay.