This is a more general question, not really related to any programming language.
I ask it here because I like it here, out of the box thinking is required
I often find myself running multiple different programs or multiple instances of the same program.
Often, they access the same registry keys/files remember them or not, restore them later or not...
What's important is that they don't collide.
( Let's leave out the possibility of 3th party software I didn't wrote ).
One way is by using some kind of parent/child hierarchy.
Imagine I want to write to a file but I need to make sure no other program writes to it.
For files I use a trick where I let the parent write an initial key to the registry, let's say fileOpen.
The children will try to delete this key, however only one of them can succeed at any given time and...
The second way is more complex but doesn't requires the parent/child hierarchy.
Now I simply write a key to registry it's name is PID, it's initial value is waiting.
Then wait for the CPU to drop and then enumerate all the PID's of all other processes found by a regread to the same path.
It will find out which process has the lowest PID and if that is this process change all values to locking.
The other instances or this instance will change this value again to detected.
When the program with the lowest PID finds everyone has changed to detected it changes them to freezed.
Finally the instance that initiated the locking can run and...
Because the 1st way isn't perfect because it needs a parent and the 2nd way is so complex.
I was wondering if there was an easier way to serialize multiple processes.
So I was hoping that opening a file could only be done once, but then I discovered that two unrelated programs could open a file for writing simultaneously and still collide ( I used autoIT but assume the same is true in any language ).
So far I only know two things that work, deleting a file or deleting a regkey because this can only be done once obviously.
Unfortunately they both have the same problem which is you can't delete a file or key that doesn't exist already.
Maybe I delete a system file or key, but that is a dangerous practice.
I was thinking of a default file in the driver backup cache or something, but the OS should always restore it by itself after a reboot, I mean what if my program crashes, then the whole mechanism would be broken !
Am I really asking too much, can't believe I'm the only one strugling with these kind of issues.
How do the experts do it then, there must be a simpler way to serialize a process.
like set /a $err = rundll.exe, serialize/lock filename
I hope this long post is going to help, and we can exchange knowledge.
Code: Select all
:setSerial ( $var = 'default' )
:: (
%write% $var %= unlock =%
%= ...we could examine the CPU to see if we need to wait longer or some other variant :? =%
set $time = 1
%while% not delete $var
set $time = %sleep% ( $time ) %= $time multiplies =%
%wend%
%= ...We are serial/locked yippie :P =%
:: )
exit /b $err
Impatiently waiting for your input,
Ed