The execution of threads and processes can be monitored by analyzing process statistics such as CPU and memory usage, which are displayed by commands such as ps and top.
The commands used to obtain information about process and threads in UNIX based systems are:
The ps command provides a report of current processes in the system. The process table shown by the ps command shows process attributes such as user, PID, status and size
The options that the ps command supports are:
-a: Shows all processes on each terminal and user.
-r: Shows only running processes
-l: Shows all processes with flags
-x: Shows all processes on all terminals
-m: Shows both processes and threads
-c: Shows processes that match the given command
This code snippet shows a ps command with the Ã¢â‚¬“c option:
ps -c httpd
-u: Shows processes that match the given command
The various columns in the output of the ps command are:
** User: Name of the user who has spawned the process
** PID: The process identifier, which is unique to every process
** %CPU: Percentage of CPU used by the process
** %MEM: Percentage of main memory used by the process
** VSZ:: Amount of virtual memory used by the process in bytes
** RSS: Amount of main memory used by the process in bytes
** TTY: Terminal name
** STAT: Process status
** START: Time when the process started
** TIME: Cumulative of use and system time for which the process has executed.
** COMMAND: Name of the command or process
The Ã¢â‚¬“l options can be used with the ps command to obtain information on the behavior of processes. Process behavior is indicated by the flag or F field shown in the process table.
The pstree command shows current processes in the form of a hierarchical structure. By default, pstree shows all processes. If a user name is specified, pstree displays all processes whose root has been spawned by the specified user.
The various options of the pstree command are
-h: Shows the process tree with running processes highlighted.
-p: Shows processes followed by PID
-u: Shows UIDs of processes when they are different from the UIDs of the parent.
In the output of the pstree, repeating processes under the same parent processes are combined and preceded by the number of instances of the child process.
The procinfo command displays system data and statistics about current processes.
The various options that can be used with procinfo are:
-f: Show output continuously, once in every n secs.
-n number:Show output once every n secs, specified by the value within number. This option is used in combination with the Ã¢â‚¬“f command.
The columns of the procinfo indicate:
** Mem: Shows the total, used and free memory, in bytes. The amount of shared, buffered and cached memory is also displayed.
** Swap: Shows the total, used and free swap memory, in bytes.
** Bootup: Shows the time the computer was booted.
** Load average: Shows the number of processes executing on the computer, on an average.
** User: Shows the time that computer has taken to execute processes in user space
** Nice: Shows the time that computer has taken to execute niced processes in user space
** System: Shows the time that computer has taken to execute processes in system space
** System: Shows the time that CPU has been idle
** Uptime: Shows the total time for which the CPU has been running.
** Context: Shows the number of context switches that processes and threads have executed
** Swap out: Shows the number of pages that have been swapped to the swap area
** Swap in: Shows the number of pages that have been swapped from the swap area into main memory
** IRQ: Shows the devices and the ports that they are connected to.
The top command displays information about processes that have the highest CPU priority. It provides a user interface that allows you to modify process statistics. By default, the top command runs continuously.
The various options for the top command are:
– d: Changes the time interval or the refresh rate of the top command output.
– q: Changes the time interval to 0 secs.
– i: Enters interactive mode
The interactive mode of the top command has various options:
– h: Offers help on the various interactive commands
– space: Refreshed the top command output
– k: Terminates a process
– q: Exit
– l: Hide/unhide information about CPU states and processes.