LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-17-2010, 01:11 AM   #1
sinu_nayak2001
Member
 
Registered: Oct 2009
Location: India
Distribution: Puppy Linux, Wary 530.
Posts: 31

Rep: Reputation: 20
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

Last edited by sinu_nayak2001; 05-17-2010 at 01:13 AM.
 
Old 05-17-2010, 01:21 AM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,275

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Try gettid (get thread ID) -- I think it's what you're looking for.
 
Old 05-17-2010, 05:14 AM   #3
sinu_nayak2001
Member
 
Registered: Oct 2009
Location: India
Distribution: Puppy Linux, Wary 530.
Posts: 31

Original Poster
Rep: Reputation: 20
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);
}
 
Old 05-17-2010, 04:19 PM   #4
fpmurphy
Member
 
Registered: Jan 2009
Location: /dev/ph
Distribution: Fedora, Ubuntu, Redhat, Centos
Posts: 297

Rep: Reputation: 62
Note SYS_gettid is actually merely a define to __NR_gettid.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
mmap function returns 0 instead of the address location btklee Linux - Kernel 0 01-09-2009 02:16 PM
c++ function that returns the user ID Hard_Working_ Programming 5 04-06-2007 07:26 PM
C function that returns $HOME cbranje Programming 1 02-12-2005 06:41 PM
open function returns error sudheernair Programming 2 05-07-2004 12:14 PM


All times are GMT -5. The time now is 08:59 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration