Process in Linux/Unix explained with ps command examples

As we know Unix is a multi-user and multi-task operating system. It means that at every instant there might be programs of several users be running in memory. all these programs share the CPU’s attention between them.

What is a Process?

There is a difference between a program and a process. a program becomes a process when it starts executing.

Before going to the ps command let’s understand how processes work in unix. you can skip this and jump to commands if you want but its always good to know some theory.

There are several processes running in the memory at a particular instance, but the CPU can give its attention to only one process at a time.

There is a program called ‘scheduler’ which decides which program should get the CPU’s attention and when.

When you execute a program the scheduler submits it to a queue called process queue, at this moment the process is said to be in submit state. After submission the process waits for its turn for some time. At this stage the process is said to be in hold state. After some more time the process becomes the next one in the queue to get CPU attention. At this stage it is called to be in ready state. Finally the process gets the CPU attention and said to be in run state. In the middle of execution it may happen that the time alloted to this process gets over and CPU starts running another process. The old process is returned back to the ready state and placed back in the process queue. As the CPU takes it attention to the new process the necessary parameters of the old process is saved so that the CPU can take it from where it left it. Now, the old process waits for its time slice to arrive.

Some process are required to do some disk input/output which is a slow operation. The CPU won’t wait for it to complete its operation so such process will be kept in wait state until input/output is over and are then placed in the ready state.

A process whose execution finishes gets the complete state and removed from the process queue.

The ps command examples and usages

if you want to see what processes are currently running

$ ps

 PID TTY          TIME    CMD

 5116 pts/6    00:00:00    bash

 5165 pts/6    00:00:00    ps

There is a unique number called process ID (PID) associated with every running process.

we can see that there are two processes runnning each with their unique PID. It also shows the time elapsed since the processes were started. and under CMD the process name is given. The first pocess is bash and other one is ps itself

Note that the PID is not fixed for a particular process. if you run the ps command again you are most like to see some other PIDs for the processes.

But you can see that the ps command shows processes only for you terminal. To find out processes run by other users use the -a option which stands for all users

$ ps -a

if you want to see the process of a particular user

$ ps -u james

where -u stands for user and james is the username of the user
Process launched from a particular terminal

$ ps -t tty3d

we can drop the tty and get the same results

$ ps -t 3d

view detailed information about the running processes using the -f option

$ ps -f
aman 4069 4062 0 13:25 pts/4 00:00:00 bash
aman 5107 4069 0 14:28 pts/4 00:00:00 ps -f

in Unix one process (called parent) gives birth to another process (called child) the PPID field stands for PID of the parent process. so looking at the PID of bash and PPID of ps -f you can say that the ps -f is the child process of bash process, which means that ps -f was launched by the shell (bash). under UID is the name of the user running the process. under C is the CPU utilization of the process
Till now we have seen only the processes associated with individual users. But there are many more running which are necessary for the system to work

$ ps -e

Leave a Reply

Your email address will not be published.