简体   繁体   English

用户定义的信号 1 问题,signal() 未处理 C 语言

[英]User defined signal 1 issue, signal() isn't handling C language

So, first of all, I know that this topic is talked a lot but I have been searching for more than one hour and I can't solve this issue.所以,首先,我知道这个话题很多,但我已经搜索了一个多小时,我无法解决这个问题。

I am doing a project and this part consists in sending a SIGUSR1 signal from a citizen process to a server process and the server has to handle SIGUSR1 and check citizen's id (previously read).我正在做一个项目,这部分包括将 SIGUSR1 信号从公民进程发送到服务器进程,服务器必须处理 SIGUSR1 并检查公民的 id(以前阅读)。

I have used function signal() for some other signals such as SIGALRM and SIGINT and it is handling fine.我已经将 function signal() 用于其他一些信号,例如 SIGALRM 和 SIGINT,并且处理得很好。 However, when it comes to signal(SIGUSR1,handler_usrUm);但是,当涉及到 signal(SIGUSR1,handler_usrUm); it isn't handling and shows User defined signal 1 .它没有处理并显示用户定义的信号 1

my citizen process我的公民程序

FILE * pidfile = fopen (FILE_PID_SERVIDOR, "r");
        int pidServer;                                             // reading server's pid from file
        fread(&pidServer, sizeof(int), 1, pidfile);
        printf("%d\n", pidServer);
        kill(pidServer, SIGUSR1); 
        pause();   

my server我的服务器

int main(){
/* other code*/    
    printf("I will wait send it here: %d\n", getpid());        // just to check what was the server's pid      
    pause(); //Waits for (SIGUSR1).
    signal(SIGUSR1,handler_usrUm);
}

Handler处理程序

void handler_usrUm(int sinal){
    printf("Got it!\n");  // We shouldn't use printf but it is just to check
/* some other code*/
}

Is there some incompatibility with signal() and SIGUSR1? signal() 和 SIGUSR1 是否存在一些不兼容? Do I have to use sigaction?我必须使用 sigaction 吗?

Regards问候

But you are positioning the catching behaviour after pausing:但是您在暂停后定位捕获行为:

pause(); //Waits for (SIGUSR1).
signal(SIGUSR1,handler_usrUm);

make the converse:反过来说:

signal(SIGUSR1,handler_usrUm);
pause(); //Waits for (SIGUSR1).

More: don't use the old API, prefer the use of sigaction which is more reliable and give you more control.更多:不要使用旧的API,更喜欢使用sigaction ,它更可靠,给你更多的控制权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM