I want to continuously execute a C program on my computer. It doesn't actually have to "do" anything until it identifies an event
though.
For example, the C program could be written with a do while
loop that never exits. Inside the loop, there could be an if else
condition that listens for an event
. Lets say "if
a file exists in a directory, then open the file and do something, else
take a break and sleep(60)
for a minute".
Of course this functionality can be implemented outside the C program. I could run the same idea above in the shell script..."if
a file exists in a directory, then run C program, else
take a break and sleep(60)
for a minute". I could also write a shell script to execute the C program if a file exists and run the shell program with a crontab
or some other scheduler.
If I didn't run sleep
or I set the crontab
to a very high frequency, then obviously my response time would improve. While that would be ideal, I have my doubts that is a safe and proper way to do this. I basically want my program to always be ready for action...an event
occurs, get to work!
Since I've never implemented such a program, my question is, is there a proper and safe way of creating an "event-listener" that serves this type of functionality? C vs shell approach? Other ideas?
man select
, andman epoll
.man inotify
. Can be used to check if a specific files gets created in the directory though.