How to copy 2-byte file without locking it?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
miskox
Posts: 630
Joined: 28 Jun 2010 03:46

How to copy 2-byte file without locking it?

#1 Post by miskox » 13 Apr 2016 05:54

Hi all!

I have an application which has a 2-byte status file which it opens/reads/writes/rewrites.

1. My .bat sets one bit in this 2-byte status file so the processing can begin.
2. Application reads this file once every few seconds.
3. When this specific bit is set the application starts with the processing and resets this bit.
4. When it finishes it sets another bit to 1.
5. I check if this file has this 'completed bit set to 1'.

I tried:

1. copy command to copy this 2-byte status file to my temporary file
2. type command to type this 2-byte status file to my temporary file

Looks like both comamnds lock the file so when I copy/type this file to my temporary file it gets locked for a fraction of a second and if at the same time the main application tries to write to it the application returns error.

If I get the error bit set I just restart the application and it finishes just fine.

Any other way to get the contents of the file without locking it?

FC maybe (based on aGerman's excellent file dump)? I use FC to get the contents of this temporary file because it has binary values in it. (first byte has the value of 7 max (lower 3 bits), the second has the value of 31 max (lower 5 bits).

Thanks.
Saso

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to copy 2-byte file without locking it?

#2 Post by Squashman » 13 Apr 2016 08:48

If TYPE was locking the file you would think this should not work then.

Code: Select all

<nul set /p ".=A" >dummy.txt
for /l %%n in (1 1 15) do type dummy.txt >>dummy.txt

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to copy 2-byte file without locking it?

#3 Post by foxidrive » 13 Apr 2016 09:02

miskox wrote:2. Application reads this file once every few seconds.


Without exact information we just end up guessing.
My guess is your application opens the file for exclusive use.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to copy 2-byte file without locking it?

#4 Post by penpen » 13 Apr 2016 09:21

Squashman wrote:If TYPE was locking the file you would think this should not work then.
Only if you are using mutal exclusive locks.
In contrary, to make endless loops impossible, i would expect the type command and the file redirection mechanism both to use "read/write-preferring (read)write locks".

miskox wrote:Any other way to get the contents of the file without locking it?
Any program that has a microsoft logo should create a read-lock (or a read+write-lock) when accessing the file for reading.
Maybe there is a way to better synchronize ".bat" and "application" access to this file.

How do you start the "application" (".bat", task scheduler, ...)?
If your ".bat" runs it, then just avoid accessing the same file by multiple processes/threads.

"fc.exe" should be the best to read the bits:

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
set "sampleFile=2.txt"
set "dummyFile=dummy.bin"
>nul fsutil file createnew "dummy.bin" 2
>"%sampleFile%" echo(

:: depends on the byte order (little/big endian?) you may exchange the set/a's within the if-else-cases
set /A "value=0"
for /F "skip=1 tokens=1-2 delims=: " %%a in ('fc /B "!sampleFile!" "!dummyFile!"') do (
   if 0x%%~a equ 0 ( set /A "value|=0x%%~b00"
   ) else set /A "value|=0x%%~b"
)

if %value% equ 0x0d0a ( echo Value ^(%value%^) ok.
) else echo Value ^(%value%^) false.

rem del "%sampleFile%" "%dummyFile%"
endlocal
goto :eof


penpen

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: How to copy 2-byte file without locking it?

#5 Post by miskox » 13 Apr 2016 11:34

foxidrive wrote:
miskox wrote:2. Application reads this file once every few seconds.


Without exact information we just end up guessing.
My guess is your application opens the file for exclusive use.


Exactly as I wrote above.

Application reads this status file every few seconds. When one bit is set to 1 and application sees this then this application starts the process (and puts this bit to zero). When it finishes it updates this status file and sets another bit to 1 so other process(es) 'know' that it completed. The problem is if my .bat copies this file at the same time as application tries to update this 'completed' bit.

Saso

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to copy 2-byte file without locking it?

#6 Post by Squashman » 13 Apr 2016 13:35

Did you try just a simple redirection with SET /P ?

Code: Select all

set /p bytes=<file.txt

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to copy 2-byte file without locking it?

#7 Post by foxidrive » 13 Apr 2016 19:56

miskox wrote:
foxidrive wrote:
miskox wrote:2. Application reads this file once every few seconds.


Without exact information we just end up guessing.
My guess is your application opens the file for exclusive use.


Exactly as I wrote above.

Saso


What are you saying Saso? Do you know what "exclusive access" is?

It seems you don't... because your reply makes no mention of it.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: How to copy 2-byte file without locking it?

#8 Post by miskox » 13 Apr 2016 23:44

Squashman wrote:Did you try just a simple redirection with SET /P ?

Code: Select all

set /p bytes=<file.txt


Never tried this. I will try it and let you know.

foxidrive wrote:What are you saying Saso? Do you know what "exclusive access" is?

It seems you don't... because your reply makes no mention of it.


My answer was an answer to your sentense: Without exact information we just end up guessing.

This application has this file for it's own use. But it provides a way of controling what it does from external application (according to the manual). That is why I never mentioned exclusive access. It is not important. The only problem is when this application tries to update this status file it is 'locked' by my .bat for a short period of time. Currently I just set the required bit to 1 so it starts processsing the input file again. And it finishes without errors.

Saso

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: How to copy 2-byte file without locking it?

#9 Post by miskox » 14 Apr 2016 00:44

Squashman wrote:Did you try just a simple redirection with SET /P ?

Code: Select all

set /p bytes=<file.txt


Update: looks like this approach does not work.

Bytes variable is not defined.

This 2-byte file has:

- first byte: bits7-3 are 0 (officialy reserved so they can contain anything). Bits 2-0 are used.
- second byte: bits 7,6,5 are 0 (reserved. see above). Bits 4-0 are used.

Currently I see that reserved bits are always zero so this means that this status file can have a values of:

0x00 0x00 - 0x07 0x1F

This might be the reason why SET/P does not work?

Saso

einstein1969
Expert
Posts: 961
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: How to copy 2-byte file without locking it?

#10 Post by einstein1969 » 14 Apr 2016 09:30

I don't know if "volume snapshot" or "shadow copy" are the solutions.

Einstein1969

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: How to copy 2-byte file without locking it?

#11 Post by Squashman » 14 Apr 2016 09:50

penpen wrote:Maybe there is a way to better synchronize ".bat" and "application" access to this file.

I agree. Would also help to see all the code and configuration files being used.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: How to copy 2-byte file without locking it?

#12 Post by miskox » 14 Apr 2016 23:41

Update:

I did many test with FC.EXE and so far it is OK.

I do a FC.EXE with the 'production' status file instead doing it with a copy of that file.

Thank you all.
Saso

Post Reply