- 論壇徽章:
- 0
|
LZ是在linux下創(chuàng)建的嗎?
對于在一個進(jìn)程中創(chuàng)建的多線程,貌似用ps是看不到線程的,雖然linux的多線程是用進(jìn)程實現(xiàn)的,如果
你調(diào)用fork創(chuàng)建子進(jìn)程的話,ps可以看到多個。
不過你可以在proc下看看到底有幾個線程,方法:
ps獲取當(dāng)前你的程序的PID
然后- cat /proc/你進(jìn)程的ID/status | grep Threads
復(fù)制代碼 就能看到該程序創(chuàng)建線程的個數(shù)了。
下面是一個例子,ps看到的只有一個進(jìn)程。(我的系統(tǒng)是linux AS4 64位 kernel 2.6.9-11.ELsmp)
//duanjigang@2008-07-25
//thread_test.c
//build: gcc thread_test.c -o thread_test -lpthread
#include <pthread.h>
#include <stdio.h>
void * thread_func(void*);
int main(int argc, char* argv[])
{
pthread_t thread;
int interval = 1;
if(pthread_create(&thread, NULL, thread_func, 0) != 0)
{
printf("create tread fail\n");
return 0;
}
if(pthread_detach(thread) != 0)
{
printf("thread detach fail\n");
return 0;
}
while(1)
{
printf("I am the main thread\n");
sleep(1);
}
return 1;
}
void* thread_func(void* pData)
{
int interval = 0;
if(!pData)
{
interval = 1;
}else
{
interval = *(int*)pData;
}
if(interval <= 0)
{
interval = 1;
}
while(1)
{
printf("I am a thread function\n");
sleep(interval);
}
return 0;
}
|
[ 本帖最后由 duanjigang 于 2008-7-25 10:30 編輯 ] |
|