LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Which function returns LWP in Linux. (https://www.linuxquestions.org/questions/linux-newbie-8/which-function-returns-lwp-in-linux-808242/)

sinu_nayak2001 05-17-2010 12:11 AM

Which function returns LWP in Linux.
 
Hi,

For a process, we have different PIDs. For threads under a single processes, we have different LWP id, but same PID in Linux.
ex.

Code:

root@pc:/home/srinivas# ps -eLf
UID        PID  PPID    LWP  C STIME      TIME    CMD
root      100    1    100  0 May13      00:00:02 ./a.out
root      100    1    102  0 May13      00:00:00 ./a.out

getpid() function returns PID of a process.
Which function retuns LWP id for thread?

Sincerely,
Srinivas Nayak

btmiller 05-17-2010 12:21 AM

Try gettid (get thread ID) -- I think it's what you're looking for.

sinu_nayak2001 05-17-2010 04:14 AM

Code:

#include<stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include<pthread.h>

#define NUM_THREADS    5

void *PrintLWPid(void *threadid)
{
  long tid;
  tid = (long)threadid;

  printf("It's me, thread %ld and my LWP id=%ld!\n", tid, syscall(SYS_gettid));
  sleep(60);
  pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
  pthread_t threads[NUM_THREADS];
  int rc;
  long t;
  for(t=0; t<NUM_THREADS; t++){
      printf("In main: creating thread %ld\n", t);
      rc = pthread_create(&threads[t], NULL, PrintLWPid, (void *)t);
      if (rc){
        printf("ERROR; return code from pthread_create() is %d\n", rc);
        return -1;
      }
  }
  sleep(60);
  pthread_exit(NULL);
}


fpmurphy 05-17-2010 03:19 PM

Note SYS_gettid is actually merely a define to __NR_gettid.


All times are GMT -5. The time now is 05:23 AM.