0

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

pilcrow
  • 56,591
  • 13
  • 94
  • 135
improve
  • 31
  • 5
  • Note that you should avoid [calling `printf()` in a signal handler](https://stackoverflow.com/questions/16891019/how-to-avoid-using-printf-in-a-signal-handler/16891799#16891799). – Jonathan Leffler Apr 18 '21 at 09:23

1 Answers1

2

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.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • 1
    Thank you very much! It is solved. I know that sigaction is preferable because of the OS differences but once I am using just this scholar one it is fine . Also I have to study more about sigaction I am still not comfortable with it. – improve Apr 18 '21 at 09:11