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
PID
is process ID.TTY
is the terminal associated with it.TIME
is the CPU usage time by process.CMD
is the name of command.
to get more details about processes, type the command
ps aux
here flags are,
a
display all processes.u
details about processes.x
list all processes whether a terminal is associated with it or not.
now understand the output of ps aux
USER
is the user name.PID
is the process ID.%CPU
is the CPU time usage.%MEM
is the percentage of the memory allocated to the process.VSZ
Maximum memory allocated to the process when it was initialized which it can use.RSS
actual memory process is used.TTY
terminal is associated with the process.STAT
is the status of the Process.START
starting time of the process.TIME
time taken to execute.COMMAND
command name.top
command 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
TTY
Regular Terminal (no GUI, just a shell, only takes input from keyboard)ets
Pseudo 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,
PID
is Process ID.PPID
is 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?
init
is 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 / 1
Hangup, when associated terminal closed before completing of process.SIGINT / INT / 2
Interruption Signal.SIGKILL / KILL / 9
Killing a process by callingExit System Call
SIGESEGV / SEGV 11
Segmentation fault SignalSIGTERM / TERM / 15
Termination Signal.SIGSTOP / STOP / 19
Stopping 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 -50
priority set to high.
renice
to change priority meanwhile running process.
Process States
R
Running or Runnable Process.S
Interruptable Sleep (waiting for something to happen)D
Uninterruptable Sleep (can't be killed)Z
Zombies (Waiting for Wait Call Status to be collected)T
Stop
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.py
Write 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.13159
is PPID.
to check its status
jobs
+
sign means that it is the latest jobs which is created.fg %1
to put this process in foreground.fg %4
to kill the process.kill %4
to terminate the process.
- We've come so far to become a Linux Wizard. see you in the next blog. *