How to track the threads created for a process in linux?
On Terminal if you know the process-id you can run the command
"ps -e -T | grep<pid-no>" It will show all the threads for the process(pid-n0)
Or you can write a sample program
pthread_t ntid;
void printids(const char *s)
{
pid_t pid;
pthread_t tid;
pid=getpid();
tid=pthread_self();
printf("%s pid %u tid %u(0x%x)\n",s,(unsigned int)pid, (unsigned int)tid, (unsigned
int)tid);
}
void *thr_fn(void *arg)
{
printids("new thread");
return ((void *)0);
}
int main(int argc, char *argv[])
{
int err;
err=pthread_create(&ntid,NULL,thr_fn,NULL);
if(err!=0)
cout<<"can't create thread "<<strerror(err);
printids("main thread");
sleep(1);
exit(0);
}
How to get number of processes and threads in a C program?
The simplest is to parse the output of commands using popen.
The following:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void parse_output(char *buf, size_t bufsize, const char cmd[])
{
assert(buf != NULL);
assert(cmd != NULL);
FILE *fp;
// add dynamic allocation here
memset(buf, 0, bufsize);
if ((fp = popen(cmd, "r")) == NULL) {
printf("Error opening pipe!\n");
exit(-__LINE__);
}
// worst speed ever. And strlen is called twice...
while (fgets(&buf[strlen(buf)], bufsize - strlen(buf), fp) != NULL);
if(pclose(fp)) {
printf("Command not found or exited with error status\n");
exit(-__LINE__);
}
}
int main() {
char buf[256];
long num;
parse_output(buf, sizeof(buf), "ps -A --no-headers | wc -l");
if (sscanf(buf, "%ld", &num) != 1) {
exit(-__LINE__);
}
printf("Number of processes: %ld\n", num);
parse_output(buf, sizeof(buf), "ps -AL --no-headers | wc -l");
if (sscanf(buf, "%ld", &num) != 1) {
exit(-__LINE__);
}
printf("Number of processes including tasks: %ld\n", num);
}
will output on my system:
$ gcc 1.c && ./a.out
Number of processes: 241
Number of processes includeing tasks: 867
Linux Performance Monitoring, any way to monitor per-thread?
perf is a system profiling tool you can use. it's not like https://github.com/castl/easyperf), which is a library and you use it in your code. Following the steps and use it to profile your program:
Install
perf
on Ubuntu. The installation could be quite different in different Linux distribution. You can find out the installation tutorial line.Simply run your program and get all thread id of your program:
ps -eLf | grep [application name]
open separate terminal and run perf as
perf stat -t [threadid]
according to man page:usage: perf stat [<options>] [<command>]
-e, --event <event> event selector. use 'perf list' to list available events
-i, --no-inherit child tasks do not inherit counters
-p, --pid <n> stat events on existing process id
-t, --tid <n> stat events on existing thread id
-a, --all-cpus system-wide collection from all CPUs
-c, --scale scale/normalize counters
-v, --verbose be more verbose (show counter open errors, etc)
-r, --repeat <n> repeat command and print average + stddev (max: 100)
-n, --null null run - dont start any counters
-B, --big-num print large numbers with thousands' separators
there is an analysis article about perf
, you can get a feeling about it.
Maximum number of threads per process in Linux?
Linux doesn't have a separate threads per process limit, just a limit on the total number of processes on the system (threads are essentially just processes with a shared address space on Linux) which you can view like this:
cat /proc/sys/kernel/threads-max
The default is the number of memory pages/4. You can increase this like:
echo 100000 > /proc/sys/kernel/threads-max
There is also a limit on the number of processes (and hence threads) that a single user may create, see ulimit/getrlimit
for details regarding these limits.
FreeBSD: How to check how many threads are running in a process by PID?
You want:
procstat -t <pid>
This answer is short, but I'm typing more to get the answer up to the minimum length.
Related Topics
Find All Files with Name Containing String
Extract Lines Between Two Patterns from a File
How to Automatically Add User Account and Password with a Bash Script
Meaning of Tilde in Linux Bash (Not Home Directory)
How to Program Linux .Dts Device Tree Files
How to Establish Ssl Connection Upon Wget on Ubuntu 14.04 Lts
Bash Script to Remove 'X' Amount of Characters the End of Multiple Filenames in a Directory
How to Encrypt a Large File in Openssl Using Public Key
Merge PDF's with PDFtk with Bookmarks
How to Grep for the Dollar Symbol ($)
What Is the Correct Way to Start a Mongod Service on Linux/Os X
Prevent File Descriptors Inheritance During Linux Fork
Docker Command Can't Connect to Docker Daemon
How to Get the Resolution (Width and Height) for a Video File from a Linux Command Line
What Does "Ulimit -S Unlimited" Do
Receiving Key Press and Key Release Events in Linux Terminal Applications
Why Doesn't "Sort File1 > File1" Work
Setting Default Permissions for Newly Created Files and Sub-Directories Under a Directory in Linux