ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am working on a project on redhat linux 9.0 using C++. I created several threads using pthread_create(). I know that each of the threads are running, as I can see that they are performing their tasks. Inside of the function for each thread i print the pid (using getpid()). On 9.0 each function reports the same pid as the parent. Also, in the /proc directory there is only one pid (the afore mentioned one) that contains the process with the cmdline name of my program. Using "top" I only see one process, even though I turn on the 'H' option o show threads.
when I coded for a RedHat 7.2 and Monta Vista versions of linux, each created thread received its own pid and each showed in the proc folder. Redhat 8.0 also seems to work this way. What's goig on? How can I tell what each thread is doing if I can't see it using a program like top? Is this some new way of handling threads in linux?
Well i think that process id is given to only processes ie if you create multiple processes using fork() then each new process will be assigned a new PID. However threads are part of the same process and hence do not have a separate PID.
At least this is what i think.. i dont know how you managed to get separate PIDs for thread in RedHat 7.2.
I found out what's up. Kernel 2.4.20 uses NPTL (Native posix thread library) and this is the kernel shipped with RH9. RH8 uses Kernel 2.4.18 which doesn't implement NPTL (Meaning each thread gets its own PID and therefore a good description of it's status in /proc). NPTL is a "real" implementation of POSIX threads meaning that the threads share alot more including the PID. It is more efficient way of running threads for a couple of reasons, however, I don't know of any easy tricks to debug these kind of threads. How do you know when your thread is sleeping versus waiting on a semaphore, or which threads have died in a process with lots of threads, etc.
According to my OS book ,a number of threads can be a part of a process and we can also run different threads under different processes.I have been programming in pthread for some time and I would like to tell that instead of getpid() use pthread_self().pthread_self returns the thread identifier for the calling
thread.
do like this
pthread_t tid = pthread_self();
printf( "%d",tid );
I can get the thread ID. Having that doesn't help me in identifying what thread is sleeping or waiting on a semaphore. As for the earlier question, do I know the difference between a thread and a process, the answer is yes. I have coded for both plain old linux (like redhat) and embedded linux (hard-hat/monta vista). In all cases, the threading model for Linux (up until kernel 2.4.20) issued a different pid for each thread. Go ahead and try it if you don't believe me. Use KPM or TOP to see that you can see the status of each thread, and if you print the pid in the thread, it will be different than the calling process or parent thread. This way, you can use the /proc directory to check status of each thread in a process. Some of the status will be meaningless as it will reflect the entire process (e.g. memory usage - so you can't just add up memory from each thread. Instead, the reported memory is the memory for the whole process shared by the thread and reported redundantly by each thread.) You will also notice on the older kernel that another thread is spawned; this is a manager thread needed for this model. In any event, getting a thread ID is nice, but what can you really do with it in the new kernel? Is there an API that I can call using this thread to find out what condition the thread is in?
This is a thread event. The additional argument has to be of type pth_t. One of PTH_UNTIL_TID_NEW, PTH_UNTIL_TID_READY, PTH_UNTIL_TID_WAITING or PTH_UNTIL_TID_DEAD has to be OR-ed into spec to specify on which state of the thread you want to wait. Example: `pth_event(PTH_EVENT_TID|PTH_UNTIL_TID_DEAD, tid)'.
PTH_ATTR_STATE (read-only) [pth_state_t]
PTH_ATTR_STATE (read-only) [pth_state_t]
The scheduling state of the thread, i.e., either PTH_STATE_NEW, PTH_STATE_READY, PTH_STATE_WAITING, or PTH_STATE_DEAD This can be queried only when the attribute object is bound to a thread.
Posix threads can either have scope process or system, if you want each thread to appear as a process and be scheduled as such you will need to call pthread_attr_setscope and pass a value of PTHREAD_SCOPE_SYSTEM. Sounds like the default for your Linux installation is PTHREAD_SCOPE_PROCESS.
there is no scope process on linux threads...
default is SYSTEM and this is to say that each thread competes with all the threads in the system for the resources.
solaris has this option of choosing if you want a bound thread or an unbound thread...by using PTHREAD_SCOPE_SYSTEM your thread is bound to an LWP ...the other option(PTHREAD_SCOPE_PROCESS) doesnt work on Linux...that is you can create just bound threads on linux..
as for all the threads having separate process ids: this is the linux threads implementation...creation of a thread is similar to forking a process...just that the clone() call includes various options such as enable to share the vm space etc..(dont remember all the flags..) ...do everytime you do a pthread_create...your thread gets a block in the task structure...
if you notice carefully, youll see that this thread doesnt have the forking thread as the ppid, but some other pid as its parent process id.......this is the thread manager process.......
I have the same problem.
In my program, I need get a thread's PID to do something(main process send a signal to child thread.), but I am always get the same PID number - getpid() returns the same value in all threads..
main process and child thread get the same PID when I used getpid() function in RedHat 9.0 (Kernel 2.4.20).
However, I can get different PID in Kernel 2.4.18.
The following is example from book of Advanced Linux Programming:
Code:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void* thread_function (void* arg) {
fprintf (stderr, "child thread pid is %d\n", (int) getpid ());
/* Spin forever. */
while (1);
return NULL;
}
int main () {
pthread_t thread;
fprintf (stderr, "main thread pid is %d\n", (int) getpid ());
pthread_create(&thread, NULL, &thread_function, NULL);
/* Spin forever. */
while (1);
return 0;
}
Running Reqult:
Code:
main thread pid is 2132
child thread pid is 2132
Use top to observe...
Code:
20:28:18 up 8 min, 3 users, load average: 1.21, 0.48, 0.22
65 processes: 60 sleeping, 5 running, 0 zombie, 0 stopped
CPU states: 100.0% user 0.0% system 0.0% nice 0.0% iowait 0.0% idle
Mem: 255300k av, 80156k used, 175144k free, 0k shrd, 8416k buff
66648k actv, 1232k in_d, 688k in_c
Swap: 265032k av, 0k used, 265032k free 45240k cached
PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME CPU COMMAND
2133 marty 25 0 792 792 684 R 50.0 0.3 0:27 0 pid
2132 marty 25 0 792 792 684 R 49.5 0.3 0:27 0 pid
you can do a pthread_self() inside the thread and store that in a global variable.
but i am surprised to see this program behave in this manner ..yo ushould have two different process ids for the main thread and the pthread_create'd thread..
what is the full kernel version and the release that you are using?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.