P
P
Pavel Tsaregorodtsev2012-12-02 13:57:15
linux
Pavel Tsaregorodtsev, 2012-12-02 13:57:15

Linux thread pids?

Hello, a question about Linux threads - should each thread have its own PID?
Issue resolved.
There is this code:

#!/usr/bin/perl -w
use strict;
use warnings;
use threads;

my $thread_num = 20;
my @threads;

foreach (1..$thread_num) {
    push @threads, threads->create(\&calc);
}

foreach my $thread ( @threads ) {
        my $thread_end = $thread->join ;
        print "Thread [ $thread_end ] Ending ... \n";
}


sub calc {
    my $count = 0;
    foreach (1..1000000000){
        $count += $_;
    }
    return $count;
}

Run on Debian 6.0:
[email protected]:~# ps xaHo pid,ppid,pcpu,lwp,args,rss | grep thread_test
3873 3871 0.0 3873 perl thread_test.pl 11464
3873 3871 3874 4.9 perl thread_test.pl 11464
3873 3871 3875 4.9 perl thread_test.pl 11464
3873 3871 3876 4.9 perl thread_test.pl 11464
3873 3871 3877 4.9 perl thread_test.pl 11464
3873 3871 4.9 3878 Perl Thread_test.pl 11464
3873 3871 4879 Perl Thread_Test.pl 11464
3873 3871 4880 Perl Thread_test.pl 11464
3873 3871 4881 Perl Thread_Test.pl 11464
3873 3871 4882 Perl Thread_Test.pl 11464
3873 3871 4883 Perl Thread_Test.pl 11464
3873 3871 4.9 3884 perl thread_test.pl 11464
3873 3871 4885 Perl Thread_test.pl 11464
3873 3871 4886 3873 3871 4886
3873 3871 4887 3873 3871 4887 Perl Thread_test.pl 11464
3873 3871 4888 Perl Thread_Test.pl 11464
3873 3871 4889 Perl Thread_Test.pl 11464
3873 3871 4.9 3890 Perl thread_test.pl 11464
3873 3871 4.9 3891 perl thread_test.pl 11464
3873 3871 4.9 3892 perl thread_test.pl 11464
3873 3871 4.9 3893 perl thread_test.pl 11464
3926 3872 0.0 3926 perl thread_test.pl
11464 3926 3872 0.0 3926
man clone()
CLONE_PARENT (since Linux 2.3.12)
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.
Note that it is the parent process, as returned by getppid(2), which is signaled when the child terminates, so that if CLONE_PARENT is set, then the parent of the calling
process, rather than the calling process itself, will be signaled.
Again, in the htop picture:
022f676e6826bea761c7930ef16f2c57.png
Here the pids of the threads are different, wtf?
In htop, in the pid column, when will display the LWP.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vsespb, 2012-12-02
@ixpict

Everything is correct.
1) use threads; does not use fork (although the implementation, neither by design nor by the real picture, does not outperform fork in anything, under linux)
2) different pids are needed - try use forks or use fork
3) The process has one PID
4) man pstree
Child threads of a process are found under the parent process and are shown with the process name in curly braces, eg
5) htop - press letter 'H' this will show/disable threads

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question