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

来源:互联网 发布:淘宝卖家催好评用语 编辑:程序博客网 时间:2024/05/21 15:00

最近在看 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++)
                sum+=1;

        pthread_exit(0);
}

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

然后编译:gcc pthread.c -o pth -pthread

运行:./pth 5

出现了Segmentation fault (core dumped)这样的错误,经上网查证,是core dump了,

然后就有了下面几个问题:

1、什么是Core:

在使用半导体作为内存的材料前,人类是利用线圈当作内存的材料(发明者为王安),线圈就叫作 core ,用线圈做的内存就叫作 core memory。如今 ,半导体工业澎勃发展,已经没有人用 core memory 了,不过,在许多情况下,人们还是把记忆体叫作 core 。

2、什么是Core Dump:

我们在开发(或使用)一个程序时,最怕的就是程序莫明其妙地宕掉。虽然系统没事,但我们下次仍可能遇到相同的问题。于是这时操作系统就会把程序宕掉时的内存内容 dump 出来(现在通常是写在一个叫 core 的 file 里面),让我们或是 debugger 做为参考。这个动作就叫作 core dump。

3、Core Dump时会生成何种文件:

Core Dump时,会生成诸如 core.进程号 的文件。

4、为何有时程序Down了,却没生成 Core文件。

Linux下,有一些设置,标明了resources available to the shell and to processes。可以使用

#ulimit -a 来看这些设置。 (ulimit是bash built-in Command)

从这里可以看出,如果 -c是显示:core file size。如果这个值为0,则无法生成core文件。所以可以使用:#ulimit -c 1024   或者 #ulimit -c unlimited   

来使能生成 core文件。如果程序出错时生成Core 文件,则会显示Segmentation fault (core dumped) 。

5、Core Dump的核心转储文件目录和命名规则:

/proc/sys/kernel/core_uses_pid可以控制产生的core文件的文件名中是否添加pid作为扩展,如果添加则文件内容为1,否则为0

可通过以下命令修改此文件:

echo "1" > /proc/sys/kernel/core_uses_pid

6、如何使用Core文件:

在Linux下,使用:

#gdb -c core.pid program_name

就可以进入gdb模式。

输入where,就可以指出是在哪一行被Down掉,哪个function内,由谁调用等等。

(gdb) where

或者输入 bt。

(gdb) bt

以上是从别的文字找到的方法,下面是具体的我的程序问题所在:


$gcc pthread.c -o pth -pthread -g

ws@wsubuntu:~/work/pthread$ ls
pth  pthread.c
ws@wsubuntu:~/work/pthread$ ./pth 5
Segmentation fault (core dumped)
ws@wsubuntu:~/work/pthread$ ls
core  pth  pthread.c

ws@wsubuntu:~/work/pthread$ gdb pth core
GNU gdb (GDB) 7.5-ubuntu
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/ws/work/pthread/pth...done.
[New LWP 3461]

warning: Can't read pathname for load map: Input/output error.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".
Core was generated by `./pth 5'.
Program terminated with signal 11, Segmentation fault.
#0  0xb75da580 in ?? () from /lib/i386-linux-gnu/libc.so.6
(gdb) bt

#0  0xb75da580 in ?? () from /lib/i386-linux-gnu/libc.so.6
#1  0xb75da337 in strtol () from /lib/i386-linux-gnu/libc.so.6
#2  0xb75d763f in atoi () from /lib/i386-linux-gnu/libc.so.6
#3  0x080486b9 in main (argc=2, argv=0xbfcaae34) at pthread.c:17

经最后程序查看,得出是一个“( ”造成的,最后终于跑通了。
0 0
原创粉丝点击