Closing all open files
Pages: 1 2
Steve Fryatt (216) 2105 posts |
Why not keep track of the files that you’ve opened, and close them all in a standard error handler? BASIC programs are far easier to debug if you have an error handler from the start, because things then exit cleanly. In general, unless you’re dealing with lots of open files1, then initialising your file handles to zero at the start of the program before hooking up the error handler
and then cleanly closing files on exit with something like
is much better than blatting all open files on a filesystem, for the reasons that Rick and Druck have already outlined. You do need to be careful to always zero your file handles after a
but if you’re doing this a lot then you’d quickly wrap it all up into a set of standard library procedures and functions. 1 ETA: By this I mean that if you’re handling a lot of files you would very quickly end up writing a generalised library to track all of this, storing the handles in a global array as files are opened and zeroing them out again on close. Then on an error, your handler can quickly traverse the array and |
Bryan (8467) 468 posts |
All good stuff, which I already try to do. Thinking more about it, I have commented out the I will use the *-ram-close manually if needed. Once again, thanks for your help. |
Dave Higton (1515) 3534 posts |
There is no problem whatsoever in keeping any number of files open over as many Wimp polls as you want. |
Alan Adams (2486) 1149 posts |
You might want to check out !resrec by A.J. Pullan. It allows you (anong other things) to see all open files, and selectively close them. Very useful when debugging, and after alt-break. Here’s my BASIC handler (which requires !Reporter to be available):
|
Rick Murray (539) 13850 posts |
Technically, there is no problem. But by using polling as a yardstick, it forces one to think more about file access rather than opening a file and leaving it open for an undefined length of time, because that is where the problem lies. Better, I feel, to open-do_stuff-close as necessary. |
Dave Higton (1515) 3534 posts |
As always, it depends on what you’re doing. But if you keep opening and closing a file many times, it will be slower than just reading or writing a chunk at a time. Especially for writing, it will be much slower – and it will thrash the storage medium more. When I say slower, it will also have some impact on other tasks when multi-tasking. I discovered many years ago that the fastest way to handle files is to read them or write them a chunk at a time, preferably an integral number of sectors of the storage medium. Nowadays I tend to use 64 kiB chunks. That’s what enables my DBack and DRest apps to go so fast, for example. |
David J. Ruck (33) 1636 posts |
If you want to know what files are open, use this BASIC program.
|
Bryan (8467) 468 posts |
That works: I can see my open RAM:: files. Why is |
David Pitt (3386) 1248 posts |
That only happens if an unpartioned Fat32 card, containing only the firmware and ROM, is in the slot, RISC OS being on SCSI. It’s handle is 255. No such thing happens with a partitioned card where the firmware is in Loader. P.S. Tried consulting the PRM for OS_Args. “Reads or writes an open file’s arguments, or returns the filing system type in use”. Page 2.49 ROOL PDF. Minor correction With a partitioned Loader type card |
Simon Willcocks (1499) 520 posts |
Director allows you see see and close open files with a right-click on the rightmost iconbar icon, iirc. |
Sprow (202) 1158 posts |
I feel compelled to suggest using OS_FSControl 58 to find the open handles rather than just prodding 1 to 255 and listening for a squeak – that would obviously fall over when we unleash the full 32 bit range of possible handles. Using 32 bit handles would also allow FileSwitch to recycle them less often (4 billion handles could just count down from &FFFFFFFF at power on) which would eliminate much of the concern about |
Steve Pampling (1551) 8172 posts |
Memories of a bug in the sw/firmware for the Nortel Baystack 4701 prompt me to say that it’s a bit over 497 days; by a number of hours, minutes, seconds and centi-seconds (can’t be bothered calculating the precise number) 1 To avoid daytime clock rollover restart issues, I kept a table of expected roll-over dates and rebooted at a time of my choosing (usually between 2300 and 0000) |
Rick Murray (539) 13850 posts |
Interesting. https://blog.michaelfmcnamara.com/2008/06/how-much-uptime-is-too-much/ |
Chris Mahoney (1684) 2165 posts |
Funny to be talking about uptime after I’ve just read this story!
Today I learned that (presumably) Linux uses centiseconds for timers. I assumed it used something more granular, but 497 days is UINT_MAX cs. |
Steve Pampling (1551) 8172 posts |
Ah, Mr McNamara – source of much useful info back then. |
Martin Avison (27) 1494 posts |
@Sprow
Thanks for that reminder. |
Sprow (202) 1158 posts |
I feel compelled to suggest using OS_FSControl 58 The wiki page is talking rubbish. FileSwitch 2.74 first appeared in RISC OS 5.17, so depending on your view the first stable release where the API would be set in stone is 5.18.
Careful now, if you have CallASWI 0.07 loaded during !Boot then that implements OS_FSControl 58 on behalf of the host OS. |
GavinWraith (26) 1563 posts |
Perhaps this is only indirectly relevant. Lua 5.4 introduced to-be-closed local variables.
File-handles are automatically equipped with __close metamethods. Some might argue that the necessity to explicitly close a file (or any other object with a __close metamethod) is good for the programmer’s soul, while others argue that automatic closure is a useful safety net. |
Rick Murray (539) 13850 posts |
A useful safety net for errors and crashes that shouldn’t be relied upon to tidy up after you (as in, close yer damn files!). It’s a double edged sword really. It would be good for an environment (OS, runtime, whatever) to finish your program leaving the machine in a sane state. But the more tidying up that is done automatically, the more people will come to rely upon that rather than bothering to do it themselves. |
GavinWraith (26) 1563 posts |
I really appreciate modern hire-cars that switch on the wipers when it rains, turn on the headlights when it gets dark, and dips them when there is oncoming traffic. On the other hand I loathe them when their gearboxes suffer software errors, and no reset buttons are available (so you have to disconnect the battery and reconnect to get started). The trouble with bells and whistles is that they reduce reliability because they multiply the probability of error. There have been times when wistful memories of a Morris Minor were recalled. But I agree that to-be-closed variables are likely to inculcate bad habits. It will be interesting to see what the Lua triumvirate does with them. Lua once had a macro system, like C. But it was jettisoned in the next version, reckoned as not worth the aggravation. |
Rick Murray (539) 13850 posts |
The guy that took me to work when mom was ill had a C4 Executive that did all that. The wipers came on, once the screen was getting hard to see out of. The lights turned themselves on and dipped themselves. It even had a button that he could get up to a speed and press and it would maintain that speed by itself until he braked. Talking about it, he appreciated the self driving, but he would have preferred to turn the automatic lights off because his wife’s car doesn’t have all the fancy and when he uses it (when his needs repairs, which is all too frequent), he’s driving without lights and failing to dip for other cars. It’s not that he’s a bad driver, he’s just used to the car doing it for itself that he doesn’t remember it’s a manual job. I rather imagine it would be a similar sort of OMG! for somebody used to power steering to use a non power car. You’re used to little movements, and now you have to fight the steering wheel like it’s a 737-MAX on a downward trajectory. Like I said, double edged sword. |
Chris Mahoney (1684) 2165 posts |
Mine has that, and it automatically slows down when the car in front does. Unfortunately it struggles with multiple lanes; the car in front indicates right and pulls into the turning bay, and my car proceeds to slow down. If left to its own devices I think it would probably come to a complete stop. |
Alan Adams (2486) 1149 posts |
Mine too. It works pretty well. It’s by far the easiest way to keep to speed limits. It is quite smart regarding lanes – it will happily overtake cars on my left, but to overtale cars on the right I have to touch the accelerator pedal to tell it so. In Germany, that logic swaps sides. It’s great in start-stop traffic. After a few seconds stopped, the engine stops. When the vehicle in front moves it starts the engine, and waits for a touch on the accelerator before moving off. As a result I can read a book while waiting, and when I feel the engine start, I put the book down. However the “lane-assist” isn’t so great. About 30% of the time it can’t detect the lanes and disengages. It gets confused with repainted lines in road-works too – those where the white lines are replaced by yellow ones with narrow lanes. If that’s the future of self-driving cars, I don’t want to be on the same road as them. |
Pages: 1 2