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

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

最近在看 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 fo

即便在Linux系统将core文件大小设置为unlimited后,程序运行仍出现Segmentation fault (core dumped),可从以下方面解决: ### 内存访问越界 - **数组下标使用错误**:检查代码中数组的下标使用情况,保证没有使用错误的下标导致数组访问越界。例如,在使用数组时,要确保下标在合法范围内。 ```c #include <stdio.h> int main() { int arr[5]; // 错误示例:访问越界 // arr[5] = 10; // 正确示例 for (int i = 0; i < 5; i++) { arr[i] = i; } return 0; } ``` - **字符串结束符问题**:在搜索字符串时,要保证字符串正常使用结束符。使用字符串操作函数时,为防止读写越界,应使用更安全的函数,如用`strncpy`替代`strcpy`等。 ```c #include <stdio.h> #include <string.h> int main() { char src[] = "Hello"; char dest[10]; // 安全的字符串复制 strncpy(dest, src, sizeof(dest) - 1); dest[sizeof(dest) - 1] = '\0'; return 0; } ``` ### 多线程相关问题 - **线程不安全函数**:若程序是多线程的,要检查是否使用了线程不安全的函数,可替换为线程安全的版本。 - **数据未加锁保护**:对于会被多个线程同时访问的全局数据,要添加锁保护。 ```c #include <stdio.h> #include <pthread.h> pthread_mutex_t mutex; int shared_data = 0; void* thread_function(void* arg) { pthread_mutex_lock(&mutex); shared_data++; pthread_mutex_unlock(&mutex); return NULL; } int main() { pthread_t thread; pthread_mutex_init(&mutex, NULL); pthread_create(&thread, NULL, thread_function, NULL); pthread_join(thread, NULL); pthread_mutex_destroy(&mutex); return 0; } ``` ### 非法指针问题 - **空指针使用**:检查代码,避免使用空指针。在使用指针前,要确保其已正确初始化。 ```c #include <stdio.h> int main() { int* ptr = NULL; // 错误示例:使用空指针 // *ptr = 10; // 正确示例 int num = 10; ptr = &num; *ptr = 20; return 0; } ``` - **随意指针转换**:不要随意进行指针转换,若要访问特定结构或类型的内存,应先将内存拷贝到相应结构或类型中。 ### 堆栈溢出问题 避免使用大的局部变量,因为局部变量都分配在栈上,容易造成堆栈溢出。可考虑使用动态内存分配(如`malloc`)。 ```c #include <stdio.h> #include <stdlib.h> int main() { // 避免大的局部数组 // int large_array[100000]; // 使用动态内存分配 int* large_array = (int*)malloc(100000 * sizeof(int)); if (large_array != NULL) { // 使用数组 free(large_array); } return 0; } ``` ### 利用core文件调试 生成core文件后,使用`gdb`进行调试,定位问题代码行。 ```bash gdb ./your_program core-file-name ``` 在`gdb`中使用`bt`(backtrace)命令查看调用栈,找出问题所在。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值