免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3763 | 回复: 3
打印 上一主题 下一主题

[技术动态] strace与pthread之间的关系 [复制链接]

论坛徽章:
7
摩羯座
日期:2013-12-05 10:42:57辰龙
日期:2013-12-27 13:40:49亥猪
日期:2014-01-15 09:10:37天秤座
日期:2014-01-20 11:22:20辰龙
日期:2014-01-26 17:02:25午马
日期:2014-01-27 14:22:34水瓶座
日期:2014-02-19 09:36:40
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-09-29 16:17 |只看该作者 |倒序浏览
之前一直没有用过strace命令,今天用的时候用"strace -p 进程id“ 发现只列出来主线程的系统调用,其他的列不出来。然后做了一个例子,非常简单。
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <pthread.h>

  4. void* runner(void* param)
  5. {
  6.     while (1)
  7.     {   
  8.         sleep(1);
  9.         printf("........\n");
  10.     }   
  11. }

  12. int main()
  13. {

  14.     pthread_t pid;
  15.     int s = pthread_create(&pid, NULL, runner, NULL);

  16.                                                                                                                                                                                                
  17.     while (1)
  18.     {   
  19.         sleep(2);
  20.     }   
  21.     return 0;
  22. }
复制代码
这时用strace -p 进程id -f 才可以列出创建线程中的系统调用。
man strace 发现
       -f          Trace child processes as they are created by currently traced processes as a result of the fork(2) system call.

                   On  non-Linux  platforms  the new process is attached to as soon as its pid is known (through the return value of fork(2) in the parent process).
                   This means that such children may run uncontrolled for a while (especially in the case of a vfork(2)), until the parent  is  scheduled  again  to
                   complete  its  (v)fork(2) call.  On Linux the child is traced from its first instruction with no delay.  If the parent process decides to wait(2)
                   for a child that is currently being traced, it is suspended until an appropriate child process either terminates or incurs a  signal  that  would
                   cause it to terminate (as determined from the child’s current signal disposition).

英语不大好,大体的意思应该是追踪当前进程fork出来的子进程。没有研究过pthread的源码,难道pthread是通过fork实现的?!

论坛徽章:
7
天蝎座
日期:2013-09-28 10:45:42双子座
日期:2013-10-16 16:27:09射手座
日期:2013-10-23 10:21:32处女座
日期:2014-09-17 16:44:332015年亚洲杯之巴林
日期:2015-04-09 17:28:01冥斗士
日期:2015-11-26 16:19:0015-16赛季CBA联赛之山东
日期:2018-03-02 23:59:31
2 [报告]
发表于 2013-09-29 16:29 |只看该作者
strace -pf pid
会列出所有的fork,pthread_create出来的进程,线程。

如果觉得strace跟踪出来的太乱,就
pstree -p pid,会给你列出进程,线程树。

然后strace -p,一个一个的strace。

论坛徽章:
7
摩羯座
日期:2013-12-05 10:42:57辰龙
日期:2013-12-27 13:40:49亥猪
日期:2014-01-15 09:10:37天秤座
日期:2014-01-20 11:22:20辰龙
日期:2014-01-26 17:02:25午马
日期:2014-01-27 14:22:34水瓶座
日期:2014-02-19 09:36:40
3 [报告]
发表于 2013-09-29 16:34 |只看该作者
谢谢!回复 2# cxytz01


   

论坛徽章:
0
4 [报告]
发表于 2013-09-30 13:41 |只看该作者
948039985QQ群,这是一个讨论Linux技术和服务器技术平台,希望大家加入这个群一起讨论和学习,群主旨在提供一个Linux技术平台,为加入此群的人提供技术交流,同时希望高手,老手们加入,以帮助辅导那些刚入门的新手们,俗话说“助人乃快乐之本”,同时我们还欢迎成员讨论当前IT新技术话题,同样可以讨论技术管理和管理话题。此群是一个多元化的平台,学习技术的同时还可以了解管理方面的知识,促进群成员的全面发展,而不只是停留在技术层面。欢迎你们的加入!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP