C functions called as threads - Linux Linux user program

I am writing a linux daemon in C that gets values ​​from ADC using the SPI interface (ioctl). SPI (spidev - userland) seems a little unstable and freezes the daemon at arbitrary points in time.

I need to better control the calls of functions that receive values, and I thought about making it a thread that I could wait to finish and get the return value, and if it expires, suppose it freezes and kills it without this new thread filming the demon himself. Then I could apply measures such as resetting ADC before restarting. Is it possible?

A pseudo example of what I want to achieve:

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

  • pid = thread (get_adc_value (1, & value); // makes the thread calling the function
  • wait_until_finish (pid, timeout); // waiting for the function to complete / timesout
  • if (timeout) kill pid, start over // if the stream does not return at the specified time, kill it (it is frozen)
  • else, if the return value is sane, continue // if successful, process the return value of the variable and continue

Thanks for any contribution to the case, examples are highly appreciated!

+3
source share
4 answers

I would try using the pthreads library . I have used it for some of my projects with good success, and it gives you pretty good control over what works and when.

: http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

+2

.

, . alarm(TIMEOUT); , .

, . , . :

main(){for(;;){system("sampler");sleep(1);}}

FILE*fp=popen("supervise_sampler","r"); fp. : stdin , :

(while true;do sampler;sleep 1; done)|program

, , , :

sampler > data
program < data

, , .

, . , , , .

0

- , ioctl(), , , , . .

ioctl(), , ( , , ADC ).

I would advise to drop something, for example, a file with a timestamp, before calling it ioctl()in the well-known buggy interface. If your stream does not detach this file in xx in a few seconds, something else needs to restart the ADC.

I also agree using pthreads, if you need some sample code, just update your question.

0
source

Source: https://habr.com/ru/post/1745373/


All Articles