0

This is the function I use to make process A sleep a random amount of time


int sd_sleepRandomTime() {
    debug("SD15", "<");
   
    srand(time(NULL));
    int sec = rand()%((MAX_PROCESSAMENTO)-MIN_PROCESSAMENTO) + MIN_PROCESSAMENTO;
    success("SD15", "%d", sec);
    sleep(sec);
    printf("here");
    debug("SD15", ">");
    return 0;
}

Here is the SIGINT handler I use in process B to send a SIGHUP to process C

void trataSinalSIGINT(int sinalRecebido) {
    debug("C9", "<");
    kill(pidServidor, SIGHUP);
    success("C9", "Processo Cancelado pelo Cliente");
    
    debug("C9", ">");
    exit(0);
    return;
}

And here is the SIGHUP handler in process C to SIGTERM process A (it only matters starting from "//S11.3" I think)

void trataSinalSIGHUP(int sinalRecebido, siginfo_t *info, void *context) {
    debug("S11", "<");
   
    success("S11", "Cancel");
    //S11.1
    int pid = info->si_pid;
    success("S11.1","Cancelamento enviado pelo Processo %d", pid);
    //S11.2
    int pid_sv=-1;
    int ch = 0;
    for(int i = 0; i!= NUM_PASSAGENS; i++){

        if(lista_passagens[i].pid_cliente==pid){
            ch=1;
            pid_sv=lista_passagens[i].pid_servidor_dedicado;
            success("S11.2", "Cancelamento %d", pid_sv);
            
        }
    }
    if(ch==0){
        
        error("S11.2","");
    }

    //S11.3
    kill(pid_sv, SIGTERM);
    success("S11.3", "Cancelamento Shutdown %d", pid_sv);
    debug("S11", ">");
}

For some reason once I Ctrl+C in process B the only thing showing in terminal is Segmentation fault and after that it just completes process A like nothing had happened and without even entering the SIGHUP handler in process C

  • 1
    You're calling several functions that aren't async-signal safe in your signal handlers. – Barmar Apr 25 '22 at 19:32
  • 1
    It's not clear to me whether that's true, @Barmar. I see a bunch of user functions being called that appear to be intended to emit output, but whether those are async-signal-safe depends on what standard library functions they themselves call. If they use `write()` to output the messages then they might effectively be async-signal-safe. – John Bollinger Apr 25 '22 at 19:54
  • 1
    We need a [mre] to debug this for you. However, if the problem is represented somewhere in the code that has been provided at all, then my first guess would be the `for` loop in process C's `trataSinalSIGHUP()` overrunning the bounds of `lista_passagens`. – John Bollinger Apr 25 '22 at 20:07
  • 1
    If you are wanting to see the output from your `printf()` calls, at minimum end the format string with a newline. For maximal confidence, use `fflush(stdout);` after the call to `printf()`, especially if you refuse to add newlines at the end. Also note the rules in [How to avoid calling `printf()` in a signal handler?](https://stackoverflow.com/q/16891019/15168) – Jonathan Leffler Apr 25 '22 at 20:07

0 Answers0