| Understanding Processes: What They Are and How Linux Manages Them |
|
a process is software which has been loaded into memory currently executed by the CPU Linux is a multitasking system a single CPU core can only do one thing at a time ilusion of multi-tasking is created by the Scheduler switches billions of times per second The Process ID (PID)
kernel assigns unique sequential number to each processIDs start with 1 on boot User-Space vs. Kernel-Space
processes generally live in the user spacehave limited powers interacts with the kernel to talk with hardware a crash in the user space does no affect the kernel or hardware |
| Viewing Running Processes: ps, top, htop |
|
The Snapshot: ps
ps command stands for Process Statustakes a static photo of what is happening short list from a user's terminal PID TTY TIME CMD 1234 pts/0 00:00:00 bash 1255 pts/0 00:00:00 psonly shows processes running in the current terminal session usually want to see everything running on the system use a standard combination of option aux
returns massive list the columns in the list
The Dashboad: top
top is a video feed of same information in real-timeupdates every ~3 seconds display has two parts
The Friendly Dashboard: htop
htop is a modern alternative to topto install sudo apt install htop |
| Understanding Process Hierarchy: Parent and Child Processes |
|
every process is created by another process creates a family tree The Ancestor: PID 1
when booted the kernel starts one process systemd with PID 1PID 1 the starts other essential services
Bash is the parent of Firefox Firefox is the child of Bash Why Hierarchy Matters
when a child process finishes its task, it notifies its parentparent then cleans up the memory the child was using when a parent terminates usually its children will be terminated as well |
| Process States: Running, Sleeping, Stopped, Zombie |
|
process is not always using the CPU has different states
|
| Managing Processes: kill, killall, pkill |
|
The Precision Strike (kill)
utility for sending signals to processesneed to us PID kill <process ID> The Name Strike (killall)
just know the name of the program
killall firefoxwill terminate every process named 'firefox' The Pattern Strike (pkill)
use a partial name
pkill firekills every process which contains 'fire' in its name |
| Understanding Signals: SIGTERM, SIGKILL, and Others |
|
kill command sends a signal to the process the process determines how to handle the message Linux has dozens of standard signals signals referenced by name or number below firefox is assigned PID 4521 essential signals SIGTERM (Signal 15) - The Polite Request
the default signal sent by the kill commandwhen a process receives SIGTERM then has a chance to save its work, close its open files, disconnect from the network, and exit cleanly kill 4521defaults to -15 SIGKILL (Signal 9) - The Ultimate Sanction
sometimes a process can't hear the SIGTERM requeststuck in a loop and ignoring the world use SIGKILL SIGKILL is not sent to the process sent to the Linux Kernel to focre process shutdown it can lead to data loss or corrupted files kill -9 4521 SIGHUP (Signal 1) - The Reload
SIGHUP stands for 'hang up'tell a background service to reload its configuration no need to stop and restart the service to apply changes kill -1 4521 SIGINT (Signal 2) - The Interrupt
sends a stop what you're doing to a foreground commandCtrl+C |
| Foreground and Background Processes: &, fg, bg, jobs |
|
when a process runs in the terminal it runs in the foreground the terminal is 'occupied' until the process completes Starting in the Background (&)
to start a process in the background add an ampersand & to the end of the command
cp massive_video.mp4 /backup/ &the copy runs in the background and the prompt returns immediately Pausing and Moving (Ctrl-Z, bg, fg)
Listing Jobs (jobs)
to see what is running in the background of current shell use the jobs command
[1]- Running cp massive_video.mp4 /backup/ & [2]+ Stopped nano notes.txtshows the job ID for each process background processes created from a terminal are attached to the shell if the terminal is closed, its background processes will all terminate |
| Process Priority: nice and renice |
|
CPU switches between tasks doesn't always treat tasks equally more attention to important tasks than to unmportant ones niceness value determines priority a nice process lets others go first a not nice process pushes to the front of the line The Niceness Scale
runs from -20 to 19
Setting Priority at Start (nice)
should run script which will slow down computer with a high niceness value
nice -n 10 tar -czf backup.tar.gz /home/annatells system to run the backup but be nice Changing Priority of Running Processes (renice)
if a process is running and slowing down the system, can make it nicer without stopping itneed the PID renice 10 -p 4521changes the process' niceness to 10 The Root Restriction
standard users can increase the niceness of their own processesonly the root user can decrease niceness |
| Real-World Scenario: The Runaway Script |
|
shared server is sluggish delay experienced while typing in terminal
|
| Summary |
covered
key points
|