C functions invoked as threads - Linux userland program

Posted by Einar on Stack Overflow See other posts from Stack Overflow or by Einar
Published on 2010-05-13T23:03:10Z Indexed on 2010/05/13 23:14 UTC
Read the original article Hit count: 259

Filed under:
|
|
|
|

I'm writing a linux daemon in C which gets values from an ADC by SPI interface (ioctl). The SPI (spidev - userland) seems to be a bit unstable and freezes the daemon at random times.

I need to have some better control of the calls to the functions getting the values, and I was thinking of making it as a thread which I could wait for to finish and get the return value and if it times out assume that it froze and kill it without this new thread taking down the daemon itself. Also I could do other things like resetting the ADC before restarting. Is this possible?

Pseudo example of what I want to achieve:

(function int get_adc_value(int adc_channel, float *value) )

  1. pid = thread( get_adc_value(1,&value); //makes thread
  2. wait_until_finish(pid, timeout); //waits until function finishes/timesout
  3. if(timeout) kill pid, start over //if thread do not return in given time, kill it (it is frozen)
  4. else if return value sane, continue //if successful, handle return variable value and continue

Thanks for any input on the matter, examples highly appreciated!

© Stack Overflow or respective owner

Related posts about c

    Related posts about linux