LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-12-2003, 06:10 PM   #1
robnob
LQ Newbie
 
Registered: Jun 2003
Location: Seattle
Posts: 3

Rep: Reputation: 0
pthread problem


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?
 
Old 06-15-2003, 06:24 AM   #2
rch
Member
 
Registered: Feb 2003
Location: Santa Clara,CA
Distribution: Mandriva
Posts: 909

Rep: Reputation: 48
Can you give me the code snippet.
Next do you know the difference between process and thread?
 
Old 06-16-2003, 02:35 AM   #3
yrraja
Member
 
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Rep: Reputation: 15
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 would request gurus to shed some light on this
 
Old 06-16-2003, 11:21 AM   #4
robnob
LQ Newbie
 
Registered: Jun 2003
Location: Seattle
Posts: 3

Original Poster
Rep: Reputation: 0
Solution

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.

Thanks for your responses!
 
Old 06-16-2003, 10:01 PM   #5
rch
Member
 
Registered: Feb 2003
Location: Santa Clara,CA
Distribution: Mandriva
Posts: 909

Rep: Reputation: 48
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 );

Last edited by rch; 06-16-2003 at 10:09 PM.
 
Old 06-17-2003, 12:19 PM   #6
robnob
LQ Newbie
 
Registered: Jun 2003
Location: Seattle
Posts: 3

Original Poster
Rep: Reputation: 0
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?
 
Old 06-18-2003, 10:05 PM   #7
rch
Member
 
Registered: Feb 2003
Location: Santa Clara,CA
Distribution: Mandriva
Posts: 909

Rep: Reputation: 48
I think that you are looking for something like the
GNU portable threads.
http://gnu.linuxforum.net/software/pth/pth.html
Read also
http://gnu.linuxforum.net/software/pth/pth-manual.html
Quote:
PTH_EVENT_TID

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.

Last edited by rch; 06-18-2003 at 10:08 PM.
 
Old 08-06-2003, 04:30 PM   #8
paulwk
LQ Newbie
 
Registered: Aug 2003
Posts: 1

Rep: Reputation: 0
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.
 
Old 08-07-2003, 06:12 AM   #9
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
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.......

Last edited by shishir; 08-07-2003 at 06:14 AM.
 
Old 10-08-2003, 12:16 PM   #10
cs87668
LQ Newbie
 
Registered: Jun 2003
Posts: 6

Rep: Reputation: 0
dear all,

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
is any way that can slove this problem?

thanks~

Last edited by cs87668; 10-08-2003 at 12:41 PM.
 
Old 10-08-2003, 01:24 PM   #11
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
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?
 
Old 10-09-2003, 12:13 AM   #12
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
Check this page-

http://www.redhat.com/docs/manuals/l...s-x86-en-9.pdf

page 7, para 1
 
Old 10-09-2003, 12:13 AM   #13
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15

Last edited by Kumar; 10-09-2003 at 12:15 AM.
 
Old 10-09-2003, 12:34 AM   #14
cs87668
LQ Newbie
 
Registered: Jun 2003
Posts: 6

Rep: Reputation: 0
thanks for all replied,

My kernel version is 2.4.20-20.9

and the solution is setting an environment variable "setenv LD_ASSUME_KERNEL 2.4.1" in tcsh

and I found a way to send signal to another thread - int pthread_kill(pthread_t thread, int sig);

 
Old 11-11-2003, 06:17 AM   #15
yaumeileng
LQ Newbie
 
Registered: Sep 2003
Location: Malaysia
Posts: 4

Rep: Reputation: 0
Unhappy PID in RH9.0

Yap, I am facing the same problem as well. By the way cs87668 has solved this problem by setting the setenv in tcsh.

I am having problem in setting this. Can you please further elaborate this?Set it in which file? Why do you do it this way?

Thanx: meileng
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Possible pthread permission problem Zotty Programming 1 06-05-2005 02:50 PM
pthread problem on redhat9 wizardjack Programming 4 02-02-2005 08:54 AM
pthread signal problem again xinwang00 Programming 4 11-18-2003 02:21 PM
pthread signal problem xinwang00 Programming 12 11-11-2003 02:30 PM
pthread signal problem xinwang00 Linux - Software 2 11-07-2003 12:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration