免费注册 查看新帖 |

Chinaunix

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

线程 Segmentation fault (core dumped)[已解决] [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-02-12 21:17 |只看该作者 |倒序浏览
本帖最后由 crazyhadoop 于 2012-02-16 18:29 编辑

小弟初学linux, 根据书上写了个小程序
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

void printids(const char *);
void *start(void *);

pthread_t ntid;

void printids(const char *s)
{
        pid_t pid;
        pthread_t tid;
       
        pid = getpid();
        tid = pthread_self();
        printf("%s : pid is %u, tid is %u (0x%x)\n", s, pid, tid, tid);
}

void *start(void *p)
{
        printids("new thread");

        return ((void *)0);
}

void main()
{
        int err;

        err = pthread_create(&ntid, NULL, start(NULL), NULL);
        if(err != 0)
        {
                printf("error, cann't create new thread");
                exit(0);
        }
        printids("main thread");
        printf("start sleep\n");
        sleep(5);
        printf("end \n");
        exit(0);
}

编译后执行,结果如下
[root@localhost test]# gcc thread.c -o thread -lpthread
[root@localhost test]# ./thread
new thread : pid is 2442, tid is 4008711936 (0xeef01700)
main thread : pid is 2442, tid is 4008711936 (0xeef01700)
start sleep
Segmentation fault (core dumped)

当我把程序中  sleep(5)  注释掉后编译执行正常,结果为
[root@localhost test]# gcc thread.c -o thread -lpthread
[root@localhost test]# ./thread
new thread : pid is 2830, tid is 2600584960 (0x9b01c700)
main thread : pid is 2830, tid is 2600584960 (0x9b01c700)
start sleep
end

而且main thread的线程id,和新创建线程new thread 的线程id 相同, 我不明白程序哪里出错了

我的系统是centos6.2,gcc version 4.4.6

请高手指点

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
2 [报告]
发表于 2012-02-13 18:32 |只看该作者
两个问题:
1. pthread_create(&ntid, NULL, start(NULL), NULL)  改为 pthread_create(&ntid, NULL, start, NULL)
2. main函数中,请用pthread_join等待线程结束,不要用sleep这种不靠谱的方式
修改后,自然就正常了。

论坛徽章:
0
3 [报告]
发表于 2012-02-13 23:00 |只看该作者
pthread_create(&ntid, NULL, start(NULL), NULL)  改为 pthread_create(&ntid, NULL, start, NULL)后编译通不过,

man pthread_create内容如下

SYNOPSIS
       #include <pthread.h>

       int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

       Compile and link with -pthread.

论坛徽章:
11
技术图书徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
4 [报告]
发表于 2012-02-13 23:40 |只看该作者
回复 3# zzzhktk
好好检查下,我的环境和你完全一致,可以编译通过。不过你的代码还有其他问题,我顺便改了几处。
  1. #include <stdlib.h>
  2. #include <pthread.h>
  3. #include <stdio.h>
  4. #include <unistd.h>

  5. pthread_t ntid;

  6. void printids(const char *s)
  7. {
  8.         pid_t pid;
  9.         pthread_t tid;

  10.         pid = getpid();
  11.         tid = pthread_self();
  12.         printf("%s : pid is %u, tid is %u (0x%x)\n", s, pid, tid, tid);
  13. }

  14. void *start(void *p)
  15. {
  16.         printids("new thread");

  17.         return ((void *)0);
  18. }

  19. void main()
  20. {
  21.         int err;

  22.         printids("main thread");

  23.         err = pthread_create(&ntid, NULL, start, NULL);
  24.         if(err != 0)
  25.         {
  26.                 printf("error, cann't create new thread");
  27.                 exit(0);
  28.         }

  29.         printf("start sleep\n");
  30.         pthread_join(ntid, NULL);
  31.         printf("end \n");

  32.         exit(0);
  33. }
复制代码

论坛徽章:
0
5 [报告]
发表于 2012-02-14 13:59 |只看该作者
pthread_create(&ntid, NULL, start(NULL), NULL)  改为 pthread_create(&ntid, NULL, start, NULL)后编译能够编译通过,可能是昨天我有地方弄错了吧,不好意思

谢谢

论坛徽章:
1
天蝎座
日期:2013-12-06 18:23:58
6 [报告]
发表于 2012-02-16 18:30 |只看该作者
第三个参数要是函数指针。

论坛徽章:
0
7 [报告]
发表于 2012-02-20 16:28 |只看该作者
可是这个函数指针指向的函数有参数啊,怎么给这个函数传参呢

论坛徽章:
0
8 [报告]
发表于 2012-02-25 14:02 |只看该作者
不好意思,这个问题太弱智了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP