Processes in Linux
Processes in Linux
•
••
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.
$ 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.
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.
Pressing CTRL+Z in between the execution of the command will stop it. Note: In this
case the name of the process is sleep 100 but you may change the same as per your
need. 3. To get the list of jobs that are either running or stopped.
jobs
It will display the stopped processes in this terminal and even the pending
ones. 4. To run all the pending and force stopped jobs in the background.
bg
This will start the stopped and pending processes in the background. 5. To get
details of a process running in background.
ps -ef | grep sleep
Note: In this case the name of the process is sleep 100 but you may change the same
as per your need. 6. To run all the pending and force stopped jobs in the foreground.
fg
This will start the stopped and pending processes in the foreground. 7. To run a
process in the background without getting impacted by the closing of the terminal.
nohup sleep 100 &
While executing, it will even store all the output after execution
in nohup.out file. Note: In this case, the process is sleep 100, you may modify it as
per your need. 8. To run some processes in the background directly.
sleep 100&
This will run the process in the background and will display the process id of the
process. Note:- In this case, the process is sleep 100, you may modify it as per your
need. 9. To run processes with priority.
nice -n 5 sleep 100
The top priority is -20 but as it may affect the system processes so we have used the
priority 5. Note: In this case, the process is sleep 100, you may modify it as per your
need. 10. To get the list of all the running processes on your Linux machine.
top
This will display all the processes that are currently running in your system.