第二期 getpid()、pthread_self()、pthread_create()

简介:     线程的使用就要先创建!线程的创建也是一个学问,请听dyli向你慢慢道来。请看下面代码:     1、主要函数代码 thread_create.c   /* Name thread_create.
 
  线程的使用就要先创建!线程的创建也是一个学问,请听dyli向你慢慢道来。请看下面代码:
 
 
1、主要函数代码 thread_create.c
 
  1. /* Name thread_create.c
  2.  * Author dyli
  3.  * date 20110615
  4.  * Description 处理getpid()、pthread_self()、pthread_create()函数,
  5.  *                并进行讨论主子线程的问题
  6.  * */

  7. #includestdio.h>
  8. #includepthread.h>
  9. #includestring.h>
  10. #includesys/types.h>
  11. #includeunistd.h>

  12. /***getpid()函数************************************************
  13. *    函数功能:    取得进程ID
  14. *    函数原型:
  15. *    返回值 :    
  16. *                
  17. *    所需库 :    
  18. * 备注 :    
  19. ****************************************************************/

  20. /***pthread_self()函数*******************************************
  21. *    函数功能:    获取当前线程的ID
  22. *    函数原型: pthread_t pthread_self(void)
  23. *    返回值 :    
  24. *                
  25. *    所需库 : pthread.h>    
  26. * 备注 : pthread_t的类型为unsighned long int,打印时用 %u/%lu,
  27. *             否则显示结果出问题     
  28. *****************************************************************/

  29. /***pthread_create()函数******************************************
  30. *    函数功能:    创建线程
  31. *    函数原型:
  32. *            参数一 指向线程TID指针
  33. *            参数二 设置线程默认属性,NULL则默认
  34. *            参数三 线程内要运行的函数的起始地址
  35. *            参数四 运行的函数的参数,一般为NULL            
  36. *                
  37. *    返回值 :    
  38. *                
  39. *    所需库 :    phthead.h>
  40. * 备注 :        
  41. ****************************************************************/


  42. /********pthread_t类型*******************************************
  43. pthread_t
  44. 重定义类开 :    typedef long int pthread_t
  45. 用途         :    用于声明线程TID
  46. 长度         : sizeof(pthread_t)=4
  47. ****************************************************************/
  48. pthread_t ntid;


  49. /*******************
  50. *打印ID信息:PID TID
  51. ********************/
  52. void print_id_mes(const char *s){
  53.     pid_t pid;
  54.     pthread_t tid;

  55.     pid = getpid();    //    -------------------------------------------------------->func1获取进程PID

  56.     tid = pthread_self();// --------------------------------------------------->func2获取线程TID

  57.     printf("%s pid %u tid %u (0x%x)\n\n",s,(unsigned int)pid,(unsigned int)tid,(unsigned int)tid);

  58.     printf("use 百分之d to print tid = %d,this is a mess Num!!! \n\n",(unsigned int)tid);
  59. }

  60. /*******************
  61. *thr_fn 新创建的线程所调用的函数
  62. ********************/
  63. void *thr_fn(void *arg){
  64.     print_id_mes("this is new thread:");//先被子线程调用
  65.     return ((void *)0);
  66. }

  67. /******************************************
  68. 进程的主线程
  69.     当一个程序启动时,就有一个进程被操作系统(OS)创建,与此同时一个线程也立刻运行,
  70. 该线程通常叫做程序的主线程(Main Thread),因为它是程序开始时就执行的,如果你需要
  71. 再创建线程,那么创建的线程就是这个主线程的子线程。每个进程至少都有一个主线程,
  72. 在Winform中,应该就是创建GUI的线程。
  73. 主线程的重要性体现在两方面:
  74.     1.是产生其他子线程的线程;
  75.     2.通常它必须最后完成执行比如执行各种关闭动作。
  76. *****************************************/
  77. int main(){
  78.     int err;

  79.     err = pthread_create(&ntid,NULL,thr_fn,NULL);//---------------------------->func3创建线程
  80.     if(err != 0){//--------------------------->这里说明返回0表示创建线程成功!
  81.     printf("can't create thread: %s\n",strerror(err));
  82.     return 1;
  83.     }

  84.     print_id_mes("this is main thread:");//后被主线程调用
  85.     sleep(1);
  86.     return 0;
  87. }
 
 
 
2、本程序的执行效果如下
 
 
  1. [root@localhost thread_create]# ./thread_create
  2. this is new thread: pid 10222 tid 3087928208 (0xb80e0b90)

  3. use 百分之d to print tid = -1207039088,this is a mess

  4. this is main thread: pid 10222 tid 3087931072 (0xb80e16c0)

  5. use 百分之d to print tid = -1207036224,this is a mess
 
 
3、主要代码文件
 
 
 
 
 
 
 
 
相关文章
|
3月前
|
存储 安全 NoSQL
pthread_getspecific和pthread_setspecific详解
pthread_getspecific和pthread_setspecific详解
|
4月前
条件变量函数pthread_cond_timedwait实现业务场景
条件变量函数pthread_cond_timedwait实现业务场景
34 0
|
3月前
|
存储 缓存 安全
C语言进程(第二章,wait,sleep,waitpid,pthread_mutex_lock,pthread_mutex_unlock)
C语言进程(第二章,wait,sleep,waitpid,pthread_mutex_lock,pthread_mutex_unlock)
27 0
|
算法 物联网 Linux
Pthread_create 线程创建|学习笔记
快速学习 Pthread_create 线程创建
96 0