Note that there are some explanatory texts on larger screens.

plurals
  1. POC: Writing a proper time-out
    primarykey
    data
    text
    <p>Upon closely scouring through resources, I'm still not entirely sure how to write a proper and usable timer function in C. I am not working with threads (or parallelizable code). I simply want to write a stopwatch function that I can use to trigger a bit of code after a small amount of time has passed.</p> <p>This is a very common use of a timer, in the situation of "time-out", where I have a client-server set up where the client is sending some data (UDP style with sendto(...) and recvfrom(...)). I have written my system so that the client sends a chunk of data in a packet struct I have defined, and the server processes it via CRC then sends back an acknowledgement packet (ACK) that the msg was received uncorrupted. However, I want to implement a time-out, where if the client does not receive an ACK in a certain period of time, the client resends the data chunk (of course the server is rigged to check for duplicates). I want to nest this bit of timer code in the client, and for some reason do not think this should be so difficult.</p> <p>I have dug up old signal handling code from work I had done long ago, as this seems to be the only way I commonly see mentioned as a solution, can someone please guide me as to how I can use the following signal handling code to not just receive a timed signal but trigger an action of some sort. Conceptually, I feel it would be: "send data, start timer, after timer expires execute a resend, reset timer...repeat until that ACK received". Better yet, would be an easier way of writing a timer function, but it doesn't look like there's much hope for that given C is a low-level language....</p> <pre><code>#include &lt;sys/time.h&gt; #include &lt;errno.h&gt; #include &lt;stdio.h&gt; #include &lt;signal.h&gt; extern char *strsignal(int sig); void timer_handler(int a) { // handle signal printf("&gt;&gt;&gt;&gt; signal caught\n"); printf("&gt;&gt;&gt;&gt; int parameter = %s\n", (char*) strsignal(a)); } int main(int argc, char* argv[]) { int retval; struct itimerval timerValue; struct itimerval oldTimerValue; struct sigaction action; action.sa_handler = &amp;timer_handler; action.sa_flags = SA_NODEFER; // initialize timer parameters: expires in 5 seconds timerValue.it_interval.tv_sec = 5; timerValue.it_interval.tv_usec = 0; timerValue.it_value.tv_sec = 5; timerValue.it_value.tv_usec = 0; // install signal handler to catch SIGALRM //signal(SIGALRM, timer_handler); sigaction(SIGALRM, &amp;action, NULL); retval = setitimer(ITIMER_REAL, &amp;timerValue, &amp;oldTimerValue); if (-1 == retval) perror("Could not set timer"); while(1); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload