ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Even if you don't have access to this library, you have access to your own code.
So if the library wants to write a file immediately, then simply don't call the library functions until you want to.
That should work, right?
Keep your own copy of whatever you want to ultimately write in memory and only call the library function when you want to.
when my code does, eventually, call that library code, there will still be a period of time between when it calls open() and when it calls the first write().
i'm thinking to create an empty file distinct to the target file, though not the same exact name as the target file, and lock on that file to avoid concurrently doing this part in my code. that will at least be a start. locks will need to be added to other code, too.
when my code does, eventually, call that library code, there will still be a period of time between when it calls open() and when it calls the first write().
i'm thinking to create an empty file distinct to the target file, though not the same exact name as the target file, and lock on that file to avoid concurrently doing this part in my code. that will at least be a start. locks will need to be added to other code, too.
I don't see how you will be able to do anything then.
This makes it seem like you do one call to the library, which then doesn't return until it writes the file, which entails opening it first for some time while it does some operation that takes some time.
I can't think of a reliable, non crazy way to use a second thread to somehow balance the time between the library call starts (and creates the file) and finishes its prepwork to write to it.
There's so many variables.
Why does it even matter?
Security?
Well then encrypt the data first before you send it to the library and then decrypt it in a post processing step.
yeah, there's no hard definitive solution under the I/O model Posix/Unix/Linux uses. there's only designing around the problem to be sure no issues can happen in the first place. and that might mean having some way to lock against double uses of the file before some like first write or file completion.
there is a related problem. it's related because these programs will often take all day to complete the file due to the nature of what they are doing. consider opening a file for appending. now consider 2 processes opening the very same file for appending.
if the kernel (of any POSIX OS) were changed to work as i described, what would break? any applications? any libraries? is it something that would be harder to implement had the kernel done it this way since its first creation?
Zero-length files are valid and useful. They indicate the absence of something. The "test -s" command checks for non-zero-length files. I have used it many times.
I am still not sure why you want to avoid zero-length files.
Ed
We posted the solution: create a temporary file, write to it, and then rename the temporary file to the desired name.
Ed
the contrary case is something that runs for hours and hours collecting some statistical data that needs to be reviewed periodically all day. and this data needs to not be lost if the system reboots, eliminating the use of /tmp.
...or, as I said back in post #9, open() it with O_TMPFILE: though that is a Linux specific solution and is not portable.
you sure did. and i overlooked it thinking it was something different. that does look like a doable approach. i'll have to make sure there is a lock to prevent duplicate attempts. but this is certainly a new, good, perspective on the problem.
Well, it's an option, but after reading your later posts (#20 specifically), IMO you're approaching the problem from the wrong end. Catch SIGTERM/SIGINT and give it a handler that will remove an empty file before exiting; then you can simply rely on open(filename, O_CREAT|O_EXCL) semantics to provide serialisation.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.