Process-related Commands in Linux

Process-related Commands in Linux

Processes in Linux

  • In the Linux operating system, processes represent the active occurrences of programs.
  • Understanding and managing processes is crucial for system administrators and developers.
  • There are several commands and system calls available to interact with processes.

ps Command

  • The ps command stands for "process status" and is essential for monitoring and managing processes in Linux.
  • It allows users to view detailed information about running processes.
1ps aux
  • The ps command with the aux options displays a detailed list of all processes running on the system.
  • Each line in the output represents a process with information such as PID (Process ID), CPU usage, memory usage, and command.
1USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
2user1     1234  0.0  0.5  12345  6789 pts/0    Ss+  Nov01   0:00 /path/to/command
3

top Command

  • The top command provides a dynamic real-time view of the system processes.
  • It's an interactive command that continuously updates the information.
1Top
1top - 12:34:56 up 1 day,  3:45,  2 users,  load average: 0.10, 0.15, 0.20
2Tasks: 201 total,   1 running, 200 sleeping,   0 stopped,   0 zombie
3%Cpu(s):  2.0 us,  1.0 sy,  0.0 ni, 97.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
4MiB Mem :   3953.1 total,    123.4 free,   3232.2 used,    597.6 buff/cache
5MiB Swap:   4096.0 total,   4095.9 free,      0.1 used.   501.0 avail Mem
6
  • Running top in the terminal displays a live, updating list of processes.
  • It shows details such as CPU usage, memory usage, and process information.
  • Pressing 'q' exits the top command.

pstree Command

The pstree command displays a tree diagram of processes, showing their hierarchical relationship.
1Pstree
1systemd─┬─(sd-pam)
2        ├─atd
3        ├─cron
4        ├─dbus-daemon
5        ├─...
6
  • The pstree command without any options shows a hierarchical view of all processes.
  • It helps visualize parent-child relationships between processes.

nice Command

The nice command is used to launch a process with a specified priority. Lower values represent higher priority.
1
2nice -n 10 ./my_program.
3
  • This command starts the "my_process" with a lower priority, allowing other processes with higher priority to receive more CPU time.
  • The nice command with the -n option sets the priority of the specified command.
  • In this example, the command is launched with a lower priority (higher nice value).

renice Command

  • The renice command enables users to adjust the priority of a running process.
  • This can be useful for dynamically managing system resources based on changing requirements.
1renice 5 -p <PID>
1renice 5 -p 1234
  • The renice command with the priority value and process ID (<PID>) changes the priority of the specified process.

System Calls in Linux

  • System calls in Linux are low-level functions provided by the kernel to allow user-level processes to request services from the operating system.
  • System calls are functions used by programs to request services from the kernel.
  • System calls provide an interface between user-level processes and the kernel, allowing processes to perform privileged operations.

Types of System Calls in Linux

Two fundamental system calls are fork() and exec(). These exemplify process creation and execution, respectively.

Example: fork() - Process Creation

1#!/bin/bash
2
3echo "Before fork"
4
5# Fork a child process
6(
7  echo "Inside child process"
8)
9
10echo "After fork"
  • The fork() system call creates a new process by duplicating the existing one.
  • The script demonstrates a simple shell example using parentheses to create a subshell, simulating a child process.

Example: exec() - Process Execution

1#!/bin/bash
2
3echo "Before exec"
4
5# Replace the current process with a new one (ls -l command)
6exec /bin/ls -l
7
8echo "This line will not be executed"
  • The exec() system call replaces the current process with a new one, specified by the command provided.
  • In this script, the exec command replaces the current shell with the "ls -l" command, and subsequent lines are not executed.

Example: Writing to a File using System Calls

file-writing operation using the open(), write(), and close() system calls.
1#!/bin/bash
2
3# Open a file (or create if it doesn't exist)
4file_descriptor=$(open file.txt O_WRONLY | O_CREAT)
5
6# Check if the file was opened successfully
7if [ $file_descriptor -eq -1 ]; then
8  echo "Error opening file"
9  exit 1
10fi
11
12# Write data to the file
13write $file_descriptor "Hello, System Calls!"
14
15# Close the file descriptor
16close $file_descriptor
  • The script opens a file (file.txt) in write-only mode, creating it if it doesn't exist.
  • It then writes the string "Hello, System Calls!" to the file using the system call.
  • Finally, it closes the file descriptor using the close() system call.