- 论坛徽章:
- 0
|
advance linux programing上说:
Whenever you call pthread_create to create a new thread, Linux creates a new process that runs that thread.
还附了一个程序:
- #include <pthread.h>
- #include <stdio.h>
- #include <unistd.h>
- void* thread_function (void* arg) {
- fprintf (stderr, “child thread pid is %d\n”, (int) getpid ());
- /* Spin forever. */
- 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);
- /* Spin forever. */
- while (1);
- return 0;
- }
复制代码
作者的运行结果是:
% ./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
14608 pts/9 R 0:01 ./thread-pid
14609 pts/9 S 0:00 ./thread-pid
14610 pts/9 R 0:01 ./thread-pid
14611 pts/9 R 0:00 ps x
但我的机子上运行的结果是:
./thread-pid &
[1] 13071
david@david-desktop:~/alp/ALP-listings/chapter-4$ main thread pid is 13071
child thread pid is 13071
呵呵。小弟是个菜鸟。请各位指教。
[ 本帖最后由 wtwang1985 于 2007-2-9 23:17 编辑 ] |
|