LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 07-07-2007, 07:34 AM   #1
piyush.kansal
LQ Newbie
 
Registered: May 2007
Location: Pune, India
Distribution: Fedora Core
Posts: 29

Rep: Reputation: 15
Are threads allocated different PIDs then its parent's PID?


Hi,
I wrote a simple program just to see if a thread has a different PID then its parents. Here it goes,

#include <pthread.>
#include <stdlib.h>

void *threadFunc(void *);

int main()
{
pthread_t thread;
int returnCode;

returnCode = pthread_create(&thread, NULL, threadFunc);
if(0 == returnCode)
{
perror("Thread creation failed\n");
exit(EXIT_FAILURE);
}

while(1)
{
printf("I am Parent. My PID is :: %d and PPID is :: %d\n", getpid(), getppid());
sleep(1);
}

exit(EXIT_SUCCESS);
}


void * threadFunc(void *arg)
{
while(1)
{
printf("I am Child. My PID is :: %d and PPID is :: %d\n", getpid(), getppid());
sleep(1);
}
}

After that i complied this program and executed it. This showed some o/p as:
./prog
I am Child. My PID is :: 3220 and PPID is :: 3210
I am Parent. My PID is :: 3220 and PPID is :: 3210
I am Child. My PID is :: 3220 and PPID is :: 3210
I am Parent. My PID is :: 3220 and PPID is :: 3210
I am Child. My PID is :: 3220 and PPID is :: 3210
I am Parent. My PID is :: 3220 and PPID is :: 3210
.
.
.
When i did ps -ef to see what's going on in the process table, i was able to find one entry in it:

PID PPID PROGRAM
3220 3210 prog

Well this is quite obvious to me. But whenever i do ps -efm(to see all threads also) then i am seeing that a PID has been allocated to a thread also and thats the thing which is confusing me:

PID PPID PROGRAM
3220 3210 prog
3221 3220 prog

Can anyone please tell me that why a separate PID has been allocated to a thread? Threads are allocated thread identifiers not separate PIDs.
 
Old 07-07-2007, 08:48 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
I wanted to try your program out myself and explore some stuff, but it wouldn't compile, even after I fixed the #include <pthread.>. I could guess how to fix it, but that's your job.

Please post the program you actually ran. (grin)

And when you do, please put it between CODE markers. The best way to do that is:
  1. Click the "Go Advanced" button at the bottom of your editing window.
  2. Highlight your code with the mouse.
  3. Click the # icon at the top of the new editing window.
 
Old 07-07-2007, 11:45 AM   #3
piyush.kansal
LQ Newbie
 
Registered: May 2007
Location: Pune, India
Distribution: Fedora Core
Posts: 29

Original Poster
Rep: Reputation: 15
Smile

Oh, I am sorry for the mistake. Here is the actual program:

Code:
#include <pthread.h>
#include <stdlib.h>

void *threadFunc(void *);

int main()
{
        pthread_t thread;
        int returnCode;

        returnCode = pthread_create(&thread, NULL, threadFunc, NULL);
        if(0 != returnCode)
        {
                perror("Thread creation failed\n");
                exit(EXIT_FAILURE);
        }

        while(1)
        {
                printf("I am Parent. My PID is :: %d and PPID is :: %d\n", getpid(), getppid());
                sleep(1);
        }

        exit(EXIT_SUCCESS);
}

void * threadFunc(void *arg)
{
        while(1)
        {
                printf("I am Child. My PID is :: %d and PPID is :: %d\n", getpid(), getppid());
                sleep(1);
        }
}

Last edited by piyush.kansal; 07-07-2007 at 11:48 AM.
 
Old 07-07-2007, 11:19 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Running your program on my system (FC3) did not exhibit the same behavior. How did you build & run the program?
--- rod
 
Old 07-08-2007, 08:57 AM   #5
piyush.kansal
LQ Newbie
 
Registered: May 2007
Location: Pune, India
Distribution: Fedora Core
Posts: 29

Original Poster
Rep: Reputation: 15
Well,
I build the program like this:

gcc -D_REENTRANT -I/usr/include/nptl main.c -o main -L/usr/lib/nptl -lpthread

and I have FC1 on my machine.
 
Old 07-08-2007, 09:01 AM   #6
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Quoth theNbomr:

Quote:
Running your program on my system (FC3) did not exhibit the same behavior.
It didn't for me, either. I'm running Slackware 9.1, kernel 2.4.22.
 
Old 07-08-2007, 09:08 AM   #7
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
I posted just before you did, piyush.kansal, so I didn't see your post #5. But when I built it as you did, the behavior of my program continued to be different from the behavior you reported.

Incidentally, the "normal" way to compile a pthreads program is thus:

Code:
cc -pthread main.c -o main
... but that doesn't change the results; either way, my test didn't have the same results as yours.
 
Old 07-08-2007, 09:22 AM   #8
piyush.kansal
LQ Newbie
 
Registered: May 2007
Location: Pune, India
Distribution: Fedora Core
Posts: 29

Original Poster
Rep: Reputation: 15
This is the fresh result of that program:

[piyushkansal@localhost Threading]$ ./prog
I am Thread. My PID is :: 4042. My PPID is :: 3916
I am Parent. My PID is :: 4042. My PPID is :: 3916
I am Thread. My PID is :: 4042. My PPID is :: 3916
I am Parent. My PID is :: 4042. My PPID is :: 3916

and this is what I get from ps -efm:
[piyushkansal@localhost piyushkansal]$ ps -efm
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 18:18 ? 00:00:04 init [5]
root 2 1 0 18:18 ? 00:00:00 [keventd]
root 3 1 0 18:18 ? 00:00:00 [ksoftirqd/0]
root 5 1 0 18:18 ? 00:00:00 [bdflush]
.
.
500 4036 3961 0 18:43 pts/2 00:00:00 ./firefox-bin
500 4042 3916 0 18:43 pts/2 00:00:00 ./prog
500 4043 4042 0 18:43 pts/2 00:00:00 ./prog
500 4044 3999 0 18:44 pts/3 00:00:00 ps -efm
[piyushkansal@localhost piyushkansal]$

The reason I am doing the compilation like this (gcc -D_REENTRANT -I/usr/include/nptl main.c -o main -L/usr/lib/nptl -lpthread) is because if I try to compile it in a normal way, then it gives linking problem to be:

[piyushkansal@localhost Threading]$ touch prog.c
[piyushkansal@localhost Threading]$ make prog
cc prog.c -o prog
/tmp/ccfODsNh.o(.text+0x1e): In function `main':
: undefined reference to `pthread_create'
collect2: ld returned 1 exit status
make: *** [prog] Error 1
[piyushkansal@localhost Threading]$

So, whatever way I am doing compilation is just to ensure that my application is getting linked with libpthread.so

Last edited by piyush.kansal; 07-08-2007 at 09:27 AM.
 
Old 07-08-2007, 09:30 AM   #9
piyush.kansal
LQ Newbie
 
Registered: May 2007
Location: Pune, India
Distribution: Fedora Core
Posts: 29

Original Poster
Rep: Reputation: 15
Infact,
When i tried to build like:

cc -lpthread prog.c -o prog

Then also i am getting the same behaviour:

500 4081 3916 0 18:58 pts/2 00:00:00 ./prog
500 4082 4081 0 18:58 pts/2 00:00:00 ./prog
500 4083 3999 0 18:58 pts/3 00:00:00 ps -efm
[piyushkansal@localhost piyushkansal]$
 
Old 07-08-2007, 02:13 PM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
It looks like you are using the Native POSIX Thread Library, which seems to be a kludge. If your FC1 system has something like /usr/lib/libpthread.so (no nptl), try linking against that. Otherwise, I think you are stuck with what you have unless you upgrade your kernel/distro.

--- rod.
 
Old 07-09-2007, 11:17 AM   #11
piyush.kansal
LQ Newbie
 
Registered: May 2007
Location: Pune, India
Distribution: Fedora Core
Posts: 29

Original Poster
Rep: Reputation: 15
Quote:
It looks like you are using the Native POSIX Thread Library, which seems to be a kludge. If your FC1 system has something like /usr/lib/libpthread.so (no nptl), try linking against that. Otherwise, I think you are stuck with what you have unless you upgrade your kernel/distro.
Yes, you are right. My distribution has got NPTL only. So, I will now download the latest pthread implementation, try it again and will get back to you.

But just for my learning, I dig the NPTL thing more to understand why its happening that way and found following:

1. Threads are created by clone() system call. While calling, we pass one flag to it. This can have multiple values, but the ones relevant to us are:
a) CLONE_PARENT and
b) CLONE_PID

a) CLONE_PARENT: (Linux 2.4 onwards) If CLONE_PARENT is set, then the parent of the new child (as returned by getppid(2)) will be the same as that of the calling process. If CLONE_PARENT is not set, then (as with fork(2)) the child’s parent is the calling process.
So, here is the first catch that may be in the source code, CLONE_PARENT is not set and which is why I am seeing child's parent as its calling process (on ps -efm).

b) CLONE_PID: If CLONE_PID is set, the child process is created with the same process ID as the calling process. If CLONE_PID is not set, the child process possesses a unique process ID, distinct from that of the calling process. This flag can only be specified by the system boot process (PID 0).
And here is the second catch that may be CLONE_PID is not set and that is why i am seeing different PID for child process (on ps -efm)

But if I assume it as correct then why my program is printing same PID and PPID for both child and parent (see my post no. 8, http://www.linuxquestions.org/questi...86#post2815086). So, the output shown by getpid(), getppid() and "ps -efm" contradicts each other.

I can see the source code of pthread_create() that which flags are being passed to clone(), but that will not be going to solve the crisis as the real crisis is the contradictory output shown by the two mechanisms.

Please suggest something as this is literally making me crazy.
 
Old 07-09-2007, 09:02 PM   #12
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Don't know if this will help you, but I noticed that creating thrs on 2.4 kernel caused each thr to have it's own PID, whereas on 2.6 kernel there is only 1 process (& 1 pid) for whole 'program'.
NB: this was done in Perl, but that (eventually) uses the underlying libs that C uses.
 
  


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
kill pid.... won't work with 'pid' variable given.. sachitha Programming 6 03-06-2006 08:48 PM
Can sub processing modify the parent's Gtk Widgets? asnoka Programming 2 12-24-2005 04:35 AM
How do I get the LWP pid of linux threads? ninelion Programming 4 08-16-2005 09:32 PM
/var/run/[XXX].pid - Tcl pid code liguorir Linux - Software 1 05-20-2004 11:32 PM
ERROR: Couldn't write pid to pid file lawrencegoodman Linux - Newbie 2 02-13-2004 09:05 PM


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