Linux Processes



Process: 

Process is an instance of a running program. When you start a program or running an application in Linux, you actually execute that program.  Whenever a program is executed, a new process is created. A process also consumes resources like the file system, memory or other CPU resources. This gives rise to the need of process management in Linux.



PID:

A PID (Process Identifier) is a number which is uniquely assigned when the process is created. The PID’s are allocated sequentially as the processes are being created. In Linux init is the first process which PID is 1, other process generally starts from 2. There is max limit of PID which can be known by command

$ cat /proc/sys/kernel/pid_max


When the sequentially allocated PID reaches the maximum value, it wraps to the lower limit and the next PID’s allocated are the available ones starting from the lower limit.



Linux Process


Type of Process:


Interactive processes: 

Interactive process needs user’s interaction while it is active. For example, when we launch a vi-editor, it is an interactive process. Another example could be the telnet command. Hence, the interactive processes have to be associated to a terminal.

Interactive processes can be Foreground and Background processes.

Foreground Process: 

A process is a foreground process if it is in focus and can be given input from the standard input. It blocks the shell until the foreground process is complete. When we run our commands on the terminal, they generally run as foreground processes.

Background Process: 

Background processes are ones that are running, but in the background, not taking any user input from the terminal. It doesn’t block the terminal, and allows us to use the terminal irrespective of the background process is complete or not. They key-character to make any new process to be run in background is ‘&’.

For example a process is bg_process.sh, for running this in background command is

$ ./bg_process.sh &


Batch processes: 


These are the processes which are queued in and executed one by one in First In First Out. Batch processes are not associated with any terminal instead given to the system to run.

There are two Linux commands which are provided to create the batch processes:
at and batch


Daemon Processes: 


A daemon process in Linux is also one of its kind which runs in background. However, what is different here is, daemon processes are not associated to any terminal in any way. Therefore such processes don’t take interact with the user. A widespread example of daemon process is a server service of any kind, like httpd, samba, mail server. 



State of Process:  

As a process executes it changes state according to its circumstances. Linux processes have the following states: 

Running (R): The process is either running (it is the current process in the system) or it is ready to run (it is waiting to be assigned to one of the system's CPUs).  Running always does not mean utilising the CPU. Even while a process is ready to run, the state is running state.

Waiting or Sleep: The process is waiting for an event or for a resource. Linux differentiates between two types of waiting process; interruptible and uninterruptible.
Interruptible waiting(S) processes can be interrupted by signals whereas uninterruptible waiting (D) processes are waiting directly on hardware conditions and cannot be interrupted under any circumstances.


Stopped (T): The process has been stopped, usually by receiving a signal. A process that is being debugged can also be in a stopped state.

Zombie or defunct (Z): This is a halted process which, for some reason, still has a task_struct  data structure in the task vector. It is what it sounds like, a dead process.


COMMAND-LINE Options to check Process

This version of ps accepts several kinds of options:

1) UNIX options, which may be grouped and must be preceded by a dash.

2) BSD options, which may be grouped and must not be used with a dash.

3) GNU long options, which are preceded by two dashes.

EXAMPLES

To see every process on the system using standard syntax:

ps -e
ps -ef
ps -eF

ps -ely

To see every process on the system using BSD syntax:

ps ax

ps axu

For BSD formats and when the stat keyword is used, additional characters may be displayed:

< high-priority (not nice to other users)

N low-priority (nice to other users)

L has pages locked into memory (for real-time and custom IO)

s is a session leader

l is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)

+ is in the foreground process group.

Listing of Process:  

The Linux command used to view list of processes is ‘ps’ i.e. process status.

$ ps

PID TTY TIME CMD

353 tty1 00:00:01 bash

736 tty1 00:00:00 ps


 PID is the process Identifier a unique process no.

TTY is terminal-type or name of the console/terminal.

TIME is the CPU time since the process has started.

CMD is Command name that started the process.


List all process:

$ ps –e

$ ps –A

To get more information about each process, we use option ‘-f

$ ps -ef
UID        PID  PPID  C    STIME  TTY     TIME   CMD
root         1     0           0    Mar08   ?        00:00:00  /sbin/init
root         2     0           0    Mar08   ?        00:00:00  who
root         3     2          0    Mar08   ?        00:00:01  find /
root         5     2           0     Mar08  ?        00:00:00  bash
root         6     2           0    Mar08   ?         00:00:00   vim
root         7     2           0    Mar08   ?        00:00:00    ssh
root         8     2          0     Mar08   ?        00:00:00   ls
root         9     2          0     Mar08   ?        00:00:00   top
root        10     2          0    Mar08   ?        00:00:00  grep
..
..
..

Here

UID is the user who started or owns the process.

PID is process ID

PPID is parent process ID of that process.

Parent Process ID (PPID): 

Every process (except init) is created by some other process, and that process is called the parent process of the newly created process.

C is the percentage of CPU usage.

STIME is start time of the process.

Time is cumulative CPU time since the process is running. 





jobs fg bg commands in Linux                                              Linux text Editor

No comments: