亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 10099 | 回復(fù): 7
打印 上一主題 下一主題

線程 Segmentation fault (core dumped)[已解決] [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2012-02-12 21:17 |只看該作者 |倒序瀏覽
本帖最后由 crazyhadoop 于 2012-02-16 18:29 編輯

小弟初學(xué)linux, 根據(jù)書上寫了個小程序
#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);
}

編譯后執(zhí)行,結(jié)果如下
[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)

當(dāng)我把程序中  sleep(5)  注釋掉后編譯執(zhí)行正常,結(jié)果為
[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,和新創(chuàng)建線程new thread 的線程id 相同, 我不明白程序哪里出錯了

我的系統(tǒng)是centos6.2,gcc version 4.4.6

請高手指點

論壇徽章:
11
技術(shù)圖書徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
2 [報告]
發(fā)表于 2012-02-13 18:32 |只看該作者
兩個問題:
1. pthread_create(&ntid, NULL, start(NULL), NULL)  改為 pthread_create(&ntid, NULL, start, NULL)
2. main函數(shù)中,請用pthread_join等待線程結(jié)束,不要用sleep這種不靠譜的方式
修改后,自然就正常了。

論壇徽章:
0
3 [報告]
發(fā)表于 2012-02-13 23:00 |只看該作者
pthread_create(&ntid, NULL, start(NULL), NULL)  改為 pthread_create(&ntid, NULL, start, NULL)后編譯通不過,

man pthread_create內(nèi)容如下

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
技術(shù)圖書徽章
日期:2014-03-01 14:44:34天蝎座
日期:2014-05-21 22:11:59金牛座
日期:2014-05-30 17:06:14
4 [報告]
發(fā)表于 2012-02-13 23:40 |只看該作者
回復(fù) 3# zzzhktk
好好檢查下,我的環(huán)境和你完全一致,可以編譯通過。不過你的代碼還有其他問題,我順便改了幾處。
  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. }
復(fù)制代碼

論壇徽章:
0
5 [報告]
發(fā)表于 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 [報告]
發(fā)表于 2012-02-16 18:30 |只看該作者
第三個參數(shù)要是函數(shù)指針。

論壇徽章:
0
7 [報告]
發(fā)表于 2012-02-20 16:28 |只看該作者
可是這個函數(shù)指針指向的函數(shù)有參數(shù)啊,怎么給這個函數(shù)傳參呢

論壇徽章:
0
8 [報告]
發(fā)表于 2012-02-25 14:02 |只看該作者
不好意思,這個問題太弱智了
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP