在Linux中,线程被定义为"任务".
每个任务都是流程中的单个执行单元.它们都有自己的任务ID tid- 与进程ID有关pid.
每个进程在启动时都有一个主要任务,"主"任务标识进程,实际上进程ID pid是tid主任务的任务ID .
执行过程中的过程状态类似于主要任务的过程状态R,包括状态D,S......
因此,如果您的进程被标记为D(磁盘休眠),则仅表示主任务处于磁盘休眠状态.所有其他任务(线程)可能正在做其他任何事情.
检查/proc/[pid]/task/[tid]/stat各个任务状态.
还ps -eLf显示任务的ps条目.
试试这段代码:
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
void * thread_sleep() {
long long int i = -1;
while (i--);
exit(0);
return NULL;
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, thread_sleep, NULL);
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运行它并做
cat /proc/$PID/stat
cat /proc/$PID/task/*/stat
Run Code Online (Sandbox Code Playgroud)
您将注意到进程具有状态S(等待终端输入),第一个任务(与tid进程相同pid),而另一个线程是R.如果你设置i为较小的东西,它实际上会在某个时刻完成.
| 归档时间: |
|
| 查看次数: |
3260 次 |
| 最近记录: |