-4

You will be given a ID as a input to a process. We need to find if its a PID or TID This ID can be of any process not necessary of the same process. For example:

.\a.out 1234

Output: its a Thread ID.

I need to get the same info im my program. If I do

ps -elf | 1234 

it will list all entries with the given ID. The Entry with PID and TID are same is a process and if those are different then its a thread. I want to do the same through C program

Piyush
  • 33
  • 1
  • 1
  • 11
  • 1
    What have you tried? Where's your work? Have you read [How to Ask a Question](http://stackoverflow.com/questions/how-to-ask)? How about the [FAQ](http://stackoverflow.com/faq)? – John Szakmeister Oct 23 '12 at 09:58
  • You question is incorrect: learn about PID & TID here: http://stackoverflow.com/questions/4517301/difference-between-pid-and-tid – Grijesh Chauhan Oct 23 '12 at 09:59
  • I know that I can get PID and TID by using 'ps' command. I need to get the same info im my program. If I do ps -elf | 1234 it will list all entries with the given ID. The one with PID and TID are same is a process and if those are different then its a thread. I want to do the same through C program. Any idea how can I do? – Piyush Oct 23 '12 at 10:03
  • You might like to check out how they do it: http://procps.sourceforge.net/ – alk Oct 23 '12 at 13:03
  • As this can readily be done searching the /proc filesystem for a first level process, or second level thread entry (for processes belong to the same user, or by the superuser) it seems this is another case of a question being closed by people who simply do not understand the subject. – Chris Stratton Oct 23 '12 at 14:42
  • Specifically, for a process, /proc/pid will exist and have a directory entry (will not show up in ls /proc). For a thread, /proc/tid will exist but will not have a directory entry; it will have a directory entry in /proc/pid/task/tid. This of course concerns entities known to the kernel; some userspace threading implementations make additional virtual threads. – Chris Stratton Oct 23 '12 at 14:51
  • To try it from the command line, find /proc -name SOME_PID 2> /dev/null – Chris Stratton Oct 23 '12 at 21:05

2 Answers2

3

PID and TID both are number(identifiers). How would you differentiate? In single threaded code both PID & TID are same.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
0

The best you can do is to check against your current thread or process. Use getpid() to get current process id, if it is the same as the given pid, it is obviously a process id.

pthread_self() returns the current thread's id.

However, this would be of no use when it can belong to another process or thread.

This is for POSIX threads and fork process api

EDIT: @Chris suggested that proc file system can be explored for other process and thread id's. One might require privileged access though. Details are explained in the comments under the question

fkl
  • 5,412
  • 4
  • 28
  • 68
  • No, one is not limited to checking information about threads in the same process, though information about processes belonging to other users may not be accessable. – Chris Stratton Oct 23 '12 at 18:43
  • Your comment is vague and i don't fully understand it. All i said is since you know your own process id and thread id, if it is one of them, you can tell whether it was a pid or a thread id. What is wrong with that? When one counters an answer, you should give some better detail about your opinion some api ref, code etc. or point to some standard which better helps resolving the problem at hand. – fkl Oct 23 '12 at 19:54
  • You said that the best one can do is check against the current thread or process. That is nowhere near the best that can be done, as it is limited to checking parts of the current process while the question specifically states that the number may belong to any process. In contrast, using the /proc filesystem you can check at a minimum against the threads of any process belonging to the user, and possibly any belonging to any user. – Chris Stratton Oct 23 '12 at 21:00
  • Agreed but is it guaranteed? I believe it is not always possible to access pid and tid of others as you mentioned too. So isn't it guaranteed to be only able to check only your own? and every thing else on chance. Secondly, I am not sure if i can distinguish process ids vs tids while checking proc. Can you elaborate how to do that? – fkl Oct 24 '12 at 02:46