Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to write a function that takes in a timeout (posix C)
    primarykey
    data
    text
    <p>So I have an embedded Linux device that is connected to a motor controller via a serial port. I am writing an interface library which makes a lot of nice generic functions which other programs will call. One of which is a function to run the program that is currently on the controller's flash disk:</p> <pre><code>int run_motor_program(int serial_fd, char *label, timeout); </code></pre> <p>The general pseudocode for this function is:</p> <pre><code>call write(serial_fd, "start program at `label`") perform a couple read()'s / write()'s to check whether program has started on the motor controller do /* some stuff */ while(program is running AND timeout hasn't exceeded) If the timeout exceeded, kill motor and return timeout error </code></pre> <p>The timeout in the above function definition is used in case something goes wrong while running the program on the motor controller. If the motor controller gets stuck in a longer loop than expected, I need the ability to stop program.</p> <p>The only ways I know for keeping track of a timeout are:</p> <p>1) Calling gettimeofday() before and during the loop to see if elapsed time is > timeout value passed in</p> <p>2) Calling clock_gettime() and basically doing the same as 1.</p> <p>3) Using timer_create() before the loop and timer_getoverrun() in the loop to check if the time has elapsed (this seems to be the most elegant solution, but I can't seem to get timer_getoverrun() to work with SIGEV_NONE [I don't want to use signals]).</p> <p>Which of these (or if anyone has any other suggestions) is the best way to handle including a timeout in a function? I really only need resolution down to the millisecond.</p>
    singulars
    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.
    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