Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7553450
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: 嵌入式

2011-02-25 10:27:38

  1. /* 文件名:gettid.c */
  2. /* 说明:获取 TID 例程 */

  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/syscall.h>
  7. #include <pthread.h>

  8. void *thread_fun(void *arg)
  9. {
  10.     pid_t pid;
  11.     pthread_t th;
  12.     int index = (int)arg;
  13.     th = pthread_self();
  14.     printf("pthread of thread %d is: %x\n", index, (int)th);
  15.     pid = getpid();
  16.     printf("PID of thread %d is: %d\n", index, pid);
  17.     pid = syscall(SYS_gettid);
  18.     printf("TID of thread %d is: %d\n", index, pid);
  19.     return NULL;
  20. }

  21. int main(void)
  22. {
  23.     int i;
  24.     pid_t pid;
  25.     pthread_t th;
  26.     pid = getpid();
  27.     printf("PID of main process is: %d\n", pid);
  28.     pid = syscall(SYS_gettid);
  29.     printf("TID of main process is: %d\n", pid);
  30.     for (i = 0; i < 3; i++) {
  31.         if (pthread_create(&th, NULL, thread_fun, (void *)i)) {
  32.             perror("main: pthread_create()");
  33.             return -1;
  34.         }
  35.         pthread_join(th, NULL);
  36.     }
  37.     return 0;
  38. }
阅读(1290) | 评论(0) | 转发(2) |
0

上一篇:定时器例程

下一篇:内核集合与对象例程

给主人留下些什么吧!~~