Solved

How to diplay the real thread ID

Posted on 2011-09-13
21
1,206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 7
21 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 36529022
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
ID: 36529072
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
ID: 36529116
I basically want to write the thread ID in to log file from the same statement.

Sham
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 36529263
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
ID: 36529299
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
ID: 36529334
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
ID: 36529376
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
ID: 36529599
Ok i got the solution
xxx is buf.lwp_lwpid
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 36529618
>> 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
ID: 36529647
I did not get you, when you say linux thread id

What do you think the fourth paramter should be?
Sham
0
 

Author Comment

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

0
 

Author Comment

by:mohet01
ID: 36529669
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
ID: 36529692
>> 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
ID: 36529906
hello infinity
What should I write in place of pthread-self()?
Sham
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 36530267
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
ID: 36530519
hello infinity
Am trying to display  thread Id which is shown in glance tool of HP

Sham
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 36530634
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
ID: 36531965
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
ID: 36534400
>> 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
ID: 36535027
fourth argument should be "pthread_self()-1"
0
 

Author Closing Comment

by:mohet01
ID: 36535030
thank you
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access_log 17 183
awk sed 8 135
Field name with special character (Ñ) in Oracle 11 187
SSH to Solaris server (which uses NIS) 1 49
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

734 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