05-23-2024, 06:56 PM
(05-23-2024, 05:45 PM)Kernelpanic Wrote: One could almost compare it with a traffic light at an intersection: If North - South is green, then West - East has to wait (red) until it itself is green. If all road users stick to it, there won't be crashed.Unfortunately Steve's approach can't prevent both programs from reading and writing at the same time, I don't think there's a way to do it with read/write file interactions alone. Both programs can read the `00` at the same time and will simply write over each-other when updating the status. Then you'll have the main program reading the mouse data while the second program is writing it, and it falls apart.
That said an alternative for madscijr's original approach could be to have the mouse program write to a completely separate temporary file and then use `NAME` to rename the file to be the correct one. That has two advantages that 1. There's no period where the file is created but doesn't have the correct contents, and 2. There's no period where the file exists but is open by the mouse program (since the mouse program closes it before renaming). Over in the main program you can have it use `KILL` to delete the file after reading the contents, that would signal to the mouse program that the file is available to be used again.
It's still not as good as a proper IPC approach like using networking or a pipe, but might solve the problem.