Привет, вопрос про Linux треды — должен ли быть свой PID у каждого треда?
Вопрос решен.
Есть такой код:
#!/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;
}
Запускаем на Debian 6.0:
root@localhost:~# ps xaHo pid,ppid,pcpu,lwp,args,rss | grep thread_test
3873 3871 0.0 3873 perl thread_test.pl 11464
3873 3871 4.9 3874 perl thread_test.pl 11464
3873 3871 4.9 3875 perl thread_test.pl 11464
3873 3871 4.9 3876 perl thread_test.pl 11464
3873 3871 4.9 3877 perl thread_test.pl 11464
3873 3871 4.9 3878 perl thread_test.pl 11464
3873 3871 4.9 3879 perl thread_test.pl 11464
3873 3871 4.9 3880 perl thread_test.pl 11464
3873 3871 4.9 3881 perl thread_test.pl 11464
3873 3871 4.9 3882 perl thread_test.pl 11464
3873 3871 4.9 3883 perl thread_test.pl 11464
3873 3871 4.9 3884 perl thread_test.pl 11464
3873 3871 4.9 3885 perl thread_test.pl 11464
3873 3871 4.9 3886 perl thread_test.pl 11464
3873 3871 4.9 3887 perl thread_test.pl 11464
3873 3871 4.9 3888 perl thread_test.pl 11464
3873 3871 4.9 3889 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 grep thread_test 876
Почему pid процесса один?
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.
Опять таки в htop картинка:
Здесь pid-ы у тредов разные, wtf?
В htop в колонке pid при отображет LWP.