When multiple processes access the r/w configuration file (in. ini format), the file is always damaged.

Source: Internet
Author: User

How can this problem be solved? Decide to use the file lock. There are BSD and POSIX File locks, as follows: # if (pai_flock_bsd = 1) ret = flock (fd, LOCK_SH); if (ret! = 0) // can not get R_lock when writting {ret =-100; goto End;} # elif (pai_flock_posix = 1) ret = _ SimpleSetPosixLock (fd, F_RDLCK ); if (ret <0) // can not get R_lock when writting {ret =-100; goto End;} # After endif adds a file lock, this still happens. It was suspected that there was a problem with the file lock itself. The final result is that the file size obtained by stat is incorrect. Replace/stat (pconfig-> fileName, & sb) with ret = fstat (fd, & sb. According to the file name to stat file size, it is possible that the file is being written by another process, then the file is read into the memory according to the incorrect size, write back the file at commit, then an error occurs. The correct operation procedure is: open the file --> apply for a read lock --> fstat size upon success, and read the file into the memory according to the size. In this way, the file will not be damaged. Of course, the API provided by the INI file operation itself also has a defect. When ret fails to return, no exit parameters are set. When the return value fails, the export parameter is also set to an invalid value. # Define NULL_STR '\ 0' # define NULL_VAL 0xFFFFIni configuration file is written in ansi c and easy to transplant. Supports multi-process access with high efficiency. If you have any need, you can leave a message, send a text message or email to me.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.