<div dir="ltr">Hi David,<div class="gmail_extra"><br><div class="gmail_quote">On Sat, May 21, 2016 at 10:15 AM, David T. Lewis <span dir="ltr">&lt;<a href="mailto:lewis@mail.msen.com" target="_blank">lewis@mail.msen.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Sat, May 21, 2016 at 09:50:16AM -0700, tim Rowledge wrote:<br>
&gt; We have an interesting problem in Pi-land where many teachers report kids losing their Scratch work (and other, but I don???t get to fix that) because of pulling the power before fully shutting down etc. This tends to put them off trying again, apparently.<br>
&gt;<br>
&gt; We already close files properly after writing out files but it seems that dear ol???unix likes to save actual writing for later, perhaps when it has time for a relaxing latt?? or whatever. It has been suggested that using fsync() might force the lazy writer to actually do its job properly, which seems reasonable. In thinking about where to add this I see a couple of obvious possibilities<br>
<span class="">&gt; a) a new primitive<br>
&gt; b) add fsync() (suitably wrapped in case of non-availability) to the end of the sqFileFlush() called in primitiveFileFlush() code.<br>
&gt;<br>
</span>&gt; I think I prefer b) personally but I???m happy to be educated. Possible probles insclude some OS??? not having a fsync option; so far as I can see Windows already uses a totally disjoint set of code.<br>
&gt;<br>
&gt; Can anyone think of bad things happening if I do this? Think of the children???.<br>
&gt;<br>
<br>
I suspect that the problem lies elsewhere. The difference between fsync()<br>
and fflush() is basically that fflush() functions at the higher level stdio<br>
(C runtime lib) level, and fsync() is a system call at a lower level that<br>
operates directly on the file descriptor.<br></blockquote><div><br></div><div>fflush and fsync are different.  fflush merely ensures that the stdio buffers in the process are flushed to the kernel file state via write calls. sync however ensures that kernel state for that file descriptor is written to disc.  sync does this for all kernel file state.  I think Tim&#39;s diagnosis and solution are correct.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
The unix file functions are written to the stdio level, so the existing call<br>
to fflush() is the right thing to do. I do not know if mixing a lower level<br>
call in with this would hurt anything, but it will not help anything either<br>
and it does not seem like a good thing to do.<br>
<br>
My guess would be that there is some path through the code in the image where<br>
you might be able to add a #flush to address the issue. This can have performance<br>
tradeoffs, but reliable would be better than fast in a case like this.<br>
<br>
Dave<br>
<br>
<br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><span style="font-size:small;border-collapse:separate"><div>_,,,^..^,,,_<br></div><div>best, Eliot</div></span></div></div></div>
</div></div>