rmilville rmilville - 5 months ago 9
Linux Question

Use select like function on regular disk file

I have a computer wich logs some sensors data into 8 different files.
I developed a software that allows you to copy this data to another computer when you connect the two machines using an rj45 cable.

After retrieving data at my computer, I need to send it line by line of each file using a pseudo serial (using socat).

I created a program which uses nested for loops in order to check if data is ready in all the 8 files, and then extract a line and send it to puttySX.

Problem is CPU usage. A way to reduce it, is using blocking function to know if data is ready be to read or not but is there any function like select on sockets or serial ports but for such files?

If no, what should I do? Thanks

Answer

So,

I post to answer my question. Thanks to @yoones I found some trick to do this.

When a log file is created, I set a bool on true in a ini file looking like this

[CreatedFiles]
cli1=false
cli2=false
cli3=false
cli4=false
cli5=false
cli6=false
cli7=false
cli8=false 

Another program uses inotify to detect creation and modification in the corresponding files. Once there's some change it reads the ini file, process the data and when it finishes to read the data, it deletes the log file and write false in the ini file in the corresponding line.

Since I have to process several log files in the same time, each time I read a line, I verify my ini file to see if I have to start to process another log file as well so I can start multiple process in the same time.

I did a infinite while loop so when all processes are done, the program is back to a select call, waiting for some change and not consuming all CPU's resources.

I'm sorry if I'm not so clear, English is not my native language.

Thanks all for you reply and comments.

Comments