[root@localhost ch12]# cat thread5.c #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <pthread.h> void *thread_function(void *arg); char message[] = "Hello World"; int thread_finished = 0; int res; int main() { int res; pthread_t a_thread; void *thread_result; int count=0; while(count++<10){ res = pthread_create(&a_thread,NULL, thread_function, (void *)message); } if (res != 0) { perror("Thread creation failed"); exit(EXIT_FAILURE); } printf("main thread will exit !\n"); //pthread_exit(NULL); pthread_join(a_thread,NULL); printf("all exit!\n"); //exit(0); } void *thread_function(void *arg) { printf("thread_function is running. Argument was %s\n", (char *)arg); sleep(200); printf("sub thread will exit !\n"); //pthread_exit(NULL); }
[root@localhost ch12]# ./thread5 thread_function is running. Argument was Hello World main thread will exit ! thread_function is running. Argument was Hello World thread_function is running. Argument was Hello World thread_function is running. Argument was Hello World thread_function is running. Argument was Hello World thread_function is running. Argument was Hello World thread_function is running. Argument was Hello World thread_function is running. Argument was Hello World thread_function is running. Argument was Hello World thread_function is running. Argument was Hello World
[root@localhost ~]# ps -eLf UID PID PPID LWP C NLWP STIME TTY TIME CMD ... root 13967 9501 13967 0 11 19:16 pts/3 00:00:00 ./thread5 root 13967 9501 13968 0 11 19:16 pts/3 00:00:00 ./thread5 root 13967 9501 13969 0 11 19:16 pts/3 00:00:00 ./thread5 root 13967 9501 13970 0 11 19:16 pts/3 00:00:00 ./thread5 root 13967 9501 13971 0 11 19:16 pts/3 00:00:00 ./thread5 root 13967 9501 13972 0 11 19:16 pts/3 00:00:00 ./thread5 root 13967 9501 13973 0 11 19:16 pts/3 00:00:00 ./thread5 root 13967 9501 13974 0 11 19:16 pts/3 00:00:00 ./thread5 root 13967 9501 13975 0 11 19:16 pts/3 00:00:00 ./thread5 root 13967 9501 13976 0 11 19:16 pts/3 00:00:00 ./thread5 root 13967 9501 13977 0 11 19:16 pts/3 00:00:00 ./thread5 root 13980 13373 13980 0 1 19:16 pts/5 00:00:00 ps -eLf ...
[root@localhost ~]# ps axms UID PID PENDING BLOCKED IGNORED CAUGHT STAT TTY TIME COMMAND 0 13967 0000000000000000 - - - - pts/3 0:00 ./thread5 0 - 0000000000000000 0000000000000000 0000000000000000 0000000180000000 Sl+ - 0:00 - 0 - 0000000000000000 0000000000000000 0000000000000000 0000000180000000 Sl+ - 0:00 - 0 - 0000000000000000 0000000000000000 0000000000000000 0000000180000000 Sl+ - 0:00 - 0 - 0000000000000000 0000000000000000 0000000000000000 0000000180000000 Sl+ - 0:00 - 0 - 0000000000000000 0000000000000000 0000000000000000 0000000180000000 Sl+ - 0:00 - 0 - 0000000000000000 0000000000000000 0000000000000000 0000000180000000 Sl+ - 0:00 - 0 - 0000000000000000 0000000000000000 0000000000000000 0000000180000000 Sl+ - 0:00 - 0 - 0000000000000000 0000000000000000 0000000000000000 0000000180000000 Sl+ - 0:00 - 0 - 0000000000000000 0000000000000000 0000000000000000 0000000180000000 Sl+ - 0:00 - 0 - 0000000000000000 0000000000000000 0000000000000000 0000000180000000 Sl+ - 0:00 - 0 - 0000000000000000 0000000000000000 0000000000000000 0000000180000000 Sl+ - 0:00 - 0 13985 0000000000000000 - - - - pts/5 0:00 ps axms 0 - 0000000000000000 0000000000000000 0000000000000000 0000000073d3fad9 R+ - 0:00 -
from man ps shown below
lwp LWP lwp (light weight process, or thread) ID of the lwp being reported. (alias spid, tid). nlwp NLWP number of lwps (threads) in the process. (alias thcount).
从ps -eLf的输出可以看出,线程是当做轻量级进程对待的,但"线程"对外表现都属于同一个进程