Chinaunix首页 | 论坛 | 博客
  • 博客访问: 536054
  • 博文数量: 67
  • 博客积分: 1625
  • 博客等级: 上尉
  • 技术积分: 1053
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-04 14:40
文章分类

全部博文(67)

文章存档

2012年(67)

分类: LINUX

2012-04-19 18:23:14

#include            /*printf()*/

#include          /*pthread_create(),pthread_self()*/

#include           /*getpid(),sleep()*/

 

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成功;

 

阅读(1707) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~