0

I am attempting to create a non-responsive program handler to signal if a call is taking more than 1 second to respond. The abstraction will send a SIGUSR1 to the caller and will continue to wait. Below is snippets of code but I do not know how to make the process wait 1 second after a call doesn't respond for 1 second.This is a single process, with 1 thread. Using POSIX, any methods are available.

void USR1_handler(){
    printf("Call delayed...waiting...");
}

int foo()
{
    msg[0] = code_foo;
    write(sd, msg, 1);
    while (no_message_on_descriptor(sd))
        kill(getpid(), SIGUSR1);
    read(sd, msg, sizeof(msg));
    ...
}

int no_message_on_descriptor(int d)
{
    /*need help here */


    signal(SIGUSR1, USR1_handler);
    return(1);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Matt
  • 35
  • 5
  • 2
    I think you're going to need to provide some more context — a more precise description of what you're attempting to do. Am I right that you have a single process that is single-threaded, and it needs to execute some slow operations with a timeout of 1 second? And you're running on a POSIX system with `sigaction()` available? Do you have the `timer_create()` family of functions? Or `setitimer()`? – Jonathan Leffler Jun 27 '19 at 23:18
  • @JonathanLeffler This is a single-threaded, single porcess. Yes the timeout is 1 second or 1000ms, sigaction() is available and timer_create/setitimer() is allowed. – Matt Jun 27 '19 at 23:21
  • 1
    I'm going to point you at [How to chunk shell script input by time, not by size?](https://stackoverflow.com/questions/56314784/how-to-chunk-shell-script-input-by-time-not-by-size) because the answer there (from yours truly) shows how to use the various functions in a closely related context. There, if no data arrived for 10 seconds, the program was to stop reading; if data arrived, then wait for up to 5 seconds for more data to arrive, but don't wait longer than 10 seconds in total. It shows using `[gs]etitimer()` and `timer_create()` et al as separate compilation options — and `alarm()` too. – Jonathan Leffler Jun 27 '19 at 23:26
  • @JonathanLeffler I was hoping to use select(), like the structures found on the manual of select(), any suggestions? – Matt Jun 27 '19 at 23:34
  • 1
    Other than "don't"? Be wary of `select()`. See [Why is `select()` used in Linux?](https://stackoverflow.com/questions/14544621/) —— [Why `select()` always returns 0 after the first timeout?](https://stackoverflow.com/questions/3324078/) —— [What is the `nfds` from `select()` used for?](https://stackoverflow.com/questions/8695678/) —— [How can `select()` wait on regular file descriptors (non-sockets)?](https://stackoverflow.com/questions/11901884/), and a fair number of others. – Jonathan Leffler Jun 27 '19 at 23:40
  • 1
    AFAIK, `select()` tells you whether you could read or write some data without blocking — but it can only tell you that some data could be written, not that you can write all the data that you might want to write. If you decide to write 1 MiB of data on a currently empty pipe, you will be blocked until there's a process that reads the data (a pipe can hold up to 64 KiB before it's full, these days). But `select()` will say that it was possible to try the `write()`. I can see how to use `select()` to identify data to read, timing out if there is none, but dealing with writing is harder. – Jonathan Leffler Jun 27 '19 at 23:50

0 Answers0