- 论坛徽章:
- 0
|
LZ是在linux下创建的吗?
对于在一个进程中创建的多线程,貌似用ps是看不到线程的,虽然linux的多线程是用进程实现的,如果
你调用fork创建子进程的话,ps可以看到多个。
不过你可以在proc下看看到底有几个线程,方法:
ps获取当前你的程序的PID
然后- cat /proc/你进程的ID/status | grep Threads
复制代码 就能看到该程序创建线程的个数了。
下面是一个例子,ps看到的只有一个进程。(我的系统是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 编辑 ] |
|