#include <stdio.h>
#include <unistd.h>
void* thread_function(void* arg) {
fprintf(stderr, "child thread pid is %d\n", (int) getpid());
/* Бесконечный цикл. */
while (1);
return NULL;
}
int main() {
pthread_t thread;
fprintf(stderr, "main thread pid is %d\n", (int)getpid());
pthread_create(&thread, NULL, &thread_function, NULL);
/* Бесконечный цикл. */
while (1);
return 0;
}
Запустите программу в фоновом режиме, а затем вызовите команду ps x, чтобы увидеть список выполняющихся процессов. Не забудьте затем уничтожить программу thread-pid, так как она потребляет ресурсы процессора. Вот что мы получим:
% cc thread-pid.c -о thread-pid -lpthread
% ./thread-pid &
[1] 14608
main thread pid is 14608
child thread pid is 14610
% ps x
PID TTY STAT TIME COMMAND
14042 pts/9 S 0:00 bash
14068 pts/9 R 0:01 ./thread-pid
14069 pts/9 S 0:00 ./thread-pid
14610 pts/9 R 0:01 ./thread-pid
14611 pts/9 R 0:00 ps x
% kill 14608