关于core_uses_pid标志对多线程程序无用的调查


一、    案例的起因

在工作的过程中,发现在RHEL5.6系统下,运行进程,一旦应用线程,出core后,总会带着进程ID。即使在系统中设置core_uses_pid=0,也是不行的。

这样的话如果应用系统出现core文件,那么会是悲剧性的。磁盘空间被占满等情形都会出现的。

如何让我们的core_file的名字保持唯一或者有限个数呢?

二、资料的查询与分析

在网上“https://bugzilla.kernel.org”发现了这个bug的存在,简要信息如下:

Bug 6312 - core_uses_pid=0 not always honored

Status:

REJECTED WILL_NOT_FIX

Reported:

2006-03-30 15:10 by David M. Lee

Product:

Other

Modified:

2007-01-31 01:03 (History)

Component:

Other

Kernel Version:

2.6.15.2

Version:

2.5

Tree:

Mainline

Platform:

i386 Linux

Regression:

---

Importance:

P2 normal

 

 

Assigned To:

other_other@kernel-bugs.osdl.org

 

 

URL:

 

 

 

Depends on:

 

 

 

Blocks:

 

 

 

 

Show dependency tree / graph

 

 

 

详细信息参见:https://bugzilla.kernel.org/show_bug.cgi?id=6312

 

从该信息中获知,产生core文件时,名字是由函数format_corename来执行的。所以,针对内核源码“linux-2.6.27.62 和linux-3.2.7”做了分析。发现在3.2.7版本的内核中修复了改BUG。

经过对该函数的分析,发现我们之所以出现问题,就是由于参数nr_threads引起的,而这个参数的含义就是使用该资源的线程个数,该值是被内敛函数“zap_threads”来设置的。

而这段代码基本上是不可避免的都要走到的。我们如何跳过这段代码呢?如何让nr_threads不起作用呢?

       /*Backward compatibility with core_uses_pid:

        *

        * If core_pattern does not include a %p (as isthe default)

        * and core_uses_pid is set, then .%pid will beappended to

        * the filename. Do not do this for pipedcommands. */

       if(!ispipe && !pid_in_pattern

           && (core_uses_pid || nr_threads)) {

              rc= snprintf(out_ptr, out_end - out_ptr,

                           ".%d", task_tgid_vnr(current));

              if(rc > out_end - out_ptr)

                     gotoout;

              out_ptr+= rc;

       }

 

Fork为何会遵循core_uses_pid的设置呢?参见zap_threads中的解释。

        *fork:

        *    None of sub-threads can fork afterzap_process(leader). All

        *    processes which were created before thispoint should be

        *    visible to zap_threads() becausecopy_process() adds the new

        *    process to the tail of init_task.tasks list,and lock/unlock

        *    of ->siglock provides a memory barrier.

 

从函数实现中我们可以发现如下代码。

                            if (rc > out_end - out_ptr)

                                   goto out;

这个是表示的core文件名字的长度,那么最大长度CORENAME_MAX_SIZE是多大呢?

binfmts.h中定义:#defineCORENAME_MAX_SIZE 128

所以,我们采用边界值越界跳出上面那段让人头疼的代码。

测试的设置方法如下:设置文件名字长度core+123字符的形式定向到/proc/sys/kernel/core_pattern中。

[root@tcore]$echo"core123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123"> /proc/sys/kernel/core_pattern

测试程序:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>


#define NUM 3


void *thread_run()
{
printf("hello, I am thread[%llu]\n", pthread_self());
sleep(3);
abort();
return NULL;
}


int thread_pool_run()
{
int i;
pthread_t th[NUM];


for(i=0; i<NUM; i++){
printf("thread create\n");
pthread_create(&th[i], NULL, thread_run, NULL);
}
for(i=0; i<NUM; i++){
pthread_join(th[i], NULL);
printf("thread exit\n");
}
return 0;
}


int main()
{
int stat;
pid_t pid;


if((pid = fork()) < 0){
printf("fork error\n");
}else if(pid == 0){
printf("fork successful\n");
stat = thread_pool_run();
exit(stat);
}


pause();
return 0;
}

执行结果:

达到目的了。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值