How can I list all threads within the current process in FreeBSD? Or at least, get the number of threads running.
I found the Linux system call pstat_getproc which returns a struct containing pst_nlwps, the number of threads. I am looking for something similar to this on FreeBSD.
Or perhaps there is something like /dev/fd but for threads.
Anything I can use to get some kind of idea about how many other threads are running.
I want to be able to do this programmatically in C, not using an existing application.
Use procstat(1), eg
# procstat -t $(pgrep openvpn)
PID TID COMM TDNAME CPU PRI STATE WCHAN
537 100051 openvpn - 0 120 sleep select
which depends on libprocstat(3).
Related
I'm wondering if there is a way to print the priority of the main. In this question I asked how to print the deafult priority of a thread; now I'm very curious to know if it's possible to do the same for the main.
EDIT: my goal is to get the priority of the unique process I created (I'm using pthread library to create threads inside the int main block). The process should not be a normal process, but a real time process, so i cannot use the getpriority function. It can be used only for normal processes (source: pag. 183, Robert Love - Linux system programming - Talking directly to the kernel and C library (2013, O'Reilly Media) 2nd Ed).
How can I get the priority of the real time process and print it?
to print the priority of the main
getpriority can be used to query the niceness level of a process.
shed_getparam can be used to query the scheduling priority of a process.
pthread_getschedparam can be used to query scheduling priority of a thread.
How can I get the priority of the real time process and print it?
A real time process is typically understood as a process running with SCHED_FIFO or SCHED_RR scheduling policy.
You can use the same functions as above.
so i cannot use the getpriority function. It can be used only for normal processes
You can use getpriority on any process. (I think niceness level is just ignored in case of real-time scheduling, and this could be meant).
I have a multi-threaded application in a POSIX/Linux environment - I have no control over the code that creates the pthreads. At some point the process - owner of the pthreads - receives a signal.
The handler of that signal should abort,cancel or stop all the pthreads and log how many pthreads where running.
My problem is that I could not find how to list all the pthreads running in process.
There doesn't seem to be any portable way to enumerate the threads in a process.
Linux has pthread_kill_other_threads_np, which looks like a leftover from the original purely-userland pthreads implementation that may or may not work as documented today. It doesn't tell you how many threads there were.
You can get a lot of information about your process by looking in /proc/self (or, for other processes, /proc/123). Although many unices have a file or directory with that name, the layout is completely different, so any code using /proc will be Linux-specific. The documentation of /proc is in Documentation/filesystems/proc.txt in the kernel source. In particular, /proc/self/task has a subdirectory for each thread. The name of the subdirectory is the LWP id; unfortunately, [1][2][3] there doesn't seem to be a way to associate LWP ids with pthread ids (but you can get your own thread id with gettid(2) if you work for it). Of course, reading /proc/self/task is not atomic; the number of threads is available atomically through /proc/self/status (but of course it might change before you act on it).
If you can't achieve what you want with the limited support you get from Linux pthreads, another tactic is to play dynamic linking tricks to provide your own version of pthread_create that logs to a data structure you can inspect afterwards.
You could wrap ps -eLF (or another command that more closely reads just the process you're interested in) and read the NLWP column to find out how many threads are running.
Given that the threads are in your process, they should be under your control. You can record all of them in a data structure and keep track.
However, doing this won't be race-condition free unless it's appropriately managed (or you only ever create and join threads from one thread).
Any threads created by libraries you use are their business and you should not be messing with them directory, or the library may break.
If you are planning to exit the process of course, you can just leave the threads running anyway, as calling exit() terminates them all.
Remember that a robust application should be crash-safe anyway, so you should not depend upon shutdown behaviour to avoid data loss etc.
I have a homework.
each application must wait for each of their activity.
I can write this scenario as pthread however, I must to write this scenario with different application.
I can write application as pthread with following code;
pthread_mutex_lock(&mutex2);
printf("I am in i");
pthread_mutex_unlock(&mutex2);
requested to me ; Users run application as following format;
./application 1
./application 2
./application 3
1,2,3 represented thread id value, and each application includes loops, each application in order to run code.
second application will not start until finish first loop of first application, and third application must wait end of first loop of first and second application.
how can i write this application ?
Thanks
The behavior you desire can be implemented by means of interprocess communication (IPC). It may be implemented, say, by means of shared memory with bool flag located in it. I don't know what OS you're going to write your app for, so can say nothing more specific at the moment.
Judging from "./" and pthread I suggest target OS is one of the *nix family, but you'd better clarify it. If it is the case, shmget and company may suit your needs.
And by the way, you wrote you're going to start app like this:
./application 1
./application 2
./application 3
which means they will run exactly one after another, otherwise you'd start them in the background:
./application 1&
If you do have to run them exactly like you described, then you'll have to fork and exit from parent in order to return control to shell and run your loop in the child process.
Again, this is relent for *nix systems with bash-like shell.
Basically I want to achieve something like this:
Process 1
Thread 1
Receive X from process 2
Thread 2
Receive Y from process 2
Process 2
Thread 1
Send X to process 1
Thread 2
Send Y to process 1
in MPI for C language with pthreads library.
I did it already in PVM, here's the source code:
master.c : http://pastebin.com/wwEie7gn ,
slave.c : http://pastebin.com/gfeCkcss .
What I tried to do:
prog.c : http://pastebin.com/tCVKN3fe
Somehow receiver threads don't receive anything. I don't know what the problem is. I hope that someone can show me the proper way to do it.
I'm running MPI compiled without thread support.
You would like to use different tags for both messages, e.g. you can tag the message with the (known?) ID of the receiver thread. Then each thread in process 1 would post a receive with its ID as tag and that receive would only match the message directed to that particular thread.
Please note that MPI 2.2 provides limited interoperability with threading. By default most MPI implementations are not thread-safe. Open MPI for example requires one to explicitly enable full threading support during configuration time (it is disabled by default). You need to at least have MPI_THREAD_SERIALIZED threading level as returned in the provided argument of MPI_Thread_init (or MPI_Query_thread) in order be able to make MPI calls in different threads. If your MPI library only provides MPI_THREAD_SINGLE or MPI_THREAD_FUNNELED levels, you are out of luck and cannot make MPI calls in other than the main thread. If the provided level is MPI_THREAD_SERIALIZED, then you can make MPI calls from any thread, but you have to explicitly serialise the calls, i.e. make sure that no two or more calls are made simulatenously (e.g. with critial sections or mutexes). If the provided level is MPI_THREAD_MULTIPLE then you have full multithreaded support and can make MPI calls from whatever thread and at whatever point in time.
I have a multi-threaded application in a POSIX/Linux environment - I have no control over the code that creates the pthreads. At some point the process - owner of the pthreads - receives a signal.
The handler of that signal should abort,cancel or stop all the pthreads and log how many pthreads where running.
My problem is that I could not find how to list all the pthreads running in process.
There doesn't seem to be any portable way to enumerate the threads in a process.
Linux has pthread_kill_other_threads_np, which looks like a leftover from the original purely-userland pthreads implementation that may or may not work as documented today. It doesn't tell you how many threads there were.
You can get a lot of information about your process by looking in /proc/self (or, for other processes, /proc/123). Although many unices have a file or directory with that name, the layout is completely different, so any code using /proc will be Linux-specific. The documentation of /proc is in Documentation/filesystems/proc.txt in the kernel source. In particular, /proc/self/task has a subdirectory for each thread. The name of the subdirectory is the LWP id; unfortunately, [1][2][3] there doesn't seem to be a way to associate LWP ids with pthread ids (but you can get your own thread id with gettid(2) if you work for it). Of course, reading /proc/self/task is not atomic; the number of threads is available atomically through /proc/self/status (but of course it might change before you act on it).
If you can't achieve what you want with the limited support you get from Linux pthreads, another tactic is to play dynamic linking tricks to provide your own version of pthread_create that logs to a data structure you can inspect afterwards.
You could wrap ps -eLF (or another command that more closely reads just the process you're interested in) and read the NLWP column to find out how many threads are running.
Given that the threads are in your process, they should be under your control. You can record all of them in a data structure and keep track.
However, doing this won't be race-condition free unless it's appropriately managed (or you only ever create and join threads from one thread).
Any threads created by libraries you use are their business and you should not be messing with them directory, or the library may break.
If you are planning to exit the process of course, you can just leave the threads running anyway, as calling exit() terminates them all.
Remember that a robust application should be crash-safe anyway, so you should not depend upon shutdown behaviour to avoid data loss etc.