The Wayback Machine - https://web.archive.org/web/20241125121114/https://www.geeksforgeeks.org/processes-in-linuxunix/
Open In App

Processes in Linux/Unix

Last Updated : 07 Dec, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 

$ ls pwd

Output: 

$ /home/geeksforgeeks/root

When a command/process is running in the foreground and is taking a lot of time, no other processes can be run or started because the prompt would not be available until the program finishes processing and comes out. 
 

Method 2: Background Process: It runs in the background without keyboard input and waits till keyboard input is required. Thus, other processes can be done in parallel with the process running in the background since they do not have to wait for the previous process to be completed. 
Adding & along with the command starts it as a background process 

 $ pwd &

Since pwd does not want any input from the keyboard, it goes to the stop state until moved to the foreground and given any data input. Thus, on pressing Enter:
Output: 

[1]   +   Done                 pwd
$

That first line contains information about the background process – the job number and the process ID. It tells you that the ls command background process finishes successfully. The second is a prompt for another command. 

Tracking ongoing processes

ps (Process status) can be used to see/list all the running processes. 

$ ps

PID       TTY      TIME        CMD
19        pts/1    00:00:00    sh
24        pts/1    00:00:00    ps

For more information -f (full) can be used along with ps  

$ ps –f

UID      PID  PPID C STIME    TTY        TIME CMD
52471     19     1 0 07:20    pts/1  00:00:00f     sh
52471     25    19 0 08:04    pts/1  00:00:00      ps -f

For single-process information, ps along with process id is used  

$ ps 19

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. 

$ ps –f

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

$ kill 19
Terminated

If a process ignores a regular kill command, you can use kill -9 followed by the process ID. 

$ kill -9 19
Terminated

Other process commands:

bg: A job control command that resumes suspended jobs while keeping them running in the background 
Syntax:

bg [ job ]

For example: 

bg %19

fg: It continues a stopped job by running it in the foreground. 
Syntax:  

fg [ %job_id ]

For example  

fg 19

top: This command is used to show all the running processes within the working environment of Linux. 
Syntax:  

top

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. 

renice : To change the priority of an already running process renice is used. 
Syntax:  

renice [-nice value] [process id]

df: It shows the amount of available disk space being used by file systems 
Syntax:  

df

Output:  

Filesystem     1K-blocks      Used Available Use% Mounted on
/dev/loop0      18761008  15246876   2554440  86% /
none                   4         0         4   0% /sys/fs/cgroup
udev              493812         4    493808   1% /dev
tmpfs             100672      1364     99308   2% /run
none                5120         0      5120   0% /run/lock
none              503352      1764    501588   1% /run/shm
none              102400        20    102380   1% /run/user
/dev/sda3      174766076 164417964  10348112  95% /host

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:  

             total       used       free     shared    buffers     cached
Mem:       1006708     935872      70836          0     148244     346656
-/+ buffers/cache:     440972     565736
Swap:       262140     130084     132056

Types of Processes

  1. Parent and Child process : The 2nd and 3rd column of the ps –f command shows process id and parent’s process id number. For each user process, there’s a parent process in the system, with most of the commands having shell as their parent.
  2. Zombie and Orphan process : After completing its execution a child process is terminated or killed and SIGCHLD updates the parent process about the termination and thus can continue the task assigned to it. But at times when the parent process is killed before the termination of the child process, the child processes become orphan processes, with the parent of all processes “init” process, becomes their new pid. 
    A process which is killed but still shows its entry in the process status or the process table is called a zombie process, they are dead and are not used.
  3. Daemon process : They are system-related background processes that often run with the permissions of root and services requests from other processes, they most of the time run in the background and wait for processes it can work along with for ex print daemon. 
    When ps –ef is executed, the process with ? in the tty field are daemon processes.

 


Previous Article
Next Article

Similar Reads

Resource Allocation Techniques for Processes
The Operating System allocates resources when a program need them. When the program terminates, the resources are de-allocated, and allocated to other programs that need them. Now the question is, what strategy does the operating system use to allocate these resources to user programs? There are two Resource allocation techniques: 1. Resource parti
2 min read
Chat application between two processes using signals and shared memory
Prerequisite: C signal handling, IPC through shared memory A signal is used in the UNIX system to notify a process that a particular event has occurred. A signal may be received either synchronously or asynchronously depending on the source and the reason for the event being signalled. A signal must follow the following pattern - 1. A signal is gen
3 min read
Monitoring Linux Users and Processes in Real Time Using whowatch
Whowatch is a console-based, interactive user and process monitoring tool. Whowatch shows the information about the user's currently logged on the machine, in real-time. It shows the information like login name, tty, host, user's processes of logging user's. whowatch shows all processes of the selected user's in tree format. We can send the singles
2 min read
Monitor and Find Statistics for Linux Processes Using pidstat Tool
Pidstat is a command-line tool and part of sysstat suite to monitor the Linux system. It is used to monitor every individual task currently being managed by the Linux kernel on the Linux system. Features of pistat: It can monitor every task running on the systemIt can also monitor the child's task of any task.It shows the total CPU usage by every t
4 min read
Shell Scripting - How to view Processes?
In this article, let's explore how we can view current processes running on a Linux Server. Linux machine gives users the option of multitasking hence naturally there will be multiple processes running on a session and also these processes are important fundamental components of Linux OS, Hence System Administrators may need to be aware of all proc
4 min read
How to kill processes on the Linux Desktop with xkill
xkill is a command-line utility that can kill the undesired windows on the user's screen. Basically, xkill force the X server to close the connection to the client. This utility kills the programs without providing PID with a command. Now let's see how to install the xkill on the systems. Installation of xkill The xkill utility comes with the packa
4 min read
Shell Scripting - How to send Signal to a Processes
Signals allow the operating system to communicate with programs(or processes). There are about 30 standard signals implemented by the Linux OS. We can send signals to processes via certain keyword strokes or by the kill or wait command. Prerequisites: Processes, Bash Scripting, Shell Function Library Sending signals to foreground processes Go to th
4 min read
How to Find Top Running Processes by Memory and CPU Usage
Every current (running) instance of a program is referred to as a process in Linux. There are running processes on every instance of the Linux operating system. , SSHd, SSH processes, and cron jobs are a few examples. A process is started by any application that has to be used. These processes frequently produce new processes. In Unix, each command
6 min read
Get Process ID of Linux Foreground and Background Processes
The PID (process identification number) is a serial number (starting from 1) given by the operating system to uniquely identify the process. Every process started either by the operating system or by the user gets a PID in order of their invocation by the kernel. If we start the same process several times, it will be assigned different PID numbers
4 min read
Zombie Processes and their Prevention
Zombie state: When a process is created in UNIX using fork() system call, the parent process is cloned. If the parent process calls wait() system call, then the execution of the parent is suspended until the child is terminated. At the termination of the child, a 'SIGCHLD' signal is generated by the kernel which is delivered to the parent. Parent,
5 min read
Program to show that Linux provides time sharing environment to processes
Time-sharing means sharing of computing resources among many users (processes) by means of multiprogramming and multitasking. By allowing a large number of users to interact concurrently, time-sharing dramatically lowered the cost of providing computing capability. Many operating system including Windows, Linux and many others provides time-sharing
2 min read
How to List Running Processes in Linux | ps Command
As we all know Linux is a multitasking and multi-user system. So, it allows multiple processes to operate simultaneously without interfering with each other. Process is one of the important fundamental concepts of the Linux OS. A process is an executing instance of a program that carries out different tasks within the operating system.  Table of Co
9 min read
How to Kill Processes by Given Partial Names in Linux
On a Unix system creates a separate environment for a program when it is executed. Everything the system needs to run the program as if there were no other programs in this environment. In Unix, each command you issue initiates or starts a new process. You initiated a process using the ls command to list the directory contents.Simply put, a process
4 min read
Using htop to Monitor System Processes on Linux
htop a Linux tool that is used in process-managing and terminal-based system monitoring. It allows real-time monitoring of processes and performs every task to monitor the process in the Linux system. The tool is written in the C programming language by Hisham Muhammad. It displays a complete list of processes running on the system and gives inform
5 min read
Linux Commands Cheat Sheet
Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous Windows systems. The power of Linux commands in con
13 min read
Linux Interview Questions and Answer
Linux has hundreds of important concepts that you need to understand before an interview, especially if you have 5 years of experience or are applying for roles like DevOps, Cloud Engineer, or Application Support. That's why Linux interview questions are essential for preparation. These questions cover everything from Linux commands to both basic a
15+ min read
Linux/Unix Tutorial
Linux is a widely-used open-source operating system, similar to Windows, Mac, and Android. It shares similarities with Unix, another operating system known for its commercial use. Unix and Linux have comparable components, including the kernel, shell, and programs. Many commands in Unix and Linux exhibit similar behavior and syntax. This Linux tuto
6 min read
25 Basic Linux Commands For Beginners
While performing a task, we all need shortcuts. Shortcuts help us to complete a task quickly. Linux comes with such commands which are one to two words, using that commands, you can perform several operations in no time. As a beginner, you must be aware of those basic Linux commands to complete an operation in a short time in the Linux-based operat
13 min read
What is Linux Operating System
The Linux Operating System is a type of operating system that is similar to Unix, and it is built upon the Linux Kernel. The Linux Kernel is like the brain of the operating system because it manages how the computer interacts with its hardware and resources. It makes sure everything works smoothly and efficiently. But the Linux Kernel alone is not
13 min read
How to Securely Copy Files in Linux | scp Command
scp (secure copy) command in Linux system is used to copy file(s) between servers in a secure way. The SCP command or secure copy allows the secure transferring of files between the local host and the remote host or between two remote hosts. It uses the same authentication and security as it is used in the Secure Shell (SSH) protocol. SCP is known
8 min read
How to Change File Ownership in Linux | chown Command
In the Linux operating system, file ownership is a crucial aspect of system security and user management. The `chown` command, short for "change owner," is a powerful tool that allows users to change the owner of files and directories. This command is particularly useful in scenarios where administrators need to grant or revoke access to specific r
9 min read
Data encryption standard (DES) | Set 1
This article talks about the Data Encryption Standard (DES), a historic encryption algorithm known for its 56-bit key length. We explore its operation, key transformation, and encryption process, shedding light on its role in data security and its vulnerabilities in today's context. What is DES?Data Encryption Standard (DES) is a block cipher with
15+ min read
Carrier Sense Multiple Access (CSMA)
Carrier Sense Multiple Access (CSMA) is a method used in computer networks to manage how devices share a communication channel to transfer the data between two devices. In this protocol, each device first sense the channel before sending the data. If the channel is busy, the device waits until it is free. This helps reduce collisions, where two dev
9 min read
How to Find a File in Linux | Find Command
Linux, renowned for its robust command-line interface, provides a suite of powerful tools for efficient file and directory management. Among these, the "find" command stands out as an indispensable asset, offering unparalleled versatility in searching for files based on diverse criteria. This article explores the prowess of the find command, sheddi
9 min read
How to Copy Files and Directories in Linux | cp Command
In the world of Linux computers, a common and important job is copying files. The key tool for this task is the "cp" command. In this simple guide, we'll explore how to copy a file in Linux, looking at the basic steps, different choices you can make, and giving clear examples. We'll also take a peek behind the scenes to understand how the "cp" comm
8 min read
AWK command in Unix/Linux with examples
Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements that def
8 min read
ZIP command in Linux with examples
ZIP is a compression and file packaging utility for Unix. Each file is stored in a single .zip {.zip-filename} file with the extension .zip. Zip is used to compress files to reduce file size and is also used as a file package utility. Zip is available in many operating systems like Unix, Linux, windows, etc.If you have limited bandwidth between two
7 min read
Sed Command in Linux/Unix with examples
The SED (Stream Editor) command in Unix/Linux is a powerful utility used to process and manipulate text in files. It can perform a variety of operations such as searching, find-and-replace, insertion, deletion, and more, without the need to open the file in an editor. This makes it a highly efficient tool for managing and editing text, especially w
7 min read
grep command in Unix/Linux
The grep command in Unix/Linux is a powerful tool used for searching and manipulating text patterns within files. Its name is derived from the ed (editor) command g/re/p (globally search for a regular expression and print matching lines), which reflects its core functionality. grep is widely used by programmers, system administrators, and users ali
7 min read
Introduction to Linux Shell and Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understanding shell scripting we have to get familiar with th
7 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg