问题
Is there any way to check if a file is in use in C/C++? Or do I have to ALWAYS implement a lock/semaphore to prevent simultaneous access of any file by multiple threads/processes?
If we consider Linux, and the following scenario: I want to transfer,in chunks, the contents of a file stored in device A to another device B through RS-232 communication, using a pre-defined communication framework. When the request for this transfer comes, I want to verify the file is NOT being used by any process in device A, before sending a "Ready to Transfer : OK" response, following which I will start reading and transmitting the data in chunks. Is there a way to check file if is already in use without doing fopen/fclose on the said file?
回答1:
actually
fopen();
is the best way to find this out.
Do fopen() on the receiving end, if it is successful, send the "OK to receive" message.
来源:https://stackoverflow.com/questions/17732743/way-to-check-in-c-c-if-a-file-is-in-use