Thursday, September 6, 2012

ps command : what does the TIME indicate?



   One of the output fields of the ps commands is the TIME component. As per the man page of ps command, the TIME is: "CPU utilization of process or thread, incremented each time the system clock ticks and the process or thread is found to be running".  However, most of the times, when the user executes the ps command, he always sees the TIME as  00:00:00 as shown below(usually in development or practice environments). So, what exactly is the TIME? Let us see in this article.
$ ps
  PID TTY          TIME CMD
18315 pts/3    00:00:00 bash
18347 pts/3    00:00:00 ps
TIME:
   It is the total accumulated CPU utilization time for a particular process. The 00:00:00 against the bash process indicates no CPU time has been given at all for the bash process by the kernel till now. Is it confusing? How come the CPU utilization of the bash(login shell) is 0 when we are executing so many commands under bash?

   Whenever a command is executed, say the cat command, the CPU is utilized, or the CPU utilization, is for the cat  command, not for the bash. bash is just the parent process for the cat command. Apart from this, there is nothing between cat and bash. cat command is just an example being taken here, the same is true for any external command or any shell script.

i) Simulation for TIME in the login shell:
   Let us simulate a scenario to get some value in the TIME component for the login shell bash.
$ while [ 1 ]
> do
>  x=1
> done
Run the above code snippet which is a simple infinite while loop. Since it is an infinite loop, you won't get the prompt back.
After a few seconds, terminate the command by pressing Control-C:
^C
Now, run the ps command:
$ ps
  PID TTY          TIME CMD
18315 pts/3    00:00:03 bash
18438 pts/3    00:00:00 ps
   Yes, bash is now showing the TIME as 3 seconds!!! This is the total CPU time utilized by the bash shell. What happened? When the while loop is executed in the bash shell, the CPU time was given to the bash to execute the while loop. Keep in mind, the while is an internal command, and hence the shell does not create a process, instead it is run within the shell itself, and hence the CPU time is given.

   Also, most of the times, for the 'ps' entry in the ps command output, the TIME is 0. This is because the ps commands gets over in no time which could be very few milliseconds, and hence the TIME is 0.

ii) Simulation for TIME for an external command:
   In Unix, there is a command named yes. yes command gives continuous y's as output without ending. It can only be ended by killing it, say by Control-C. Since it runs infinitely, it is a good candidate to check for the CPU utilization.

Running the yes command in background:
$ yes > /dev/null &
[1] 18453
After a few seconds, checking the process list using the ps:
$ ps
  PID TTY          TIME CMD
18315 pts/3    00:00:03 bash
18453 pts/3    00:00:04 yes
18498 pts/3    00:00:00 ps
   yes commad has taken 4 seconds of CPU utilization. And it will continue to take more CPU time if we do not kill the process. By giving the ps command again and again, we can notice the incremental change in the TIME value which will reflect the total CPU time allotted for yes.

3 comments:

  1. Hello,

    how the time in "time" field is calculated? could this value be higher than uptime of the system??

    my issue is that one process (java process) in my system is showing 132407:41 time for the process. which means 132407 minutes and 41 seconds which is almost 92 days, while the system uptime is 67 days!!!

    is it possible that "time" in ps command can be higher than system uptime??

    regards,

    ReplyDelete
  2. Very interesting article about PS Command in Linux. In future, i will surely visiting here for more updates. Keep sharing!

    ReplyDelete
  3. awesome thank you so muchf or the simple explanation

    ReplyDelete