LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-20-2010, 12:11 AM   #1
jayadhanesh
Member
 
Registered: Feb 2009
Location: Bangalore
Posts: 61

Rep: Reputation: 15
Thread PID


I have a multithreaded program running. The threads are created by using
pthread_create(). The ps shows a pid value for each thread in the process.
The name of the thread is all the same as the parent process. I want to
match the PID values in the ps output to the threads in my program. But
when I used getpid() in each thread, the value I get is different from what
ps shows. How do I match the pid value in ps output to the threads in my
program.

Thanks,
Dhanesh.
 
Old 07-20-2010, 04:12 AM   #2
pfelecan
LQ Newbie
 
Registered: Jun 2010
Posts: 9

Rep: Reputation: 1
It depends on the threads implementation that you use.

Read pthread_create(3) and pthreads(7). Excerpts:

[...]

In the obsolete LinuxThreads implementation, each of the threads in a
process has a different process ID. This is in violation of the POSIX
threads specification, and is the source of many other nonconformances
to the standard; see pthreads(7).

[...]

LinuxThreads: This is the original Pthreads implementation. Since
glibc 2.4, this implementation is no longer supported.

[...]

The LinuxThreads implementation deviates from the POSIX.1
specification in a number of ways, including the following:

- Calls to getpid(2) return a different value in each thread.

Here is an example showing the concept (built and tun on GNU/Linux
Debian testing)

#include <pthread.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>

#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)

struct thread_info { /* Used as argument to thread_start() */
pthread_t thread_id; /* ID returned by pthread_create() */
int thread_num; /* Application-defined thread # */
char *argv_string; /* From command-line argument */
};

static int sleep_time = 0;

/* Thread start function: display the thread id and the process id */

static void *
thread_start(void *arg)
{
struct thread_info *tinfo = (struct thread_info *) arg;

printf("thread %lu pid %d\n", tinfo->thread_id, getpid());

if(sleep_time)
{
sleep(sleep_time);
}

return((void*)0);
}

int
main(int argc, char *argv[])
{
int s, tnum, opt, num_threads;
struct thread_info *tinfo;
pthread_attr_t attr;
void *res;

/* The "-s" option specifies a sleep time before the threads destruction */

while ((opt = getopt(argc, argv, "s:")) != -1) {
switch (opt) {
case 's':
sleep_time = strtoul(optarg, NULL, 0);
break;

default:
fprintf(stderr, "Usage: %s [-s sleep_time] arg...\n",
argv[0]);
exit(EXIT_FAILURE);
}
}

num_threads = argc - optind;

/* Initialize thread creation attributes */

s = pthread_attr_init(&attr);
if (s != 0)
handle_error_en(s, "pthread_attr_init");

/* Allocate memory for pthread_create() arguments */

tinfo = calloc(num_threads, sizeof(struct thread_info));
if (tinfo == NULL)
handle_error("calloc");

/* Create one thread for each command-line argument */

for (tnum = 0; tnum < num_threads; tnum++) {
tinfo[tnum].thread_num = tnum + 1;
tinfo[tnum].argv_string = argv[optind + tnum];

/* The pthread_create() call stores the thread ID into
corresponding element of tinfo[] */

s = pthread_create(&tinfo[tnum].thread_id, &attr,
&thread_start, &tinfo[tnum]);
if (s != 0)
handle_error_en(s, "pthread_create");
}

/* Destroy the thread attributes object, since it is no
longer needed */

s = pthread_attr_destroy(&attr);
if (s != 0)
handle_error_en(s, "pthread_attr_destroy");

/* Now join with each thread */

for (tnum = 0; tnum < num_threads; tnum++) {
s = pthread_join(tinfo[tnum].thread_id, &res);
if (s != 0)
handle_error_en(s, "pthread_join");

free(res); /* Free memory allocated by thread */
}

free(tinfo);
exit(EXIT_SUCCESS);
}

Build as follows:

gcc -Wall thpid.c -o thpid -lpthread

Run as follows:

./thpid -s 60 a b c

The output is:

thread 3060976496 pid 8614
thread 3069369200 pid 8614
thread 3077761904 pid 8614

You have one minute to observe with:

ps -e -f -L | grep thpid
peter 8614 2211 8614 0 4 10:36 pts/5 00:00:00 ./thpid -s 60 a b c
peter 8614 2211 8615 0 4 10:36 pts/5 00:00:00 ./thpid -s 60 a b c
peter 8614 2211 8616 0 4 10:36 pts/5 00:00:00 ./thpid -s 60 a b c
peter 8614 2211 8617 0 4 10:36 pts/5 00:00:00 ./thpid -s 60 a b c
 
  


Reply



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
Get the username , memoery size and thread count for a given PID haseit Linux - Newbie 2 03-11-2009 10:12 AM
thread ID: how reliable is current->pid ? andersonfobr Programming 4 02-01-2006 01:31 AM
obtain pid of a kernel thread ratwings Linux - Software 0 02-11-2005 03:54 PM
Kernel Thread Execution Context and PID ratwings Linux - Newbie 0 02-11-2005 04:06 AM
/var/run/[XXX].pid - Tcl pid code liguorir Linux - Software 1 05-20-2004 10:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:27 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
Open Source Consulting | Domain Registration