不知道为什么,我在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();
}