List all threads linux

30 Useful ‘ps Command’ Examples for Linux Process Monitoring

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 the /proc filesystem. It is one of the important utilities for system administration specifically under process monitoring, to help you understand whats is going on in 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.

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 on the ps man page.

List All Processes in Current Shell

1. If you run the ps command without any arguments, it displays processes for the current shell.

List Current Running Processes

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.

List 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.

$ ps -fU tecmint OR $ ps -fu 1000

List User Processes by ID

7. To select a user’s processes by effective user ID (EUID) or name, use the -u option.

$ ps -fu tecmint OR $ ps -fu 1000

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.

$ ps -fG apache OR $ ps -fG 48

Display Group Processes

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.

List Process by PPID

13. Make a selection using a PID list.

List Processes by PIDs

Display Processes by TTY

14. To select processes by tty, use the -t flag as follows.

$ ps -t pts/0 $ ps -t pts/1 $ ps -ft tty1

List Processes by TTY

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).

Читайте также:  Linux remove file with spaces

List Process Tree

16. You can also print a process tree for a given process like this.

$ ps -f --forest -C sshd OR $ ps -ef --forest | grep -v grep | grep sshd

List Tree View of Process

17. To print all threads of a process, use the -L flag, this will show the LWP (lightweight process) as well as NLWP (number of the lightweight processes) 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.

List Processes with Names

20. Below is another example of a custom output format showing file system group, nice value, start time, and elapsed time of a process.

$ ps -p 1154 -o pid,ppid,fgroup,ni,lstart,etime

List Process ID Information

Find Process using 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 an std output or file.

Find All Process PIDs

24. Check the execution time of a process.

$ ps -eo comm,etime,user | grep httpd

The output below shows the HTTPD service has been running for 1 hour, 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.

$ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head OR $ ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head

Find Top Running Processes

27. To kill 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

28. Show security context (specifically for SELinux) like this.

Find SELinux Context

29. You can also display security information in a user-defined format with this command.

$ ps -eo euser,ruser,suser,fuser,f,comm,label

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).

$ watch -n 1 'ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head'

Real Time Process Monitoring

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 tools.

You might also like to read the 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.

Источник

On Linux, in C, how can I get all threads of a process?

How to iterate through all tids of all threads of the current process? Is there some way that doesn’t involve diving into /proc ?

Do you have access to a pthread_t handle? Can you hook into those threads post-creation and call pthread_self from them?

Also. one possible approach would be to set the affinity of the thread that creates those threads since CPU affinity mask is inherited.

Читайте также:  Linux чем посмотреть smart

1 Answer 1

The code I am using, based on reading /proc

#include #include #include #include

Then, from inside a funcion:

 DIR *proc_dir; < char dirname[100]; snprintf(dirname, sizeof dirname, "/proc/%d/task", getpid()); proc_dir = opendir(dirname); >if (proc_dir) < /* /proc available, iterate through tasks. */ struct dirent *entry; while ((entry = readdir(proc_dir)) != NULL) < if(entry->d_name[0] == '.') continue; int tid = atoi(entry->d_name); /* . (do stuff with tid) . */ > closedir(proc_dir); > else < /* /proc not available, act accordingly */ >

FYI, snprintf(dirname, sizeof dirname, «/proc/%d/task», getpid()); can be replaced with «/proc/self/task» for current process

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.12.43529

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to view threads of a process on Linux

Question: My program creates and executes multiple threads in it. How can I monitor individual threads of the program once they are created? I would like to see the details (e.g., CPU/memory usage) of individual threads with their names.

Threads are a popular programming abstraction for parallel execution on modern operating systems. When threads are forked inside a program for multiple flows of execution, these threads share certain resources (e.g., memory address space, open files) among themselves to minimize forking overhead and avoid expensive IPC (inter-process communication) channel. These properties make threads an efficient mechanism for concurrent execution.

In Linux, threads (also called Lightweight Processes (LWP)) created within a program will have the same «thread group ID» as the program’s PID. Each thread will then have its own thread ID (TID). To the Linux kernel’s scheduler, threads are nothing more than standard processes which happen to share certain resources. Classic command-line tools such as ps or top , which display process-level information by default, can be instructed to display thread-level information.

Here are several ways to show threads for a process on Linux. If you want to simply count the number of threads in a thread, check out this post instead.

Method One: ps

In ps command, -T option enables thread views. The following command list all threads created by a process with .

The SID column represents thread IDs, and CMD column shows thread names.

Method Two: top

The top command can show a real-time view of individual threads. To enable thread views in the top output, invoke top with -H option. This will list all Linux threads. You can also toggle on or off thread view mode while top is running, by pressing key.

To restrict the top output to a particular process and check all threads running inside the process:

Method Three: htop

A more user-friendly way to view threads per process is via htop , an ncurses -based interactive process viewer. This program allows you to monitor individual threads in tree views.

To enable thread views in htop , launch htop , and press to enter htop setup menu. Choose Display option under Setup column, and toggle on Three view and Show custom thread names options. Presss to exit the setup.

Now you will see the follow threaded view of individual processes.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Читайте также:  Linux пропала кнопка свернуть все

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Источник

Show Threads Using PS Command in Linux

In the modern operating systems, threads serve as a popular programming abstraction. These threads share resources like open files, space, and memory addresses when this happens. It avoids the inter-process communication (expensive IPC) channels and reduces forking overhead. As a result, threads perform a concurrent execution mechanism.

In Linux, threads are also known as LWP or Lightweight Processes. These threads created within the program have a “thread group ID”, the same as the program’s PID. There is an individual thread ID (TID) assigned to each thread. Scheduler threads in the Linux kernel share a few resources, which are standard processes.

Besides displaying the process-level information by default, classic command-line tools, such as top and ps, can display the thread-level information as well. Ps, top, and htop are classic commands that show you the process-level information by default. You can also view the thread-level information using the previous commands. This guide will only go through the ps command and explain how the ps command shows threads in Linux.

Show Threads Using PS Command in Linux

With the help of the ps command, you can view the threads with many filters like PID (Process ID), application name, etc. This command does not work on BSD or macOS, as there is no option to show that the thread and the -t parameter have a different meaning.

Let’s start with the following command in the terminal to list all the threads using the ps command:

Show Threads with PID

Here is the standard syntax of ps commands to view the threads using PIDs:

You can view the threads for specific PIDs. Follow the previous standard syntax and put the PID number which you want to know the threads.

For example, we use the PID . The syntax is as follows:

The “SPID” column shows the thread IDs in the previous output. And the “CMD” column represents the thread names.

Show Threads with Application Name

Here is the standard syntax of the ps command to view the threads using the application name:

Option Description
-T List all threads
-C Specifies the application name

For example, let’s find out the thread related to the Bash application. Here is the basic command:

Show Threads with Filter

The standard syntax of the ps command is to view the threads using the filter as follows:

Option Description
-T List all threads
-e Shows all processes
| Pipes the output to the next command
grep It filters the content with the help of

You can filter the content through the application name. Type and execute the following command to accomplish it:

You can also filter the threads using their PIDs:

In the previous output, you may notice that all the threads have the same PID. It means that all the previous threads are in the same process.

To find the sum of all the running threads in the system, you can execute the following command:

You can use the “pgrep” or “pidof” to get the process name’s process id:

Источник

Оцените статью
Adblock
detector