●The child is an (almost) identical copy of parent (same code, same data, etc.) ●The parent can either wait for the child to complete, or continue executing in parallel (concurrently) with the child. N In UNIX, a process creates a child process using the system call fork( ) ●In child process, fork( ) returns 0. Understanding Linux Process States Yogesh Babar 1 special access to files owned by other users (including root) or other daemon processes. Systems often start daemons at boot time and have them run continuously until the system is shut down.
Process State Transition
Applications that have strict real-time constraints might need to preventprocesses from being swapped or paged out to secondary memory. A simplifiedoverview of UNIX process states and the transitions between states is shownin the following figure.
Figure 3–2 Process State Transition Diagram
An active process is normally in one of the five states in the diagram.The arrows show how the process changes states.
A process is running if the process is assigned to a CPU.A process is removed from the running state by the scheduler if a processwith a higher priority becomes runnable. A process is also pre-empted if aprocess of equal priority is runnable when the original process consumes itsentire time slice.
A process is runnable in memory if the process is in primarymemory and ready to run, but is not assigned to a CPU.
A process is sleeping in memory if the process is in primarymemory but is waiting for a specific event before continuing execution. Forexample, a process sleeps while waiting for an I/O operation to complete,for a locked resource to be unlocked, or for a timer to expire. When the eventoccurs, a wakeup call is sent to the process. If the reason for its sleepis gone, the process becomes runnable.
When a process' address space has been written to secondarymemory, and that process is not waiting for a specific event, the processis runnable and swapped.
If a process is waiting for a specific event and has had itswhole address space written to secondary memory, the process is sleeping andswapped.
If a machine does not have enough primary memory to hold all its activeprocesses, that machine must page or swap some address space to secondarymemory.
When the system is short of primary memory, the system writesindividual pages of some processes to secondary memory but leaves those processesrunnable. When a running process, accesses those pages, the process sleepswhile the pages are read back into primary memory.
When the system encounters a more serious shortage of primarymemory, the system writes all the pages of some processes to secondary memory.The system marks the pages that have been written to secondary memory as swapped.Such processes can only be scheduled when the system scheduler daemon selectsthese processes to be read back into memory.
Both paging and swapping cause delay when a process is ready to runagain. For processes that have strict timing requirements, this delay canbe unacceptable.
To avoid swapping delays, real-time processes are never swapped, thoughparts of such processes can be paged. A program can prevent paging and swappingby locking its text and data into primary memory. For more information, seethe memcntl(2)man page. How much memory can be locked is limited by how much memory is configured.Also, locking too much can cause intolerable delays to processes that do nothave their text and data locked into memory.
Trade-offs between the performance of real-time processes and the performanceof other processes depend on local needs. On some systems, process lockingmight be required to guarantee the necessary real-time response.
Note –
See Dispatch Latency for information about latenciesin real-time applications.
ps (processes status) is a native Unix/Linux utility for viewing information concerning a selection of running processes on a system: it reads this information from the virtual files in /proc filesystem. It is one of the important utilities for system administration specifically under process monitoring, to help you understand whats is going on a Linux system.
It has numerous options for manipulating its output, however you’ll find a small number of them practically useful for daily usage.
Read Also: All You Need To Know About Processes in Linux [Comprehensive Guide]
In this article, we’ll look at 30 useful examples of ps commands for monitoring active running processes on a Linux system.
Note that ps produces output with a heading line, which represents the meaning of each column of information, you can find the meaning of all the labels in the ps man page.
List All Processes in Current Shell
1. If you run ps command without any arguments, it displays processes for the current shell.
Print All Processes in Different Formats
2. Display every active process on a Linux system in generic (Unix/Linux) format.
List Processes in Standard Format
3. Display all processes in BSD format.
4. To perform a full-format listing, add the -f or -F flag.
List Processes in Long List Format
Display User Running Processes
5. You can select all processes owned by you (runner of the ps command, root in this case), type:
6. To display a user’s processes by real user ID (RUID) or name, use the -U flag.
7. To select a user’s processes by effective user ID (EUID) or name, use the -u option.
Print All Processes Running as Root (Real and Effecitve ID)
8. The command below enables you to view every process running with root user privileges (real & effective ID) in user format.
Display Root User Running Processes
Display Group Processes
9. If you want to list all processes owned by a certain group (real group ID (RGID) or name), type.
10. To list all processes owned by effective group name (or session), type.
Display Processes by PID and PPID
11. You can list processes by PID as follows.
List Processes by PID
12. To select process by PPID, type.
13. Make selection using PID list.
List Processes by PIDs
Display Processes by TTY
14. To select processes by tty, use the -t flag as follows.
Print Process Tree
15. A process tree shows how processes on the system are linked to each other; processes whose parents have been killed are adopted by the init (or systemd).
List Process Tree
16. You can also print a process tree for a given process like this.
Print Process Threads
17. To print all threads of a process, use the -H flag, this will show the LWP (light weight process) as well as NLWP (number of light weight process) columns.
List Process Threads
Specify Custom Output Format
Using the -o or –format options, ps allows you to build user-defined output formats as shown below.
18. To list all format specifiers, include the L flag.
19. The command below allows you to view the PID, PPID, user name and command of a process.
20. Below is another example of a custom output format showing file system group, nice value, start time and elapsed time of a process.
List Process ID Information
21. To find a process name using its PID.
Display Parent and Child Processes
22. To select a specific process by its name, use the -C flag, this will also display all its child processes.
Find Parent Child Process
23. Find all PIDs of all instances of a process, useful when writing scripts that need to read PIDs from a std output or file.
24. Check execution time of a process.
The output below shows the HTTPD service has been running for 1 hours, 48 minutes and 17 seconds.
Find Process Uptime
Troubleshoot Linux System Performance
If your system isn’t working as it should be, for instance if it’s unusually slow, you can perform some system troubleshooting as follows.
26. Find top running processes by highest memory and CPU usage in Linux.
27. To kill an Linux processes/unresponsive applications or any process that is consuming high CPU time.
First, find the PID of the unresponsive process or application.
Then use the kill command to terminate it immediately.
Find and Kill a Process
Print Security Information
28. Show security context (specifically for SELinux) like this.
29. You can also display security information in user-defined format with this command.
List SELinux Context by Users
Perform Real-time Process Monitoring Using Watch Utility
30. Finally, since ps displays static information, you can employ the watch utility to perform real-time process monitoring with repetitive output, displayed after every second as in the command below (specify a custom ps command to achieve your objective).
Important: ps only shows static information, to view frequently updated output you can use tools such as htop; top and glances: the last two are in fact Linux system performance monitoring tool.
You might also like to read these following related articles.
That’s all for now. If you have any useful ps command example(s) to share (not forgetting to explain what it does), use the comment form below.