linux下pthread简单编程实例及gdb调试(core dumped)

本文介绍了在Linux环境下进行pthread编程时遇到的Segmentation fault (core dumped)错误,以及如何理解和解决这个问题。通过分析core dump,利用gdb调试,发现错误源于程序中的内存访问。文章还详细讲解了core dump的定义、生成条件、文件命名规则,并提供了使用gdb分析core文件的步骤。
摘要由CSDN通过智能技术生成

最近在看 operating system ,看到pthread编程,就试了一下啊,挺简单的却出现了错误:

程序如下:

/*************************************************************

#include <pthread.h>
#include <stdio.h>

int sum;/* this data is shared by the thread(s)*/
void *runner(void *param);

int main(int argc,char *argv[])
{
        pthread_t tid;
        pthread_attr_t attr; /*set of thread attributes*/

        if(argc!=2){
        fprintf(stderr,"usage:a.out<integer value>\n");
        return -1;
        }

        if(atoi(argv[1])<0){
        fprintf(stderr,"%d must be >=0\n",atoi(argv[1]));
        return -1;
        }

        /*get the default attributes*/
        pthread_attr_init(&attr);
        /*create the thread*/
        pthread_create(&tid,&attr,runner,argv[1]);
        /*wait for the thread to exit*/
        pthread_join(tid,NULL);

        printf("sum=%d\n",sum);
}

/*the thread will begin control in this function*/
void * runner(void *param)
{
        int i,upper=atoi(param);
        sum=0;

        for(i=1;i<=upper;i+

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值