DEV Community

Dharamraj Yadav
Dharamraj Yadav

Posted on

How Linux Actually Runs Your Programs: Processes, Signals & systemd in Plain English

Signals

What is signlas

Signals are mechanism to communicate between kernel and processes, and between processes. For example if we want to STOP the process we send SIGSTOP(kill STOP ) Signal and kernel deliver that signal to process.

Examples Of Signals

  • SIGTERM - Terminate the process

  • SIGSTOP - Stop The process

  • SIGKILL - Forcefully Kill a process

  • SIGINT - Interup the process

The kill command

kill command use for process to kill,terminate,stop etc.

Job Control & background processes

Foreground and Background

Foreground mean a process running on the screen right now and blocks the terminal and this is default behavior of the commands and background mean a process running in the background that not block the terminal

command &
Enter fullscreen mode Exit fullscreen mode

for run process in background.

The & Operator

The & operator is used to run a process in the background. For Example:-

sleep 300 &
Enter fullscreen mode Exit fullscreen mode

jobs, fg, bg commands

  • jobs - Displays the jobs running or stopped in the current shell.

  • fg - Brings a background and stopped job in the foreground.

  • bg - Resumes a stopped job in the background.

nohup command

nohub stand for No Hang Up. it allows a process to continue running even your terminal is closed or your SSH session disconnects.

nohup sleep 300
Enter fullscreen mode Exit fullscreen mode

The process will keep running even if you close the terminal or disconnect your SSH session.

Viewing & monitoring processes

ps
Enter fullscreen mode Exit fullscreen mode

Displays a snapshot of the processes running in the current terminal(current shell session).

ps aux
Enter fullscreen mode Exit fullscreen mode

Displays a snapshot of the all running processes with details such as user,pid,CPU usage,memory usage,process state,time,command etc.

top
Enter fullscreen mode Exit fullscreen mode

Displays a live, real time running processes with CPU and memory usage.

htop
Enter fullscreen mode Exit fullscreen mode

Displays a live running processes in a interactive view. it is more user friendly version of top.

The State column in processes

Every process has a state that indicate its current status. The kernel maintain this state, and commands like ps and top display it to user:-

  • S - Sleeping

  • R - Running/Runnable

  • D - Uninterruptible Sleep (usually waiting for I/O)

  • I - Idle

  • Z - Zombie

  • T - Stops

and process state have some flags like:-

    • - Running in the foreground
  • < - High Priority

  • l - Multi Threaded

  • s - Session leader

  • N - Low Priority

  • L - Pages locked in memory

Finding the top CPU processes

ps aux --sort=-%cpu
Enter fullscreen mode Exit fullscreen mode

and if you want to see top memory consumer

ps aux --sort=-%mem
Enter fullscreen mode Exit fullscreen mode

CPU vs Memory

CPU vs RAM

The CPU is the brain of the computer, The CPU executes instructions and performs calculations.
RAM is the computer's main memory, it stores the data and instructions that program are currently using.

Slow vs Crash

A System Slow indicates that one or more resources(CPU, RAM, Disk, Network) are heavily utilized, causing program to response slowly.
A System Crash occurs when the operating system and a application stops functioning correctly. one possible cause of out of memory.

The OOM Killar

The OOM is a linux kernel mechanism that is triggered when the system is out of memory. instead of allowing the system to become completely unresponsive, the kernel select a high usage CPU and kill that.

How many processes runs at a time?

Thousands of the process can exists in the system, but number of the process that can execute simultaneously depends on the number of logical CPU cores

Programs, Processes & Memory

Programs

Program is a set of of the instructions that store on disk.

Processes

Process is a running instance of the program.

Memory

Memory stores data and instructions that the CPU needs while executing a process.

Swap

Swap is a portion of disk space that linux uses as virtual memory when RAM becomes full.

Systemd

What is systemd

systemd is the first process that runs after the system boot. it ensure that all other essential processes and services are started. and it's PID is 1.

Process scheduling

Priority & the scheduler

  • Priority:- Every Process has priority for decide who run next. and that priority decide from nice if nice value is -20 it mean this is top priority and if process nice is 19 it mean it has low priority.

  • Scheduler:- A CPU Core only execute a process (or thread) at a time. the scheduler is just manage the who runs next and how much CPU time each process gets.

nice Value

The nice value determines the scheduling priority of a normal process. Every normal process has a nice value ranging from -20 to 19. A nice value of -20 gives the process highest priority, while 19 gives it the lowest priority.

nice and renice command

  • nice:- Sets the nice value (priority) of a process when starting the process.

  • renice:- Changes the nice value (priority) of a running process.

taskset command

The taskset command used to set or view the CPU affinity of a process. It allows you to specify which CPU core or cores a process is allowed to run on.

Top comments (0)