A program/command when executed, a special instance is provided by the system to the process. This instance consists of all the services/resources that may be utilized by the process under execution.
- Whenever a command is issued in Unix/Linux, it creates/starts a new process. For example, pwd when issued which is used to list the current directory location the user is in, a process starts.
- Through a 5 digit ID number Unix/Linux keeps an account of the processes, this number is called process ID or PID. Each process in the system has a unique PID.
- Used up pid’s can be used in again for a newer process since all the possible combinations are used.
- At any point of time, no two processes with the same pid exist in the system because it is the pid that Unix uses to track each process.
Initializing a process
A process can be run in two ways:
Method 1: Foreground Process
Every process when started runs in foreground by default, receives input from the keyboard, and sends output to the screen. When issuing pwd command
pwd
Output:
- When a command or process runs in the foreground, it occupies the terminal until it finishes executing.
- This means no other commands or processes can be started during that time.
- The terminal prompt becomes available only after the current process completes.
Method 2: Background processes
It run independently of the terminal, allowing you to perform other tasks simultaneously.
- They do not require keyboard input while running.
- To start a process in the background, add
& at the end of the command.
$ pwd &
Since pwd doesn’t need user input, it runs in the background and then stops until brought to the foreground (if needed). After pressing Enter, you see
- The first line shows the job number and process ID (PID) of the background job, indicating it completed successfully.
- The $ symbol indicates that the terminal is now ready for another command.
Tracking ongoing processes
ps (Process status) can be used to see/list all the running processes.
ps
- For more information -f (full) can be used along with ps
$ ps –f
Output:
- For single-process information, ps along with process id is used
Command:
$ ps 19
Output:
PID TTY TIME CMD
19 pts/1 00:00:00 sh
For a running program (named process) Pidof finds the process id’s (pids)
Fields described by ps are described as:
- UID: User ID that this process belongs to (the person running it)
- PID: Process ID
- PPID: Parent process ID (the ID of the process that started it)
- C: CPU utilization of process
- STIME: Process start time
- TTY: Terminal type associated with the process
- TIME: CPU time is taken by the process
- CMD: The command that started this process
There are other options which can be used along with ps command :
- -a: Shows information about all users
- -x: Shows information about processes without terminals
- -u: Shows additional information like -f option
- -e: Displays extended information
Stopping a process:
When running in foreground, hitting Ctrl + c (interrupt character) will exit the command. For processes running in background kill command can be used if it’s pid is known.
Command:
$ ps –f
Output:
UID PID PPID C STIME TTY TIME CMD
52471 19 1 0 07:20 pts/1 00:00:00 sh
52471 25 19 0 08:04 pts/1 00:00:00 ps –f
Command:
$ kill 19
Output:
Terminated
If a process ignores a regular kill command, you can use kill -9 followed by the process ID.
Command:
$ kill -9 19
Output:
Terminated
Other process commands:
1. bg: A job control command that resumes suspended jobs while keeping them running in the background
Syntax:
bg [ job ]
For example:
bg %19
2. fg: It continues a stopped job by running it in the foreground.
Syntax:
fg [ %job_id ]
For example
fg 19
3. top: This command is used to show all the running processes within the working environment of Linux.
Syntax:
top
Output:
4. nice: It starts a new process (job) and assigns it a priority (nice) value at the same time.
Syntax:
nice [-nice value]
nice value ranges from -20 to 19, where -20 is of the highest priority.
5. renice : To change the priority of an already running process renice is used.
Syntax:
renice [-nice value] [process id]
6. df: It shows the amount of available disk space being used by file systems
Syntax:
df
Output:
7. free: It shows the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel
Syntax:
free
Output:
Types of Processes
There are three types of Processes
1. Parent and Child process :
- Every process in Linux is created by another process, known as the parent process.
- The newly created process is called the child process.
- The
ps -f command displays the Process ID (PID) and the Parent Process ID (PPID) in its 2nd and 3rd columns. - Most user processes have the shell as their parent process.
2. Zombie and Orphan process :
- When a child process completes its execution, it sends a SIGCHLD signal to the parent process, informing it of the termination.
- If the parent process is terminated before the child finishes, the child becomes an orphan process.
- Orphan processes are then adopted by the init (PID 1) process.
- A zombie process is a process that has finished execution but still remains in the process table because its parent has not yet read its exit status.
- Zombie processes are essentially dead processes that occupy minimal system resources.
3. Daemon process :
Daemon processes are background system processes that run continuously to provide services.
- They usually run with root privileges and handle requests from other processes.
- Common examples include the print daemon or web server daemons.
- Daemons do not have an associated terminal (TTY), when you run
ps -ef, processes with a ‘?’ in the TTY field are daemon processes.
Explore
Linux/Unix Tutorial
5 min read
Getting Started with Linux
Installation with Linux
Linux Commands
Linux File System
Linux Kernel
Linux Networking Tools
Linux Process
Linux Firewall
Shell Scripting & Bash Scripting