的信号处理的pthreads中 [英] Signal handling in pthreads

查看:113
本文介绍了的信号处理的pthreads中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个pthread的,并且安装了,方式与我们在做的main()函数内部信号处理程序。线程的信号处理程序是一个独立的功能。出人意料的是,它不工作,这是线程的信号处理程序是不是能赶上信号。

下面是code:

 的#include< pthreads.h中>
#包括LT&;&unistd.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&;&stdio.h中GT;
#包括LT&;&signal.h中GT;typedef结构数据
{
 CHAR名称[10];
 INT年龄;
}数据;无效sig_func(INT SIG)
{
 的printf(夹缝信号数:%d \\ n,SIG);
 信号(SIGSEGV,sig_func);
}无效FUNC(数据* P)
{
 的printf(这是从线程函数\\ n);
 信号(SIGSEGV,sig_func); //注册内螺纹信号处理程序
 的strcpy(对GT;的名字,先生的Linux。);
 P->年龄= 30;
 睡眠(2); //睡眠捕捉信号
}诠释的main()
{
 TID的pthread_t;
 pthread_attr_t ATTR;
 数据* PTR; pthread_attr_init(安培; attr指示);
 在pthread_create(安培; TID,&安培; attr指示,(无效*)FUNC,PTR);
 pthread_kill(TID,SIGSEGV); 在pthread_join(TID,NULL);
 的printf(名称:%s \\ n,ptr->名);
 的printf(年龄:%d个\\ N,ptr->年龄);
}

输出:


  

分段故障(这意味着该信号不被处理程序捕获)



解决方案

有几个问题你code:


  • PTR 将不会被初始化,所以所有的 ptr-> 部分将引起程序崩溃

  • 您正在调用 pthread_kill()立即,很可能信号处理程序已经安装之前,以及在一个线程(有明确的行为)

  • 您拨打的printf()从信号处理程序,这是不保证工作(见人7信号对于安全功能的列表)

这将工作好了很多,但你还是会需要适当的线程同步,和其他地方一样说,你应该使用的sigaction()

 的#include< pthreads.h中>
#包括LT&;&unistd.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&;&stdio.h中GT;
#包括LT&;&signal.h中GT;typedef结构数据
{
 CHAR名称[10];
 INT年龄;
}数据;无效sig_func(INT SIG)
{
 写(1,中招信号11 \\ n,17);
 信号(SIGSEGV,sig_func);
}无效FUNC(数据* P)
{
 fprintf中(标准错误,这是从线程函数\\ n);
 的strcpy(对GT;的名字,先生的Linux。);
 P->年龄= 30;
 睡眠(2); //睡眠捕捉信号
}诠释的main()
{
 TID的pthread_t;
 pthread_attr_t ATTR;
 数据D;
 数据* PTR =和D; 信号(SIGSEGV,sig_func); //才去注册的多线程信号处理程序
 pthread_attr_init(安培; attr指示);
 在pthread_create(安培; TID,&安培; attr指示,(无效*)FUNC,PTR);
 睡眠(1); //留出时间,初始化
 pthread_kill(TID,SIGSEGV); 在pthread_join(TID,NULL);
 fprintf中(标准错误,名称:%s \\ n,ptr->名);
 fprintf中(标准错误,年龄数:%d \\ n,ptr->年龄);
}

修改:在安装主线程sighandler

I have created a pthread, and installed a signal handler inside that, same way as we do in main( ) function. The thread's signal handler is a separate function. Surprisingly, it is not working, that is the thread's signal handler is not able to catch signals.

Here is the code:

#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <signal.h>

typedef struct data
{
 char name[10];
 int age;
}data;

void sig_func(int sig)
{
 printf("Caught signal: %d\n",sig);
 signal(SIGSEGV,sig_func);
}

void func(data *p)
{
 printf("This is from thread function\n");
 signal(SIGSEGV,sig_func); // Register signal handler inside thread
 strcpy(p->name,"Mr. Linux");
 p->age=30;
 sleep(2); // Sleep to catch the signal
}

int main()
{
 pthread_t tid;
 pthread_attr_t attr;
 data *ptr;

 pthread_attr_init(&attr);
 pthread_create(&tid,&attr,(void*)func,ptr);
 pthread_kill(tid,SIGSEGV);

 pthread_join(tid,NULL);
 printf("Name:%s\n",ptr->name);
 printf("Age:%d\n",ptr->age);
}

Output:

Segmentation fault (which means the signal is not caught by handler)

解决方案

There are several problems with your code:

  • ptr is not initialised, so all the ptr-> parts will crash the program
  • you are calling pthread_kill() immediately, very likely before the signal handler has been installed, and in a thread (which has unspecified behaviour)
  • you call printf() from a signal handler, which is not guaranteed to work (see man 7 signal for a list of safe functions)

This will work a lot better, though you'd still need proper thread synchronisation, and as stated elsewhere, you should use sigaction():

#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <signal.h>

typedef struct data
{
 char name[10];
 int age;
}data;

void sig_func(int sig)
{
 write(1, "Caught signal 11\n", 17);
 signal(SIGSEGV,sig_func);
}

void func(data *p)
{
 fprintf(stderr, "This is from thread function\n");
 strcpy(p->name,"Mr. Linux");
 p->age=30;
 sleep(2); // Sleep to catch the signal
}

int main()
{
 pthread_t tid;
 pthread_attr_t attr;
 data d;
 data *ptr = &d;

 signal(SIGSEGV,sig_func); // Register signal handler before going multithread
 pthread_attr_init(&attr);
 pthread_create(&tid,&attr,(void*)func,ptr);
 sleep(1); // Leave time for initialisation
 pthread_kill(tid,SIGSEGV);

 pthread_join(tid,NULL);
 fprintf(stderr, "Name:%s\n",ptr->name);
 fprintf(stderr, "Age:%d\n",ptr->age);
}

Edit: install sighandler in main thread

这篇关于的信号处理的pthreads中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆