Solved

How to diplay the real thread ID

Posted on 2011-09-13
21
1,140 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:mohet01
  • 13
  • 7
21 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
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
 

Author Comment

by:mohet01
Comment Utility
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
 

Author Comment

by:mohet01
Comment Utility
I basically want to write the thread ID in to log file from the same statement.

Sham
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
 

Author Comment

by:mohet01
Comment Utility
Hello infinity
If i go thru my hp-ux machine
i cannot see syscall() function declaration in /usr/include/sys/syscall.h

Sham
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
 

Author Comment

by:mohet01
Comment Utility
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
 

Author Comment

by:mohet01
Comment Utility
Ok i got the solution
xxx is buf.lwp_lwpid
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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
 

Author Comment

by:mohet01
Comment Utility
I did not get you, when you say linux thread id

What do you think the fourth paramter should be?
Sham
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:mohet01
Comment Utility
Fourth paramter "index"  specifies the starting index within the context of LWPs
in a process.

0
 

Author Comment

by:mohet01
Comment Utility
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
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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
 

Author Comment

by:mohet01
Comment Utility
hello infinity
What should I write in place of pthread-self()?
Sham
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
 

Author Comment

by:mohet01
Comment Utility
hello infinity
Am trying to display  thread Id which is shown in glance tool of HP

Sham
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
 

Author Comment

by:mohet01
Comment Utility
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
Comment Utility
>> 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
 

Author Comment

by:mohet01
Comment Utility
fourth argument should be "pthread_self()-1"
0
 

Author Closing Comment

by:mohet01
Comment Utility
thank you
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now