SIGRTMIN和SIGRTMAX是否可以安全地用于信号处理程序?

时间:2021-05-28 20:50:13

The code I'm working with has a shared signal handler that switches on the signal number to handle it appropriately.

我正在使用的代码有一个共享信号处理程序,可以打开信号编号来适当地处理它。

I'm adding a custom signal. Something like this

我正在添加自定义信号。像这样的东西

static void signal_handler (int s)
{
    if ( s == SIGTERM ) clean_up () ;

    else if ( s == SIGRTMIN+1 ) ; // do nothing
}

SIGRTMIN and SIGRTMAX are #defines of function calls which initialize static data (in the implementations I've seen on google code search)

SIGRTMIN和SIGRTMAX是初始化静态数据的函数调用的#defines(在我在谷歌代码搜索中看到的实现中)

Signal handlers are supposed to be non-reentrant. Does the use of static data in the accessor to SIGRTMIN and SIGRTMAX make these macros unsafe to use in a signal handler?

信号处理程序应该是不可重入的。在SIGRTMIN和SIGRTMAX的访问器中使用静态数据是否会使这些宏在信号处理程序中不安全?

1 个解决方案

#1


4  

I don't know what implementation you are smoking, but in libc those functions seem to simply return a constant static variable most of the time.

我不知道你吸烟的实现是什么,但在libc中,这些函数似乎只是在大多数情况下简单地返回一个常量静态变量。

You are right, there is a possible race between the two calls to init(), but that simply just initializes a static int twice to the same constant, hardly a worry.

你是对的,两次调用init()之间可能存在竞争,但这只是将静态int初始化两次到同一个常量,几乎不用担心。

And, while the static variable is not really that constant, they tell you to only modify said variable at the start of your program(and I think only pthread really modifies it that much).

而且,虽然静态变量实际上不是那么常量,但是它们告诉你只在程序开始时修改所述变量(我认为只有pthread才能真正修改它)。

So no need to worry about these functions(from allocrtsig.c glibc 2.14).

因此无需担心这些功能(来自allocrtsig.c glibc 2.14)。

And, if you are really worried, just call SIGRTMIN once before you bind the signal handler. That will get the init() function out of the way.

而且,如果您真的很担心,只需在绑定信号处理程序之前调用SIGRTMIN一次。这将使init()函数不受影响。

/* Return number of available real-time signal with highest priority.  */
int __libc_current_sigrtmin (void)
{
#ifdef __SIGRTMIN
  if (!initialized)
    init ();
#endif
  return current_rtmin;
}
libc_hidden_def (__libc_current_sigrtmin)

/* Return number of available real-time signal with lowest priority.  */
int __libc_current_sigrtmax (void)
{
#ifdef __SIGRTMIN
  if (!initialized)
    init ();
#endif
  return current_rtmax;
}
libc_hidden_def (__libc_current_sigrtmax)

#1


4  

I don't know what implementation you are smoking, but in libc those functions seem to simply return a constant static variable most of the time.

我不知道你吸烟的实现是什么,但在libc中,这些函数似乎只是在大多数情况下简单地返回一个常量静态变量。

You are right, there is a possible race between the two calls to init(), but that simply just initializes a static int twice to the same constant, hardly a worry.

你是对的,两次调用init()之间可能存在竞争,但这只是将静态int初始化两次到同一个常量,几乎不用担心。

And, while the static variable is not really that constant, they tell you to only modify said variable at the start of your program(and I think only pthread really modifies it that much).

而且,虽然静态变量实际上不是那么常量,但是它们告诉你只在程序开始时修改所述变量(我认为只有pthread才能真正修改它)。

So no need to worry about these functions(from allocrtsig.c glibc 2.14).

因此无需担心这些功能(来自allocrtsig.c glibc 2.14)。

And, if you are really worried, just call SIGRTMIN once before you bind the signal handler. That will get the init() function out of the way.

而且,如果您真的很担心,只需在绑定信号处理程序之前调用SIGRTMIN一次。这将使init()函数不受影响。

/* Return number of available real-time signal with highest priority.  */
int __libc_current_sigrtmin (void)
{
#ifdef __SIGRTMIN
  if (!initialized)
    init ();
#endif
  return current_rtmin;
}
libc_hidden_def (__libc_current_sigrtmin)

/* Return number of available real-time signal with lowest priority.  */
int __libc_current_sigrtmax (void)
{
#ifdef __SIGRTMIN
  if (!initialized)
    init ();
#endif
  return current_rtmax;
}
libc_hidden_def (__libc_current_sigrtmax)