How to diplay the real thread ID

Hello
In a threaded code application, we are using the following code to display the thread id.
sprintf(temp, "CCISTUB : PID[0x%x] TID[0x%x]  ",getpid(), pthread_self() );


But if i check thread ID using glance tool in runtime for the same application
thread ID does not match with the application log.

What should be used in place of pthread_self() to match the thread ID shown by glance tool on hp machine?

Sham

mohet01Asked:
Who is Participating?
 
Infinity08Commented:
>> Am basically trying to display all lwpid(buf.lwp_lwpid) under a pid.

Ah, if you want all of them, then you can do it like this eg. :
struct lwp_status buf[128];
count = pstat_getlwp(&buf[0], sizeof(lwp_status), 128, 0, getpid());

Open in new window

0
 
woolmilkporcCommented:
The pthread_self subroutine returns the calling thread's ID.

If this is not what you desire rather use the value set by pthread_create():

pthread_t *thread;
int pthread_create (thread, attr, start_routine (void *), arg)

thread  points to where the thread ID will be stored.

wmp
0
 
mohet01Author Commented:
The scenario where we are using this thread code  to write the pid and it's own tid into log
pthread_create() would have been done somewhere else

Sham
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
mohet01Author Commented:
I basically want to write the thread ID in to log file from the same statement.

Sham
0
 
Infinity08Commented:
On a modern Linux system (with the NPTL pthreads implementation) :

getpid returns the process id (sometimes called thread group id)
pthread_self returns the pthread thread id
gettid returns the linux thread id

So, it seems that what you want is the latter (gettid). Unfortunately, it's only available as a system call, and there's no user level function for it. So you'll have to do something like :
#include <sys/types.h>
#include <sys/syscall.h>

pid_t gettid(void) {
    return syscall(__NR_gettid);
}

Open in new window

0
 
mohet01Author Commented:
Hello infinity
If i go thru my hp-ux machine
i cannot see syscall() function declaration in /usr/include/sys/syscall.h

Sham
0
 
Infinity08Commented:
Ah, sorry. I missed that you needed this for HP-UX. Unfortunately I have no experience with that platform. So, I don't know whether the gettid system call is available, and if so, how to call it. You might want to check your system documentation for that (or check the syscall.h header file to see if there's a reference to gettid, and if there are any other functions that allow performing system calls).
0
 
mohet01Author Commented:
ya i know, you do very less mistakes, i think this is the first one as per my experience.

I personally trying to write the code like this below, But am unable to know the structure of "struct lwp_status"

/* Add this two line on top of the file ccistub.c */
#include <sys/param.h>
#include <sys/pstat.h>
/* Add this two line on top of the file ccistub.c*/

In a function we write the below:
========================================================
 struct lwp_status *buf;
   int count =0;
   count =pstat_getlwp(buf,sizeof(lwp_status),0,pthread_self(), getpid());
   if(count ==-1)
         //error;
   else
         sprintf(temp, "CCISTUB : PID[0x%x] TID[0x%x] ",
            getpid(), xxx);

===============================================================
In place of xxx, i need to provide TID by reading buf? Because buf will have thread ID that matches with glance tool thread ID,
How do i do this?

Sham
0
 
mohet01Author Commented:
Ok i got the solution
xxx is buf.lwp_lwpid
0
 
Infinity08Commented:
>> count =pstat_getlwp(buf,sizeof(lwp_status),0,pthread_self(), getpid());

Are you sure that pstat_getlwp takes the pthread thread id as 4th parameter ? I'd expect it to want the linux thread id.
0
 
mohet01Author Commented:
I did not get you, when you say linux thread id

What do you think the fourth paramter should be?
Sham
0
 
mohet01Author Commented:
Fourth paramter "index"  specifies the starting index within the context of LWPs
in a process.

0
 
mohet01Author Commented:
As per the link:
http://nixdoc.net/man-pages/HP-UX/man2/pstat_getlwp.2.html

As a shortcut, information about a single LWP can beobtained by setting elemcount to zero and setting index to the TID (Thread ID) of that LWP within its process.


0
 
Infinity08Commented:
>> and setting index to the TID (Thread ID) of that LWP within its process.

Indeed. And I'd expect that to be the linux thread id (ie. what's returned by gettid), and not the pthread thread id (ie. what's returned by pthread_self).
0
 
mohet01Author Commented:
hello infinity
What should I write in place of pthread-self()?
Sham
0
 
Infinity08Commented:
gettid() ;) But that's exactly what you're trying to find out, so it doesn't help hehe. Point being that I don't think the pstat_getlwp approach you use is going to help you find the linux thread id.

The best way is probably to figure out whether HP-UX provides a gettid system call or not.
0
 
mohet01Author Commented:
hello infinity
Am trying to display  thread Id which is shown in glance tool of HP

Sham
0
 
Infinity08Commented:
If it's not the pid, and not the pthread thread id, then it's very likely that it's the linux thread id (as I mentioned earlier). So, your best chance is to get the gettid system call working (if it's available).
0
 
mohet01Author Commented:
Hello infinity
I think what you said is correct.
If i write the below code, pstat_getlwp() is always returning -1
======================================
   struct lwp_status buf;
   int count =0;

   ….

   count =pstat_getlwp(&buf,sizeof(lwp_status),0,pthread_self(), getpid());
   if (count == -1)
   {
        buf.lwp_lwpid = -9999;
   }
   sprintf(temp, "app :   PID[%d] TID[%d]  ",  getpid(), buf.lwp_lwpid);
====================================================
           

I feel pstat_getlwp() is failing because of fourth argument.
If yes, What do you think fourth argument should be?
Am basically trying to display all lwpid(buf.lwp_lwpid) under a pid.
Please help me

Sham
0
 
mohet01Author Commented:
fourth argument should be "pthread_self()-1"
0
 
mohet01Author Commented:
thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.