I hope you can give me some advise regarding my issue.
What I got is a web server running on a Raspberry Pi. On it a C-program writes a JPEG-File in a certain time inteval (1 second) like this:
fout = fopen("/tmp/image1.jpg", "w");
fwrite(jpgBuffer, jpgFileSize, 1, fout);
fclose(fout);
I access the image through my web browser: "192.168.178.xxx/tmp/image1.jpg" Most of the time the image is shown perfectly. However sometimes i see artifacts in the image.
My assumtion is, that the file is writen to during I request the image from the web browser. How can I avoid this behavior? Or how can I ensure, that the file is not written to while it is opened for reading during a request.
I read about file lock, but am not sure if this is the way to go. I know that I can set an exclusive lock with the flock function before writing to the file and unlock it afterwards. But I read also that the corresponding file open function has to set a read or shared lock for this method to work. However I don't know if the http request which I issue through the web browser sets such a read lock.
Any suggestions are appreciated.
Thanks a lot!