A
Process is a running instance of a program. So whenever we start something,
there is a process associated with that. If you have two files opened on your
window, then we have 2 process belonging to those files available. A process
started will contain all of its variables and also states.
The CPU
runs switches from one process to other process making the user think that it
is running multiple processes at a time, but a cpu is capable of running only
one process at a time.
Process ID
Process
ID : Each process in a linux is identified by a process ID which is also
referred as pid.This is 16bit number that are assigned to process whenever a
new one starts. To find the process id , we can use 'ps' command like
[root@vx111a
~]# ps
PID TTY TIME CMD
3477 pts/1
00:00:00 bash
5333 pts/1
00:00:00 ps
The
PID column gives the process ID of the instance running (bash terminal has
process ID 3477).
Every
process has a parent process associated to it. A process will look much like a
tree in linux. The parent process Id is referred as ppid.
[root@vx111a
~]# ps -eo pid,ppid | head
PID
PPID
1
0
2
1
3
1
4
1
The
PID is the process id and PPID is the parent process that started the process.
Viewing Process: In order to view the process, as i said
above we can use 'ps' command.
We
can various sort of information about the process by passing parameters to the
ps command like ,
[root@vx111a
~]# ps -e -o pid,ppid,command | head
PID
PPID COMMAND
1
0 init [5]
2
1 [migration/0]
3
1 [ksoftirqd/0]
4
1 [watchdog/0]
5
1 [migration/1]
6
1 [ksoftirqd/1]
This
gathers information like pid,ppid and command running that process.
Killing a process
We
can kill a process using the 'kill' command like,
kill
-9 3477
Process Scheduling
In
Linux, we are never aware that which process will be completed first either the
child or parent. We cant even say which process will be given more time for its
execution. We have facilities in linux to start a process with less priority or
high.
Every
process can be associated with a niceness value to it. Every process has a
niceness of zero. A higher niceness value means that the process is given a
lesser execution priority. A process with a lower value (that is, negative)
niceness gets more execution time. Priorities run from 19 (lowest) to -20 (highest). Only root may
set the negative (higher) priorities.
A
niceness should not be confused with a scheduling priority, which lets applications
determine the order in which threads are scheduled to run. Unlike a priority, a
niceness is merely advice to the scheduler, which the scheduler is free to
ignore.
In
order to check the nice values for the processes running, we can use
[root@vx111a
~]# ps axl | head
F UID
PID PPID PRI NI
VSZ RSS WCHAN
STAT TTY TIME COMMAND
4 0
1 0 15 0 10364 640 ? Ss ?
0:09 init [3]
1 0
2 1 -100
- 0 0
migrat S<
? 0:07 [migration/0]
1 0
3 1 34 19 0 0 ksofti SN
? 0:01 [ksoftirqd/0]
6th
column (NI) with value 0 is the nice.
When
we run a process on background it takes the nice value as '0'
[root@vx111a
~]# ./sam.sh &
[1]
1789
[root@vx111a
~]# ps axl | grep sam
0 0
1789 3769 16
0 4488 988 wait
S pts/2 0:00 /bin/bash ./sam.sh
We
can give a nice value (greater) by using,
nice
--10 ./test.sh & (-10). The negative values can only be provided by the
root user.
Linux
also provides facilities for increasing or decreasing the nice values by using
renice command.
if
you run CPU-bond processes you must use nice command used to start process with
modified scheduling priority / nicenesses. renice command is used to change the
priority of a process that's already running. To increase the nice value (thus
reducing the priority), execute the renice command as shown below.
[root@vx111a
~]# ps axl | grep sam
0 0
1789 3769 16
0 4488 988 wait
S pts/2 0:00 /bin/bash ./sam.sh
[root@vx111a
~]# renice 16 -p 1789
1789:
old priority 0, new priority 16
[root@vx111a
~]# ps axl | grep sam
0 0
1789 3769 33
16 4488 988 wait
SN pts/2 0:00 /bin/bash ./sam.sh
Signals
Signals
are mechanisms for communicating with and manipulating processes in Linux.When
ever we give a kill command to a process , it sends a signal (9 : force kill)
to the process.when a process receives a signal, it processes the signal
immediately, without finishing the cur-rent function.We can also use
kill
-s pid
Some
of the linux signals and Id's are
0 SIGNULL
1 SIGHUP wake up an idling program
2 SIGINT Interrupt
3 SIGQUIT quit,Terminate with core dump
9 SIGKILL Force Termination
15 SIGTERM Terminate
24 SIGSTOP stop
26 SIGCONT continue
SIGBUS bus
error
SIGSEGV segmentation violation
SIGFPE floating point exception
Process Termination
A
process terminates in one of two ways. Either the executing program calls the
exit function, or the program’s main function returns. Each process has an exit
code: a number that the process returns to its parent. The exit code is the
argument passed to the exit function, or the value returned from main.
Zombie Processes
A
zombie process is a process that has terminated but has not been cleaned up
yet. It is the responsibility of the parent process to clean up its zombie
children.
The
status of the zombie process will be in 'z' state. We can find the zombie
process using ,
ps
aux | awk '{ print $8 " " $2 }' | grep -w Z
BackGround and ForeGround Jobs
Executing
a background job : Appending an ampersand ( & ) to the command runs the job
in the background.
[root@vx111a
~]# ./sam.sh &
[1]
2105
Move
a foreground job to background by,
Press
‘CTRL+Z’ which will suspend the current foreground job.
Execute
bg to make that command to execute in background.
[root@vx111a
~]# ./sam.sh
[1]+ Stopped ./sam.sh (ctrl +z Pressed)
[root@vx111a
~]# bg
[1]+
./sam.sh &
View all the background jobs: To view all the jobs running background
,we can use the 'jobs' command like
[root@vx111a
~]# jobs
[1]+ Running ./sam.sh &
job
from the background to the foreground
[root@vx111a
~]# jobs
[1]+ Running ./sam.sh &
[root@vx111a
~]# fg %1
./sam.sh
Kill
a specific background job using kill %
[root@vx111a
~]# ./sam.sh
[1]+ Stopped ./sam.sh
[root@vx111a
~]# jobs
[1]+ Stopped ./sam.sh
[root@vx111a
~]# kill %1
[1]+ Terminated ./sam.sh
display a tree of processes : To display a tree of process from
parent to child ,we can pstree command like
[root@vx111a
~]# ./sam.sh
[1]+ Stopped ./sam.sh
[root@vx111a
~]# ps ux | grep sam
root 2229
0.0 0.0 4488
984 pts/2 T
02:26 0:00 /bin/bash ./sam.sh
root 2232
0.0 0.0 3920
664 pts/2 S+ 02:26
0:00 grep sam
[root@vx111a
~]# pstree 2229
sam.sh───sleep
Process Status Code : For every process In Linux , there is a status code available.When ever we run 'ps ux' command , the 'STAT' column in the output says about the status of the Process.The available status codes in linux are
D : Un interruptiable Sleep ( usually I/O )
R : Running Or Runnable ( On Run Queue )
S : Interruptible Sleep (Waiting for an Event to complete)
T : Stopped
X : dead
Z : Zombie
Process Status Code : For every process In Linux , there is a status code available.When ever we run 'ps ux' command , the 'STAT' column in the output says about the status of the Process.The available status codes in linux are
D : Un interruptiable Sleep ( usually I/O )
R : Running Or Runnable ( On Run Queue )
S : Interruptible Sleep (Waiting for an Event to complete)
T : Stopped
X : dead
Z : Zombie
sleep
sleep
is a command in linux which delays some thing for a specified amount of time.we
can use sleep like
sleep
5 && echo "jagadesh"
usleep
Micro sleep (the u may be read as the Greek
mu, or micro- prefix). This is the same as sleep, above, but "sleeps"
in microsecond intervals. It can be used for fine-grained timing, or for
polling an ongoing process at very frequent intervals.
Happy
learning, more to come.