简体   繁体   中英

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).

I have used function signal() for some other signals such as SIGALRM and SIGINT and it is handling fine. However, when it comes to signal(SIGUSR1,handler_usrUm); it isn't handling and shows User defined signal 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? Do I have to use 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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