Processes in Linux

DevOps Engineer
Prerequisites - Basics of Linux & Linux Administrator
ps(process) :
- Programs Running on the machine.
- Managed by Kernel.
- Each process has its unique ID (PID) associated with it.
- Kernel use PID to identify processes
- Each process is associated with a terminal. (talk later in the blog)
ps command

PIDis process ID.TTYis the terminal associated with it.TIMEis the CPU usage time by process.CMDis the name of command.
to get more details about processes, type the command
ps aux
here flags are,
adisplay all processes.udetails about processes.xlist all processes whether a terminal is associated with it or not.

now understand the output of ps aux
USERis the user name.PIDis the process ID.%CPUis the CPU time usage.%MEMis the percentage of the memory allocated to the process.VSZMaximum memory allocated to the process when it was initialized which it can use.RSSactual memory process is used.TTYterminal is associated with the process.STATis the status of the Process.STARTstarting time of the process.TIMEtime taken to execute.COMMANDcommand name.topcommand to see real-time data of processes.
Controlling Terminals
Terminals which are allocated to processes when they are initialized,
- A controlling Terminal binds processes when you kill a terminal, the process which is associated with it also gets killed.
Types
TTYRegular Terminal (no GUI, just a shell, only takes input from keyboard)etsPseudo Terminal (Regular Terminal)?no terminal
Processes are instances of programs.
Basically, the process is running a program in a system and the system is allocating various things like CPU, Memory, etc. to it.
Who manages processes?
The kernel manages the processes, allocates CPU, and memory to the processes, and when a process ends it freezes up all resources and freed them or allocates them somewhere.
How is a process created?
Let's say there is a process named ps1 so, it calls Fork System Call to create a new process, here ps1 is the parent process and the new process is its child process.

Fork System Call creates almost an identical process which is its child process.
type a command ps -l
you'll see PID and PPID,
PIDis Process ID.PPIDis Parent Process ID.
Mother Process
- Parent process of all processes.
- When our System starts, the Mother process executes first.
so which is the mother process of our system's processes?
initis the mother process

- Daemon Processes are also forks of the mother process.
Terminating a process.
- Use an Exit system call to terminate a process.
- When a Child process dies, its Parent Process has to acknowledge it by using Wait System Call , so that Kernel acknowledges it and freed up the resources it was using.
Case 1 Orphan Process
When a Parent Process dies before Child Process , then Kernel turns Child Process into Orphan Process .
- Whenever this Child Process dies, Kernel knows that it does not have any Parent to call Wait System Call to acknowledge.
- Then Terminal puts Child under the protection of mother process init
- Then init will cal Wait System Call on the death of Child Process.
Case 2 Zombie Process
When a Child Process dies but Parent Process does not call Wait System Call , then Kernel turns Child Process into Zombie Process .
- Let's say after becoming Zombie Process, the Parent Process called Wait System Call is known as Reaping .
- If not, then INIT will abopt Zombie Process to call Wait System Call .
Signals
- Event Generated by system to notify when something happened to a process.
SIGHUP / HUP / 1Hangup, when associated terminal closed before completing of process.SIGINT / INT / 2Interruption Signal.SIGKILL / KILL / 9Killing a process by callingExit System CallSIGESEGV / SEGV 11Segmentation fault SignalSIGTERM / TERM / 15Termination Signal.SIGSTOP / STOP / 19Stopping Signal
$ kill -9 35846 killing a process identifying it by PID without Signal, it kills the process immediately but resources might not be freed up at the same time.
How does Kernel Manage processes?
We often use multiple tasks like using a browser, telegram or Discord, Video player or musicplayer at the same time, but are these program's processes run simultaneously, the answer is NO.
All Programs use the CPU for a small time. let's say three processes are running, the first process uses the CPU for some little time, and then the second process, then the third, and this cycle goes on and on. [Kernel handles all switching from one process to another and the time management.]
Can we influence Kernel handling processes management Algorithm?
Yes, we can by using the command
nice
- We can set the priority of a process from High to low and vice versa.
for example, if we want a program to use more computing power, then we set its priority to low means it requires much more care.
$ nice -n -50priority set to high.
renice to change priority meanwhile running process.
Process States
RRunning or Runnable Process.SInterruptable Sleep (waiting for something to happen)DUninterruptable Sleep (can't be killed)ZZombies (Waiting for Wait Call Status to be collected)TStop
Everything in Linux is a file.
Everything in Linux is a file even commands and processes. type the command
ls /proc/to see the list of files.Any Information about processes also is in form of a file.
jobs
- To run a program in the background, not to wait until it finishes. To understand it make a python file
gedit test.pyWrite a code in this file
import time
time.sleep(1000)
this code will sleep for 1000 seconds.
- now run it
python3 test.py
now this program takes some time to finish, and till then you cant use the terminal for other purposes and you don't want to waste time, you want to run it in the background.
- so to put this in the background, use the command
python3 test.py &

[1]is Job ID.13159is PPID.
to check its status
jobs

+sign means that it is the latest jobs which is created.fg %1to put this process in foreground.fg %4to kill the process.kill %4to terminate the process.
- We've come so far to become a Linux Wizard. see you in the next blog. *




