我使用开放信号SIGUSR1和SIGUSR2来调用用户定义的函数。我已经为我的信号处理功能尝试了两个函数原型。两者都运行没有任何编译错误。打开信号调用函数时究竟发生了什么?该函数应该如何实现?
prototype1:
/***********************************************************/
/*** 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);
}
prototype2:
/***********************************************************/
/*** 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);
}
答案 0 :(得分:1)
信号处理程序的签名是:
typedef void (*sighandler_t)(int);
您的第一个示例使用空参数列表,该列表基本上不会声明任何预期参数。在这种情况下,编译器可能正在生成用于处理传递的“任何”参数的样板。
如果启用所有警告,您可能会从编译器中获得一些信息。另请注意,您可以创建处理程序static
,因为无论如何都要将指针传递给库,它不必从外部可见。
答案 1 :(得分:-3)
阅读本文:
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)
信号功能将动作确定为信号符号的动作。 有关定义信号处理函数的更多信息,请参阅定义处理程序。
甚至更好,google this:
signal handler function
或者更好的是,首先阅读有关信号处理的教科书。
如果您不熟悉C基本机制,我认为解决信号处理还为时过早。