2012年(67)
分类: LINUX
2012-04-19 18:23:14
#include
#include
#include
void *create(void *arg) /*无参函数,arg好多余*/
{
printf("New thread .... \n");
printf("This thread's id is %u \n", (unsigned int)pthread_self());/*获取并打印线程*/
printf("The process pid is %d \n",getpid()); /*获取进程id(应该和main中获取的一样)*/
return (void *)0;
}
int main(int argc,char *argv[]) /*无参函数,argc argv[]好多余*/
{
pthread_t tid;
int error;
printf("Main thread is starting ... \n");
error = pthread_create(&tid, NULL, create, NULL);/*创建线程(线程处于就绪态)*/
if(error)
{
printf("thread is not created ... \n");
return -1;
}
printf("The main process's pid is %d \n",getpid()); /*获取进程*/
sleep(1); /*主进程睡眠,让线程运行*/
return 0; /*应该有一个pthread_join()效果更好*/
}
运行结果:
tu
从运行结果看,在主进程和线程中获取的进程id都是主进程id;获取并打印线程id成功;