为什么我在信号时出现分段错误

Why I get segmentation fault when I the signal

本文关键字:分段 错误 信号 为什么      更新时间:2023-10-16

我在处理信号SIGALARM时遇到分段错误。

这是我的代码。

class UThread{
public:
UThread(){}
~UThread(){
signal(SIGALRM,SIG_IGN);
for(size_t i=0;i<thread_list.size();i++){
delete thread_list[i]->uc_stack.ss_sp;
delete thread_list[i];
thread_list[i]=NULL;
}
}
int create_thread(void (*callback)(void *),void *args){
ucontext_t *new_context= new ucontext_t;
assert(getcontext(new_context) != -1);
new_context->uc_stack.ss_sp=new char[1024];
new_context->uc_stack.ss_size=1024;
new_context->uc_flags=0;
new_context->uc_link=0;
assert(new_context->uc_stack.ss_sp!=NULL);
makecontext(new_context,(void (*)())callback,1,args);//make a context
size_t n=thread_list.size();
//find a position to save the pointer.
int i=0;
for(;i<n;i++){
if(thread_list[i]==NULL)
break;
}
if(i<n)
thread_list[i]=new_context;
else{
thread_list.push_back(new_context);
}
return i;
}
void start_thread(){
ucontext_t *main_context= new ucontext_t;
getcontext(main_context);
thread_list.push_back(main_context);
struct sigaction sa;
sa.sa_handler=schedule;
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask,SIGALRM);
sigaction(SIGALRM,&sa,NULL);
struct itimerval tick;
tick.it_value.tv_sec = 0;
tick.it_value.tv_usec = 1;
tick.it_interval.tv_sec = 0;
tick.it_interval.tv_usec = 1000;//send a SIGALRM
setitimer(ITIMER_REAL,&tick,NULL);
}
private:
static void schedule(int signo){//deal with the signal
int last_id=current_id;
int n=thread_list.size();
int i=rand()%n;//get a random number.
while(thread_list[i]==NULL){
i=rand()%n;
}
current_id=i;
if(thread_list[last_id]==NULL){//if it has been cancelled,just setcontext.
setcontext(thread_list[i]);
return;
}
swapcontext(thread_list[last_id],thread_list[current_id]);//swap the context.
}
static int current_id;
static vector<ucontext_t*> thread_list;
};
vector<ucontext_t*> UThread::thread_list;
int UThread::current_id=0;

这是类定义的。如果我调用两个以上的函数,它会得到分段错误。

void f2(void *){
const char *buf="I am f2.n";;
while(true){
write(STDOUT_FILENO,buf,strlen(buf));
}
}
void f3(void *){
const char *buf="I am------- f3.n";
while(true){
write(STDOUT_FILENO,buf,strlen(buf));
}
}
int main(){
UThread t;
t.start_thread();
int thread_id2=t.create_thread(f2,NULL);
int thread_id3=t.create_thread(f3,NULL);
const char *buf="I am main.n";
while(true){
write(STDOUT_FILENO,buf,strlen(buf));
}
return 0;
}

这是函数调用

如果我删除主函数中的t.create_thread(f3,NULL);之一,它将成功运行而不会出错。但是如果我在主函数中添加两个t.create_thread(func,NULL);swapcontext(thread_list[last_id],thread_list[current_id]);完成后会出现分段错误。

显示的代码安装了一个信号处理程序:

struct sigaction sa;
sa.sa_handler=schedule;
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask,SIGALRM);
sigaction(SIGALRM,&sa,NULL);

schedule()函数尝试访问各种C++库容器,并调用其方法:

static void schedule(int signo){//deal with the signal
int last_id=current_id;
int n=thread_list.size();

这里的thread_list参考是一个std::vector

C++库容器都不是信号安全的。尝试在信号处理程序中访问它们是未定义的行为。您的分段错误是不可避免的结果。最重要的是,信号处理程序唯一可以安全地执行的操作是调用有限数量的系统调用。它甚至不能打电话给mallocnew.这不仅适用于SIGALRM,也适用于任何信号。

底线是你试图在信号处理程序中做的事情都无法完成。

您表示您使用的是 Linux。信号安全(7) Linux 手册页列出了唯一可以从信号处理程序进行的系统调用。如果系统调用不在列表中,则无法从信号处理程序调用它。请注意,该列表中没有任何与C++相关的内容。C++库中没有任何内容是信号安全的。就这样,故事结束,句号。

但是,在 Linux 上,有一种方法可以使用信号文件描述符在C++代码中安全地处理信号。查看该手册页的内容,也许您可以调整代码以使用信号文件描述符。