Process Management
Week 7 Lesson 2
Learning Outcomes
Understanding Linux Processes
Process Management
• The key to being able to effectively manage Linux processes is to first
understand how processes function within the operating system. So,
what exactly is a process?
• A process is a program that has been loaded from a storage drive
into system
• RAM and is currently being processed by the CPU on the
motherboard
Main areas to focus
• Types of Linux programs
• User processes versus system processes
• How Linux processes are loaded
Type of Programs
Description
• Binary executables: Programs originally created as a text file
using a programming language such as C or Java. The text file is
run through a compiler, which converts the program into
machine language. This final binary file is processed by the CPU.
• Internal shell commands: Commands that are rolled into a shell
program itself, like the Bash shell. For example, when entering
cd, history, or exit at a shell prompt, it actually runs an internal
shell command.
• Shell scripts: Text files that are interpreted through the Bash shell
itself. They include commands that run binary executables and
internal shell commands within the shell script.
Process Flow
These programs are loaded into RAM and executed by the CPU.
• Linux quickly switches between various processes running on
the CPU, making it appear as if multiple processes run
concurrently. However, the CPU actually only executes a
single process at a time.
• The Linux operating system uses several types of processes.
Some processes are created by the end user when they
execute a command from the shell prompt or through the
graphi-cal interface. These processes are called user
processes. User processes are usually associated with some
kind of end-user program running on the system.
Processes tracking
• The key point to remember about user processes is
that they are called from within a shell and are
associated with that shell session.
• a system process is used to provide a system service,
such as a web server, an FTP server, a file service
such as Samba, a print service such as CUPS, or a
logging service. Such processes run in the background
and usually don’t provide any kind of user interface.
Processes tracking
• ps –e displays every process running on the system
• Most system processes are noted with a letter d at the
end of the name, which stands for daemon.
• The system has many system processes running, and
these are loaded after the kernel is booted so they are
not associated with a shell. User processes are tied to
the shell instance they were called from.
How Linux Processes Are Loaded
• All Linux processes are loaded by one single process—
either the legacy SysVinit (init) or the newer systemd,
depending on the distribution—that is started by the
Linux kernel when the system boots.
• Understand that any process running on a Linux
system can launch additional processes. The process
that launched the new process is called the parent
process. The new process itself is called the child
process.
Process Identification
• For any process on a Linux system, you need to be
able to uniquely identify it as well as its heredity.
Whenever a process is created on a Linux system, it is
assigned two resources:
• Process ID (PID) number This is a number assigned to
each process that uniquely identifies it on the system.
• Parent process ID (PPID) number This is the PID of the
process’s parent process (that is, the process that
spawned it).
Process Identification
Managing Processes
Managing running processes is one of the key tasks
performed on Linux systems.
Viewing running processes
• $ top/ htop
• $ ps
• $ pgrep / pidof
Prioritizing Processes
Prioritizing processes
• By default, Linux tries to equalize the amount of CPU time
given to all processes on the system. However, sometimes
you may want to adjust the priority assigned to a process.
• Depending on how the system is deployed, a particular
process may be set to have a higher priority than other
processes.
This can be done using several Linux utilities.
• Setting priorities with nice
• Changing priorities with renice
Managing Processes
Recall from our previous discussion of top and ps that each
process running on the system has a PR value and NI value
associated with it.
• The PR value is the process’s kernel priority. The higher the
number, the lower the priority of the process. The lower the
number, the higher the priority of the process. The NI value is the
nice value of the process, from the adage “nice guys finish last.
• The nice value is factored into the kernel calculations that
determine the priority of the process. The nice value for any
Linux process ranges between –20 and +19. The lower the nice
value, the higher the priority of the process.
• You cannot directly manipulate the priority of a process, but
you can manipulate the process’s nice value.
Prioritizing Processes
openSUSE:~ $ nice bash
openSUSE:~ $ nice -n 19 bash
openSUSE:~ $ ps -o pid,pri,ni,cmd
• The ps -o option allows us to list the columns to view from
ps. Notice that PID 8389 runs at the default nice value of
10, and PID 8470 runs at 19
Changing Priorities with renice
• Instead of having to kill a process and restart it with nice
to set its nice value, use the renice command to adjust the
nice value of a process that is currently running on the
system. The syntax for using this command is renice
<nice_value> <PID>.
openSUSE:~ $ renice 15 8389
openSUSE:~ $ ps -o pid,pri,ni,cmd
Some renice need sudo
openSUSE:~ $ renice 10 8389