05-23-2024, 08:09 PM
(05-23-2024, 06:56 PM)DSMan195276 Wrote:(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.....
It can.
Try these two programs out and watch them interact with a single file:
Code: (Select All)
Open "temp.txt" For Output As #1: Close 'make certain file is blank/new to begin with
Open "temp.txt" For Binary As #1
Do
Select Case checkFile
Case 11
Print "Program 1 has control, is reading file."
readfile
Case 12
Print "Program 1 has control, is writing to file."
writefile
Case 21
Print "Program 2 has control, is reading file."
Case 22
Print "Program 2 has control, is writing to file."
Case Else
Print "File is free for usage."
x = Int(Rnd * 100)
Select Case x
Case 10: writefile 'generate a random write every so often to see if we have issues
End Select
End Select
_Limit 10
Loop
Sub readfile
Dim temp As _Unsigned _Byte
Dim junkdata As Long
temp = 11
Put #1, 1, temp
_Delay .1 'wait 1/10 second to make certian other file isn't also trying to take control
Get #1, 1, temp
If temp <> 11 Then Exit Sub 'other file took control first. We had a race condition and avoided it.
Print "Recieved Data:"
Do Until EOF(1)
Get #1, , junkdata
Print junkdata; ",";
Loop
Print
'finished reading, free up the file now
temp = 0
Put #1, 1, temp
End Sub
Sub writefile
Dim temp As _Unsigned _Byte
Dim junkdata As Long
temp = 12
Put #1, 1, temp
_Delay .1 'wait 1/10 second to make certian other file isn't also trying to take control
Get #1, 1, temp
If temp <> 12 Then Exit Sub 'other file took control first. We had a race condition and avoided it.
Print "Writing Data:"
For i = 1 To 10
junkdata = Int(Rnd * 100) + 1
Put #1, , junkdata
Print junkdata; ",";
Next
Print
'finished reading, free up the file now
temp = 21 'tell the second program that it's now time to read the file!
Put #1, 1, temp
End Sub
Function checkFile~%%
Dim temp As _Unsigned _Byte
Get #1, 1, temp
checkFile = temp
End Function
Code: (Select All)
Open "temp.txt" For Binary As #1
Do
Select Case checkFile
Case 21
Print "Program 2 has control, is reading file."
readfile
Case 22
Print "Program 2 has control, is writing to file."
writefile
Case 11
Print "Program 1 has control, is reading file."
Case 12
Print "Program 1 has control, is writing to file."
Case Else
Print "File is free for usage."
x = Int(Rnd * 100)
Select Case x
Case 10: writefile 'generate a random write every so often to see if we have issues
End Select
End Select
_Limit 10
Loop
Sub readfile
Dim temp As _Unsigned _Byte
Dim junkdata As Long
temp = 21
Put #1, 1, temp
_Delay .1 'wait 1/10 second to make certian other file isn't also trying to take control
Get #1, 1, temp
If temp <> 21 Then Exit Sub 'other file took control first. We had a race condition and avoided it.
Print "Recieved Data:"
Do Until EOF(1)
Get #1, , junkdata
Print junkdata; ",";
Loop
Print
'finished reading, free up the file now
temp = 0
Put #1, 1, temp
End Sub
Sub writefile
Dim temp As _Unsigned _Byte
Dim junkdata As Long
temp = 22
Put #1, 1, temp
_Delay .1 'wait 1/10 second to make certian other file isn't also trying to take control
Get #1, 1, temp
If temp <> 22 Then Exit Sub 'other file took control first. We had a race condition and avoided it.
Print "Writing Data:"
For i = 1 To 10
junkdata = Int(Rnd * 100) + 1
Put #1, , junkdata
Print junkdata; ",";
Next
Print
'finished reading, free up the file now
temp = 11 'tell the second program that it's now time to read the file!
Put #1, 1, temp
End Sub
Function checkFile~%%
Dim temp As _Unsigned _Byte
Get #1, 1, temp
checkFile = temp
End Function
They set the flag for who has control, and skip doing anything unless they have control... Then when they're finished writing, they set the flag so the other program now has control to read.
Note that you can also use LOCK to make certain that only one program can access a file at a time, though I didn't mention it earlier as it's WINDOWS ONLY. https://qb64phoenix.com/qb64wiki/index.php/LOCK