Search

Processes

Aim: To introduce how various processes work in linux.

A process can be defined as any program in execution, the program in turn could be an application,a shell script or the shell it self. All of them are internally processes that are being handled by the kernel.

Each process that is being executed is recognized by a number called as the Processor Identification number of PID. The kernel or the operating system recognizes the processes by this number, which is unique for every process.

Every process can launch more processes, The processes that launches or more technically said "forks" a process is the parent of the new process and the new process is the child. The child in turn can launch more processes and so on.

To see a list of all the processes running in your system you can make use of the command "ps"

The "ps" command by itself lists very few active processes. To see and extensive list of all the processes being run in the system , we can use one of the following options


          ps -e
          ps -ef
          ps -eF
          ps -ely

All the above options give a dtailed out put of all the processes being run in the system. For eg:
ps -ef gives the information in the following format

   UserID,  ProcessID, ParentProcess ID, C(more later) , Start time, tty(terminal) ,  last access time,  command that started the process


The other variations give a little more of littles less information about the processes.

The processes in linux also follow a tree structure just like the file system. Every process starts or spwans out of a previous process. The first process that starts execution in linux is the "init" function that does the forking or spawning of other functions.

To have look how one process is spawned out of other, run the command pstree.

It should give you an output some thing similar to the one shown below.

init─┬─NetworkManager─┬─dhclient
     │                └─{NetworkManager}
     ├─acpid
     ├─apache2─┬─apache2
     │         └─2*[apache2───26*[{apache2}]]
     ├─atd
etc....

To get to know the process id of a specific process we can use a combination of ps and grep as follows

ps -ef | grep "process name" | cut -d' ' -f5 " 


"cut" is a command that can be used to output only specific parts of the input file. In the above example we have "piped" the output of the "ps -ef"  command to "grep" which searches for the specific process by name , the output of that  is in turn piped to the "cut" command.
The output after the grep and before the cut will have all the 7 fields with respect to the specific process. Each filed is separated by a few spaces. The process id which is the second filed is separated by the user id by 5 spaces. Hence we tell the "cut " command to use "space" are the delimiter by giving the -d' ' as the option. and then ask it to print the 5th field after the 5 spaces which is the process id field. The output of the command will be only the process id corresponding to the process.
for eg:
 ps -ef | grep firefox | cut -d' ' -f5  
the above commands might produce
3903

3916
3920
6658

some thing like this.... but that is dependent on your system and will vary widely.

If you are aware of "awk" then you can pick up the process id in the following way too
 ps -ef | grep firefox | awk {'print $2'}



Kill a process: 


To kill a process we can use the command "kill".

syntax: kill [signal] processid 


Generally to kill a process we would use the signal "9".
For eg to kill a process with pid 3920

kill -9 3920


You can get the process id of a process by using the command "ps -ef".

Kill has a lot more options, which you can explore once you get comfortable using Linux and the  command line.








No comments:

Post a Comment