concurrency - Linux Webserver concurrent file handling (read/write) -


i hope can give me advise regarding issue.

what got web server running on raspberry pi. on c-program writes jpeg-file in time inteval (1 second) this:

fout = fopen("/tmp/image1.jpg", "w"); fwrite(jpgbuffer, jpgfilesize, 1, fout); fclose(fout); 

i access image through web browser: "192.168.178.xxx/tmp/image1.jpg" of time image shown perfectly. see artifacts in image.

my assumtion is, file writen during request image web browser. how can avoid behavior? or how can ensure, file not written while opened reading during request.

i read file lock, not sure if way go. know can set exclusive lock flock function before writing file , unlock afterwards. read corresponding file open function has set read or shared lock method work. don't know if http request issue through web browser sets such read lock.

any suggestions appreciated.

thanks lot!

write temporary file , rename "/tmp/image1.jpg". given rename pretty atomic, problem fixed.

a possible alternative approach use mandatory locks: writing process sets rw lock after opening file reading , writing, kernel block web server reading file until lock removed (either explicitly or implicitly when writing process exits). however, approach involves additional steps (filesystem must mounted "mand" option, group setgid attribute must set "g+s", "g-x"), also, usage of mandatory locks in linux frowned upon.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -