Rock姜 2022-11-05 15:04 采纳率: 60%
浏览 14
已结题

linux平台,调用函数pthread_attr_getstack,报错。

不知道为什么,我在ubuntu22.04上编译c/c++代码。本不应该出现SIGSEGV错误,但是总是在调用函数pthread_attr_getstack的时候报这个错误。

//
// Created by rockjiang on 22-10-31.
//

#include "iostream"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "malloc.h"
#include <sys/mman.h>
#include "signal.h"

static unsigned int index = 0;

void sig_handler(int sig) {
    switch(sig) {
        case SIGSEGV:
            printf("GOT SIGSEGV signal %p\n", pthread_self());
            break;
        case SIGBUS:
            printf("GOT SIGBUS signal %p\n", pthread_self());
            break;
        default:
            printf("UNKNOW SIG\n");
            break;
    }
    exit(EXIT_FAILURE);
}

void *thread_function(void *arg) {
    pthread_t cur_id = pthread_self();
    pthread_attr_t attr;
    int rslt = pthread_getattr_np(cur_id, &attr);
    void* bottom;
    size_t* size;
//    if (pthread_attr_getstack(&attr, &bottom, size) != 0) {
//        perror("Cannot locate current stack attributes!");
//    }
    size_t guard_size = 0;
    rslt = pthread_attr_getguardsize(&attr, &guard_size);
    if (rslt != 0) {
        perror("pthread_attr_getguardsize failed");
    }
}

void simpleTest() {
    signal(SIGSEGV, sig_handler);
    size_t pagesize = sysconf(_SC_PAGESIZE);
    size_t guardsize = pagesize;
    size_t stacksize = pagesize;
    printf("pagesize:%d\n", pagesize);
    printf("sizeof(char*):%d\n", sizeof(char*));
    pthread_attr_t attr_t;
    pthread_t tid;
    pthread_attr_init(&attr_t);
    int ret = pthread_attr_setguardsize(&attr_t, pagesize);
    printf("ret=%d\n", ret);

    pthread_attr_setdetachstate(&attr_t, PTHREAD_CREATE_JOINABLE);

    pthread_create(&tid, &attr_t, thread_function, NULL);

    pthread_attr_getguardsize(&attr_t, &guardsize);
    pthread_attr_getstacksize(&attr_t, &stacksize);
    char* bottom;
    size_t* size;
    printf("11111\n");
    if (pthread_attr_getstack(&attr_t, (void**)bottom, size) != 0) {
        perror("Cannot locate current stack attributes!");
    }
    printf("tid=%p guardsize:%d stacksize:%d\n", pthread_self(), guardsize, stacksize);

    pthread_join(tid, NULL);



}

int main() {
    simpleTest();
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 已结题 (查看结题原因) 11月6日
    • 创建了问题 11月5日

    悬赏问题

    • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
    • ¥15 这种微信登录授权 谁可以做啊
    • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
    • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
    • ¥15 网络设备配置与管理这个该怎么弄
    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!