【问题标题】:signature of signal handling function in cc中信号处理函数的签名
【发布时间】:2014-01-22 15:58:11
【问题描述】:

我正在使用开放信号 SIGUSR1 和 SIGUSR2 来调用用户定义的函数。我已经为我的信号处理函数尝试了两个函数原型。两者都运行没有任何编译错误。当打开信号调用函数时究竟会发生什么?功能应该如何实现?

原型1:

/***********************************************************/
/*** Sample program demonstrating the sending of signals ***/
/*** Written by Abhijit Das, 17-Jan-2014                 ***/
/***********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

/* The signal handler for the child process */
void childSigHandler (int sig)
{
    //int sig;
   if (sig == SIGUSR1) {
      printf("+++ Child : Received signal SIGUSR1 from parent...\n");
      sleep(1);
   } else if (sig == SIGUSR2) {
      printf("+++ Child : Received signal SIGUSR2 from parent...\n");
      sleep(5);
   }
   exit(0);
}

int main ()
{
   int pid;

   pid = fork();                                   /* Spawn the child process */
   if (pid) {
                                                            /* Parent process */
      int t;
      srand((unsigned int)time(NULL));
      t = 2 + rand() % 4;
      printf("+++ Parent: Going to sleep for %d seconds\n", t);
      sleep(t);       /* Sleep for some time before sending a signal to child */
      t = 1 + rand() % 2;
      printf("+++ Parent: Going to send signal SIGUSR%d to child\n", t);
      kill(pid, (t == 1) ? SIGUSR1 : SIGUSR2);        /* Send signal to child */
      wait(NULL);                                   /* Wait for child to exit */
      printf("+++ Parent: Child exited\n");

   } else {
                                                             /* Child process */
      signal(SIGUSR1, childSigHandler);           /* Register SIGUSR1 handler */
      signal(SIGUSR2, childSigHandler);           /* Register SIGUSR2 handler */
      while (1) sleep(1);     /* Sleep until a signal is received from parent */

   }

   exit(0);
}

原型2:

/***********************************************************/
/*** Sample program demonstrating the sending of signals ***/
/*** Written by Abhijit Das, 17-Jan-2014                 ***/
/***********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

/* The signal handler for the child process */
void childSigHandler ()
{
int sig;
   if (sig == SIGUSR1) {
      printf("+++ Child : Received signal SIGUSR1 from parent...\n");
      sleep(1);
   } else if (sig == SIGUSR2) {
      printf("+++ Child : Received signal SIGUSR2 from parent...\n");
      sleep(5);
   }
   exit(0);
}

int main ()
{
   int pid;

   pid = fork();                                   /* Spawn the child process */
   if (pid) {
                                                            /* Parent process */
      int t;
      srand((unsigned int)time(NULL));
      t = 2 + rand() % 4;
      printf("+++ Parent: Going to sleep for %d seconds\n", t);
      sleep(t);       /* Sleep for some time before sending a signal to child */
      t = 1 + rand() % 2;
      printf("+++ Parent: Going to send signal SIGUSR%d to child\n", t);
      kill(pid, (t == 1) ? SIGUSR1 : SIGUSR2);        /* Send signal to child */
      wait(NULL);                                   /* Wait for child to exit */
      printf("+++ Parent: Child exited\n");

   } else {
                                                             /* Child process */
      signal(SIGUSR1, childSigHandler);           /* Register SIGUSR1 handler */
      signal(SIGUSR2, childSigHandler);           /* Register SIGUSR2 handler */
      while (1) sleep(1);     /* Sleep until a signal is received from parent */

   }

   exit(0);
}

【问题讨论】:

  • 不过,在这两种情况下,它都应该是int main (void)
  • 如果你还没有掌握基本的C语言,尝试去信号捕捉是徒劳的。谷歌搜索,先看一些教科书。

标签: c operating-system signals signal-handling


【解决方案1】:

the documentation

信号处理程序的签名是:

typedef void (*sighandler_t)(int);

您的第一个示例使用了一个空参数列表,它基本上没有声明任何预期的参数。在这种情况下,编译器可能正在生成样板来处理传递的“任何”参数。

如果您启用所有警告,您可能会从编译器中得到一些东西。另请注意,您可以将处理程序设为static,因为无论如何您将指针传递给库,它不必从外部可见。

【讨论】:

  • 那么为什么当我们没有向信号处理程序传递参数时程序仍在执行?
  • 当我将 3 个整数传递给函数时,它会生成一个警告。虽然在没有争论的情况下什么都没有。如何解释这种行为?
【解决方案2】:

阅读:

http://www.gnu.org/software/libc/manual/html_node/Basic-Signal-Handling.html

在本页的前 10 行中,您有答案。
由于我们这里的好朋友认为我不够明确,我会复制粘贴给你:

信号函数提供了一个简单的接口,用于为特定信号建立动作。函数和相关的宏在头文件 signal.h 中声明。

——数据类型:sighandler_t

这是信号处理函数的类型。信号处理程序采用一个整数参数来指定信号编号,并且返回类型为 void。所以,你应该像这样定义处理函数:

void handler (int signum) { ... }

此数据类型的名称 sighandler_t 是 GNU 扩展。

 — Function: sighandler_t signal (int signum, sighandler_t action)

信号函数将动作建立为信号符号的动作。 有关定义信号处理函数的更多信息,请参阅定义处理程序。

或者更好的是,谷歌这个:

signal handler function

或者更好的是,先阅读有关信号处理的教科书。

如果您不熟悉 C 的基本机制,我认为现在处理信号处理还为时过早。

【讨论】:

  • 这更适合作为评论。
  • @alk 好吧,我也可以复制粘贴 GNU 文档,但它不会增加任何价值。
  • 关于 OP 正在努力解决的具体问题的简短摘要怎么样? SO 此处的答案应直接指代一个问题。
  • @alk 这正是 GNU 页面所做的。在前 3 行中有一个 sig 处理程序原型的定义。
  • 那么为什么不直接引用这三行,添加一个将它们链接到 OPs 问题的介绍。然后用你得到报价单的链接完成。
猜你喜欢
  • 1970-01-01
  • 2011-09-23
  • 2011-01-29
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多