Linux threads pids?

Привет, вопрос про 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 картинка:
022f676e6826bea761c7930ef16f2c57.png

Здесь pid-ы у тредов разные, wtf?


В htop в колонке pid при отображет LWP.
  • Вопрос задан
  • 4067 просмотров
Решения вопроса 1
vsespb
@vsespb
Всё правильно.

1) use threads; не использует fork (хотя реализация ни по дизайну ни по реальной картине, не в чём не выигрывает у fork, под linux)

2) нужны разные pid — попробуйте use forks или используйте fork

3) 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, e.g.

5) htop — нажмите букву 'H' это будет показывать/отключать threads
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
25 апр. 2024, в 12:20
15000 руб./за проект
25 апр. 2024, в 12:08
300 руб./за проект
25 апр. 2024, в 11:49
25000 руб./за проект