I am writing Linux ANSI C cgi-bin server program with simultaneous access to files.
-
Is it possible to distinguish between file existence and file locking?
I can’t find the answer with Google.
I’d like to write a program which tries to open file for a few seconds if fd<0
(thinking that the file is locked for a while).
But if the file does not exist it’s fd also <0. So the program will waste time waiting. -
Suppose a few threads try to append to the same file with no locking.
One tries to add “AAAA”, another – “BBBB”.
Can the result file be like “AABBAABB”?
Or it will always be like AAAABBBB or BBBBAAAA?
Or the result is unpredictable?
Am assuming IEEE Std 1003.1-2001 might defer to the ISO C standard…
In case the fopen fails i.e. fd < 0 then the system sets some error codes…
you can check those error codes. In case of file non-existent, the returned error would be
ENOENT
A component of filename does not name an existing file or filename is an empty string.
For more reference visit:
http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html
For point 2: I have been doing logging of certain data in my system by more than 100 processes writing to single file simultaneously but have never seen a merger of records(file is always opened in append mode). i.e. its always like AAAABBBB