
What is a Process?
A process is an instance of a running program, which represents the execution of a set of instructions in a sequential manner. Every application you open, whether it be a simple text editor, a complex graphic design software, or a web browser, generates a process that operates independently in the system's memory. Similarly, every command you execute in a command-line interface, such as terminal commands in Linux, also creates a distinct process. This means that each operation you perform on your computer is encapsulated within its own process, allowing for multitasking and efficient resource management.
The Linux kernel plays a crucial role in the management of these processes. It is the core component of the operating system that interacts directly with the hardware and facilitates communication between the software applications and the underlying physical resources. The kernel is responsible for allocating system resources such as CPU time, memory space, and I/O devices to each process, ensuring that they have the necessary resources to execute effectively.
Additionally, the kernel implements scheduling algorithms to manage how processes are executed. It prioritizes processes based on various factors, such as their importance, the amount of time they have been waiting, or their resource requirements. This scheduling ensures that all processes receive fair access to the CPU, which is vital for maintaining system responsiveness and performance, especially when multiple applications are running simultaneously.
Overall, the process management capabilities of the Linux kernel are fundamental to the efficient operation of the operating system, allowing users to run multiple applications seamlessly while ensuring that system resources are utilized optimally and fairly across all running processes.
Types of Processes:
Foreground Processes: These processes run in the foreground, meaning they occupy the terminal and require your direct interaction. You can't execute other commands in the same terminal until the foreground process finishes.
Background Processes: These processes run in the background, freeing up your terminal for other tasks. You can launch background processes using the & symbol at the end of a command.
Daemon Processes: These are background processes that run without any user interaction. They typically start at boot time and provide system services, such as web servers or print spoolers.
Orphan Processes: A process becomes an orphan when its parent process terminates before it does. The init process (PID 1) adopts orphan processes.
Zombie Processes: A zombie process is a process that has completed execution but still has an entry in the process table.1 This usually happens when the parent process hasn't properly collected the child's exit status.
Managing Processes: Essential Commands
ps (Process Status): Displays information about running processes.
ps: Lists processes associated with the current terminal.
ps aux: Lists all running processes, including those from other users.
ps -ef: Lists all processes with full information.
ps -u username: List process of a specific user.
ps -p PID: List process with a specific PID.
bg (Background): Moves a suspended foreground process to the background.
bg %job_id: Moves the specified job to the background.
fg (Foreground): Moves a background process to the foreground.
fg %job_id: Moves the specified job to the foreground.
nice: Adjusts the priority of a process.
nice -n priority command: Starts a process with the specified priority (lower numbers are higher priority).
renice -n priority -p PID: Changes the priority of an existing process.
kill: Terminates a process.
kill PID: Sends a termination signal to the specified process.
kill -9 PID: Sends a forceful termination signal (kill -KILL). Use with caution.
killall process_name: Kills all processes with the specified name.
Examples:
Start a background process:
sleep 60 &
//This starts a sleep process that will run for 60 seconds in the background.
View background jobs:
jobs
Bring a background process to the foreground:
fg %1
//(Assuming the sleep job has job ID 1).
Change process priority:
nice -n 10 sleep 120 &
//This starts a sleep process with a lower priority.
Kill a process:
ps aux | grep sleep kill PID
//This finds the process ID (PID) of the sleep process and then terminates it.
Practice Session 1: With Examples
List all running processes:
ps aux
Find the PID of a specific process (e.g., gedit):
ps aux | grep gedit
Start a long process in the background (e.g., sleep 60):
sleep 60 &
Bring the background process to the foreground:
fg
Suspend a foreground process (e.g., ping google.com):
Ctrl+Z
Move the suspended process to the background:
bg
Change the priority of a process (e.g., sleep 100):
nice -n 10 sleep 100 &
renice -n -5 -p [PID]
Terminate a process (e.g., kill [PID]):
kill [PID]
kill -9 [PID]
Practice Session 2 (Without Examples):
Start a process that takes a long time to complete and send it to the background.
Find the process ID of the background process.
Change the priority of the background process.
Bring the background process to the foreground.
Suspend the foreground process.
Move the suspended process back to the background.
Terminate the process.
Key Takeaways:
Understanding process types helps you manage system resources effectively.
ps, bg, fg, nice, and kill are essential commands for process management.
Use kill -9 with caution, as it can lead to data loss.
Comentarios