There are many definitions of the OS. But if we simplify it then it is complex software.
OS provides the interface between the user and the hardware.
OS is the resource manager or allocator that manages resources available in our system like CPU, RAM, Memory, HDD, Interrupts, and devices.
What is the Kernel?
Kernel is the core of the OS. Where all the services like CPU management, Memory Management, File Management and Network Management present.
Linux(OS) Architecture Block Diagram
User Space is where all the applications run like web browsing apps (Google Chrome), and media players(VLC).
Kernel Space is where all the kernel services like Device Manager, CPU Manager, Network Manager, Memory Manager, and File Manager run.
Hardware layer contains all the physical hardware like CPU, RAM, and ROM.
User Space vs Kernel Space with real time scenario
Let’s say using the internet through Google Chrome.
Google Chrome runs in user space.
A network model or Network card is hardware that is present on the mobile or computer to send and receive data from the internet.
Google Chrome communicates to the kernel service network manager and the network manager asks Hardware network card to send and receive the data on the internet.
System call and System Programming
User Space(Unprivileged mode) and Kernel Space(Privileged mode) are different memory areas.
User space application(Chrome) in the above example is calling kernel space service(network manager).
This communication from user space to kernel space happens through kernel API(Application programming interface) or system call.
A system call is the kernel API which is used to communicate kernel space service.
Doing the programming using this system call is called System Programming.
Example of system calls: Process manager uses system calls like fork, exec.
What is Process in OS?
A program or executable file under execution is called as the Process.
The process runs in the RAM or Primary memory.
What is PCB?
PCB is the process control block.
There may be multiple processes running. PCB contains information about each process.
PCB contains information like state of the process like stack pointer information, program counter information, List of open files, signal handling information, CPU register details.
When any new process is created PCB will be created and destroyed when process completes.
PCB created by the kernel and in the kernel space and it’s task of kernel process manager.
What is PID?
Kernel process manager assigns an ID or number to each process stored in the PCB called PID(Process Identification).
getpid() is the function used to get PID for the current running process.
//getpid.c
#include<stdio.h>
#include <unistd.h>
int main()
{
printf("PID is %dn", getpid());
while(1);
}
embedded@kernel ~ % ./getpid
o/p : PID is 74060
Same PID can be seen using ps command for above program/executable.
embedded@kernel ~ % ps -ax | grep 74060
74060 ttys000 3:53.61 ./getpid
What is the ps command in the linux?
ps – process status
ps command shows the process state, PID, and parent PID of the process.
ps command also shows memory and CPU usage by the process.
more information about ps command and its usage can be found using the manual page. run “man ps” in linux terminal to get a manual page or click here.
ps command also uses the PCB to show all the process-related information.
What are the states of the process?
The below diagram explains the state transition of the process.
There are multiple states of the process.
When new process is created it will be pushed to ready queue and state is ready state.
CPU scheduler then pushes process from ready queue to run state and process gets CPU time and it executes the process.
If process gets any wait, sleep, or suspend even then it comes out of running state and goes to the wait, sleep, or suspend state respectively
Upon completion of sleep, wait and suspend even again process goes to the ready queue and process state becomes ready state(means ready to run).
Again scheduler pushes process to running state one by one.
Once the process is finished it goes to exit state.
Above State information of process is stored in the PCB.
The scheduler schedules the processes from the ready state to the run state based on the scheduling algorithm.
What is the scheduler?
Scheduler is the part of the kernel process manager runs in the kernel space.
Scheduler decides which process/job/task runs out of many jobs/processes/tasks waiting in the ready queue.
There are many different type of scheduler and scheduling algorithms. ex: fifo, CFS(complete fair scheduling), Round robin, preemptive and non-preemptive scheduling.
What is the time slice mechanism in the OS?
The scheduler schedules one process to run on the CPU for a certain time period. If that process finishes in that time period then it goes to exit state and another process will be scheduled to run. If process doesn’t get finished in that time frame then the scheduler pushes that process out of the running state to the ready state. and another process will get time on the CPU.
Let’s say four processes(P1, P2, P3, P4) waiting in the ready queue to run.
Scheduler first runs P1 for 1ms and other processes are in ready or any other state.
Then P2 gets CPU and other waits for the CPU.
Then P3 and then P4 get time on CPU.
In this way, only one process runs on the CPU and others are waiting but user feels that all the processes are running at the same time. because for a small time period process runs immediately and it comes out and other will run. So, all the processes gets time on CPU one by one very frequently and this is how User feels that all the processes are running at the same time.
It’s similar mechanism like video. Video is just watching images very fast. Images movement happens at a higher rate so that human eyes feel like video.
Same way CPU manages all the processes in such a way that user feels like all the processes running at a same time.
The time period for which process gets time on the CPU to run and then moved to out of the running period is called as the time slice period.
In above case 1ms is the time slice period.
time slice can be configured on the OS using certain environmental variables.
How multiple processes run on one core of the CPU/Processor?
Many applications or processes run on our computers simultaneously.
At a time only one job or process runs in one core of the CPU. So, if CPU has only one core then only one job runs at a time. If the CPU has two cores, then only two processes run at a time.
Intel i7 has four cores in the processor. But, more than four processes can run on intel processor/CPU.
This multi-processing of the job/task/process is possible due to scheduling done by the scheduler and time slice mechanics.
The scheduler schedules one process to run on CPU for certain time period. If that process finishes in that time period, then it goes to exit state and another process will be scheduled to run. If the process doesn’t get finished in that time frame, then the scheduler pushes that process out of the running state to ready state. And another process will get time on the CPU.