b0rsuk wrote:
If I use this function in a "while" loop, could it possibly end up with 20 million files open ?
Not unless you also change the current working directory 20 million times
(see below).
The function tries to check if a file "lockfile" exists. I tested it and it works, but is it safe ?
Kind of. There's a few issues, though.
First, you don't specify a path to "
lockfile"
, so it will be created/checked for in whatever is the current working directory (cwd). Since there's no restriction what directory you can be in when launching programs, this can be anywhere, unless you set the cwd in the program, of course.
Second, open will do fine on local file systems, but over
networked filesystems it may not be an atomic operation, and so may not work properly. The solution is to use
link in stead (see the open manpage (man 2 open) under O_EXCL, this is explained there -- you can also look at the DROD source code in DROD/Main.cpp, function IsAppAlreadyRunning(), for an example).
Third, I don't know what you do in the rest of your program, but if you don't delete the lockfile it will have to be deleted manually for subsequent runs. In any case, if the program crashes so that the lockfile isn't deleted, you'll have the same problem. In DROD I solved this by storing the program id (pid) in the lockfile and checking if that pid is running if a lockfile is found (again, you can look at IsAppAlreadyRunning() that I mentioned above for an example). If the pid doesn't exist, the lockfile is invalid, and we can safely continue. This isn't foolproof (the pid may exist but be a different application), but it's much better than nothing. Some sort of IPC would be a better solution, but that would be more complex.
Fourth, you're not closing the lockfile. This isn't the end of the world as having it open throughout the program doesn't affect anything (well, unless you hit the open file limit, which is usually 1024 files), and the OS will close it when the program exits anyway, but I still think you should close it
. In the code snipped above, inserting "
close(lock);"
right before "
return 1;"
should do fine.
Which function do I use to close a file ?
close(file). A return value of 0 means it was successful (see man 2 close).
Is unlink used to delete a file ?
Yes
Is there a simple way of checking if a file is empty or not ?
Depends what you mean by simple
. There's no "
isfileempty"
function you can call, but it's pretty easy to use the "
stat"
function to get the size of the file, and then you can just check if the size is zero. See "
man 2 stat"
.
By the way: do you know a good online Assembly manual/tutorial ? I found a good page, but it's down for several days now. Trick, perhaps ?
I
had some very nice links, but I seem to have lost them. I'll see if I can find them again. Anyway,
LinuxAssembly.org has some tutorials, but it's not that helpful if you want to do Windows assembly, and I also think most of them assume you're at least a little bit familiar with assembly already (know what a register is and what they're called, basic instructions, etc). Also,
NASM has a very nice reference. Their site is down at the moment (seems they missed that SourceForge stopped supporting mysql 3 since the 1st of November..), but archive.org's got it archived here:
http://web.archive.org/web/20041020074845/http://nasm.sourceforge.net/
(The url tag didn't work properly for this one, you need to copy and paste the entire link).
If all else fails, feel free to ask
- Gerry