将参数传递给C中的信号处理程序 [英] Pass arguments to signal handler in C

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

问题描述

如何将参数(例如,指向结构的指针)传递给信号处理程序? 我正在编写一个多线程应用程序,所以不能使用全局变量

How can I pass arguments (e.g. a pointer to a struct) to a signal handler? I'm writing a multithread application, so I cannot use global variables

我将计时器与每个线程相关联.当计时器到期时,我必须更新一个结构(每个线程都有一个不同的结构).

I associate a timer to each thread. When timer expires I have to update a struct (each thread has a different struct).

我该怎么办?

推荐答案

POSIX计时器允许使用struct sigevent结构来指定如何将过期通知您的进程的详细信息. /linux.die.net/man/7/sigevent"rel =" nofollow noreferrer> sigevent(7).

POSIX timers allow to specify the details of how your process will be notified of an expiration using the struct sigevent structure documented in sigevent(7).

要将上下文信息传递给生成信号的信号处理程序,您可以设置.sigev_value.sival_ptr成员,然后您的信号处理程序将可以检索该成员(您需要通过.sa_sigaction设置信号处理程序struct sigaction的成员,同时确保结构的.sa_flagsSA_SIGINFO进行或运算.

To pass context info to the siggnal handler for the generated signal, you can set the .sigev_value.sival_ptr member, which your signal handler will then be able to retrieve (you'll need to set the signal handler via the .sa_sigaction member of struct sigaction, while making sure the structure's .sa_flags is ORred with SA_SIGINFO).

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

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