如何显示每个线程cpu和如何定位哪个线程

1.如何显示每个线程cpu耗用

 

ps auxH | grep test

2.如何定位哪个线程


#define THREAD_NUM 8

void *myfunc(void *arg)
{
printf("thread %d\n",*(int *)arg);
sleep(1);
while(1)
{
int *tid = (int *)arg;
if (*tid == 3)
usleep((10+1)*20);
if (*tid == 7)
usleep((5+1)*20);
//printf("haha %d\n",*tid);
}
}
pthread_t tid[THREAD_NUM];
int id[THREAD_NUM] = {0};

for (int i = 0; i < THREAD_NUM; i++)
{
id[i] = i;
if (pthread_create(&tid[i],NULL,&myfunc,(void*)&id[i]) != 0)
{
fprintf(stderr,"thread create failed\n");
return -1;
}
}

for (int i = 0 ;i < THREAD_NUM; i++)
pthread_join(tid[i],NULL);
==============
从0-7启动8个线程,3号线程usleep(220),7号线程usleep(120)
运行一小段时间后,结果
root 22074 0.0 0.0 95988 1072 pts/1 Sl+ 11:11 0:00 ./test
root 22074 81.2 0.0 95988 1072 pts/1 Rl+ 11:11 0:08 ./test
root 22074 81.2 0.0 95988 1072 pts/1 Rl+ 11:11 0:08 ./test
root 22074 81.2 0.0 95988 1072 pts/1 Rl+ 11:11 0:08 ./test
root 22074 0.4 0.0 95988 1072 pts/1 Sl+ 11:11 0:00 ./test
root 22074 81.2 0.0 95988 1072 pts/1 Rl+ 11:11 0:08 ./test
root 22074 81.2 0.0 95988 1072 pts/1 Rl+ 11:11 0:08 ./test
root 22074 81.1 0.0 95988 1072 pts/1 Rl+ 11:11 0:08 ./test
root 22074 0.6 0.0 95988 1072 pts/1 Sl+ 11:11 0:00 ./test
root 22094 0.0 0.0 103248 864 pts/2 S+ 11:11 0:00 grep test

第5行cpu使用率为0.4%,第9行cpu使用率为0.6%

推测第5行为3号线程,第9行为7号线程(多的2个数为main主线程和0号线程)

可见-H显示顺序是按照pthread_create顺序来进行(猜测)

posted @ 2015-03-31 10:12  dodng  阅读(288)  评论(0)    收藏  举报