Terry Li
Terry Li

Reputation: 17268

Linux process ID and thread ID

Suppose we have many user processes running on Linux. Each process has many threads running.

I can get process ID by calling getpid(), the return value of which is an integer.

I can get thread ID by calling pthread_self(), the return value of which is an opaque type called pthread_t.

Now I need to store the process ID (an int, typically 4 bytes) and the thread ID (pthread_t, need to figure out how many bytes) in shared memory so that I can later use the two pieces of ID information to identify that specific thread and to check if the thread is still running or not.

I've found many online sources cast pthread_t to either unsigned int or unsigned long. Since I don't want any data loss during the casting, how should I deal with the pthread_t data so that it's a fixed-size piece of data (as mentioned, I need to store the thread information in shared memory).

Also, how should I identify that specific thread by combination of process ID and thread ID later? How to check if the thread is still running or not?

Upvotes: 3

Views: 16775

Answers (4)

Vinayak S M
Vinayak S M

Reputation: 49

command to get thread ids running in a process

$ ps -eLf | grep 14965 
UID        PID              PPID     LWP           C  NLWP STIME TTY      TIME     CMD 
root       14965            14732    14965         0  201  15:28 pts/10   00:00:00 ./a.out 
root       14965            14732    14966         0  201  15:28 pts/10   00:00:00 ./a.out 
root       14965            14732    14967         0  201  15:28 pts/10   00:00:00 ./a.out 
root       14965            14732    14968         0  201  15:28 pts/10   00:00:00 ./a.out 
root       14965            14732    14969         0  201  15:28 pts/10   00:00:00 ./a.out 
root       14965            14732    14970         0  201  15:28 pts/10   00:00:00 ./a.out 
root       14965            14732    14971         0  201  15:28 pts/10   00:00:00 ./a.out 
root       14965            14732    14972         0  201  15:28 pts/10   00:00:00 ./a.out

Here the 4th column (LWP) shows all the threads running in process with ID 14965

Upvotes: 0

Chris Mansley
Chris Mansley

Reputation: 782

You can use pthread_join as a crude way of detecting completion, but I am sure that is not what you want. Instead you must handle this yourself by creating a thread complete flag. A nice way of setting this flag is in the pthread cleanup handlers. See this related post

Upvotes: 3

Matteo Italia
Matteo Italia

Reputation: 126787

Why don't you just pack them in a struct?

typedef struct
{
    int procID;
    pthread_t threadID;

} ProcThreadID;

without worrying about the specific underlying type of pthread_t (after all we are in C, so everything is POD and can be copied blindly with memcpy).

You can get its size easily using the sizeof operator:

size_t ptIDSize = sizeof(ProcThreadID);

and you can copy it wherever you want with a simple memcpy.

Upvotes: 2

Dan Fego
Dan Fego

Reputation: 14004

If you want to store pid_t and pthread_t anywhere, you should use their respective types (i.e. "pid_t" and "pthread_t"). So if you want to store them in shared memory somewhere, do a memcpy() to get them there.

As far as identifying specific threads by combinations of PID and TID, see Nemo's comment.

If you do make the assumption that they will exist, you can have your program look at /proc to find the appropriate pid's directory, and looking in /proc/<pid>/task for the threads.

Upvotes: 4

Related Questions