opening same file twice

Just starting out? Need help? Post your questions and find answers here.
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

opening same file twice

Post by t57042 »

I am trying to open the same file 2 times on the same PC (windows XP) to test multiuser possibility.
The fist process seems to lock the file.
The second openfile or readfile returns 0.

Is there a solution to that?

Richard
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

Re: opening same file twice

Post by t57042 »

After further testing the problem only exists when the file is opened with "openfile".
If the file is opened twice with "readfile" both programs can open the file.
I suppose this is the way it is supposed to be.

Is there a way to open with "openfile" without preventing others from opening tat file?
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: opening same file twice

Post by Kaeru Gaman »

I thought I read a Feature Request for this...
but I can't find it now...

in my eyes it's correct this way, because write-access has to be exclusive.

why don't you use ReadFile if you want shared access?

[edit]
two times write access can produce unpredictable results.
I would suggest a central File-Handler, that grants single write access to the clients.
a client can send a request to the handler and wait for admittance.
oh... and have a nice day.
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

Re: opening same file twice

Post by t57042 »

I want to try recordlocking - for that the file should be accesible.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: opening same file twice

Post by Kaeru Gaman »

hm... still suggesting a central handler.
let the clients send a message to the central handler, the handler itself writes it to disk.

or did I misunderstand now...?
oh... and have a nice day.
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

Re: opening same file twice

Post by t57042 »

The idea is to use the first byte of each record to indicate if the record is locked.
So before updating a record - read it - check 1st byte - when not locked write.

I used this protocol before in multi user applications (1 computer with several terminals)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: opening same file twice

Post by Kaeru Gaman »

do you use a DataBase system?
those should handle this internally...

but ok, having this in mind, it maybe worth a feature request...
oh... and have a nice day.
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

Re: opening same file twice

Post by t57042 »

This would be to avoid a database system (sqlite locks the entire database = all tables together)

Could this maybe done with an API call?
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: opening same file twice

Post by Kaeru Gaman »

I would suggest introducing a new flag for OpenFile, like #PB_File_NonExclusive.
oh... and have a nice day.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: opening same file twice

Post by Trond »

t57042 wrote:The idea is to use the first byte of each record to indicate if the record is locked.
So before updating a record - read it - check 1st byte - when not locked write.

I used this protocol before in multi user applications (1 computer with several terminals)
That won't work. If another application locks the record after the record was read, but before the first byte was checked, there will be havoc.
User avatar
mk-soft
Always Here
Always Here
Posts: 6206
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: opening same file twice

Post by mk-soft »

Kaeru Gaman wrote:I would suggest introducing a new flag for OpenFile, like #PB_File_NonExclusive.
Yes, I needed too

http://www.purebasic.fr/english/viewtop ... 13&t=41197
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Mahan
User
User
Posts: 35
Joined: Sun Jan 25, 2009 10:12 am
Location: Sweden

Re: opening same file twice

Post by Mahan »

I seem to remember the reason why SQLite has to lock the entire file is that there are OSes where you cannot reliably open a file twice for writing with reliable block-locking. I think this was mainly a Linux issue. And since PB is for Linux too this might be the reason for the limitation?
Post Reply