Google Groups unterstützt keine neuen Usenet-Beiträge oder ‑Abos mehr. Bisherige Inhalte sind weiterhin sichtbar.

Locking a file from a c++ program

1.348 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Chris Halverson

ungelesen,
02.09.1998, 03:00:0002.09.98
an
Thomas Linden <t...@daemon.de> writes:

> I get the compiler error, that the first parameter of flock must be an
> integer. And _file is not an integer, it's an ofstream-object...

First, you should probably use fcntl(2) instead of flock.

Then, to get the file descriptor (the int you need), you can use the
rdbuf()->fd() method, as in:

ofstream oFile("/your/file/here");
fcntl(oFile.rdbuf()->fd(), F_SETLKW, fileLock(F_WRLCK));

Where fileLock is my own function to set the appropriate flock
structure.

Chris

--
Chris D. Halverson Complete Internet Solutions
PGP mail accepted, finger for public key http://www.CompleteIS.com/~cdh/

Thomas Linden

ungelesen,
03.09.1998, 03:00:0003.09.98
an
Thank you very much. Now I can compile it and get no errors. And the
fcntl command returns not -1, wich means, that the lock of the file was
successfull. But while the file is "locked", i can edit it with another
programm.
Can you help me once again?


Thomas
--
__________________________________
T h o m a s L i n d e n
http://www.daemon.de
mailto:t...@daemon.de

Andrew Gierth

ungelesen,
03.09.1998, 03:00:0003.09.98
an
>>>>> "Thomas" == Thomas Linden <t...@daemon.de> writes:

Thomas> Thank you very much. Now I can compile it and get no
Thomas> errors. And the fcntl command returns not -1, wich means,
Thomas> that the lock of the file was successfull. But while the file
Thomas> is "locked", i can edit it with another programm.

That's the expected behaviour.

File locking on Unix is "advisory"; that is, a lock held merely
prevents the acquisition of a competing lock, it doesn't prevent
actual reads and writes to the affected data. Cooperation between
processes is required to avoid conflicts.

Some Unixes (not all) have an option to request mandatory locking
by setting a combination of permission bits on the file.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
or <URL: http://www.whitefang.com/unix/>

Chris Halverson

ungelesen,
04.09.1998, 03:00:0004.09.98
an
Thomas Linden <t...@daemon.de> writes:

[ review, I wrote: ]

> > ofstream oFile("/your/file/here");
> > fcntl(oFile.rdbuf()->fd(), F_SETLKW, fileLock(F_WRLCK));

> Thank you very much. Now I can compile it and get no errors. And the
> fcntl command returns not -1, wich means, that the lock of the file was
> successfull. But while the file is "locked", i can edit it with another
> programm.

My fileLock (which really isn't mine, it's a common way of doing this)
is:

flock*
fileLock(const short type) {
static flock ret ;
ret.l_type = type ;
ret.l_start = 0 ;
ret.l_whence = SEEK_SET ;
ret.l_len = 0 ;
ret.l_pid = getpid() ;
return &ret ;
}

fctnl(2) says:

F_SETLKW This command is the same as F_SETLK except
that if a shared or exclusive lock is blocked
by other locks, the process will wait until
the request can be satisfied. If a signal
that is to be caught is received while
fcntl() is waiting for a region, fcntl() will
be interrupted. Upon return from the pro-
cess' signal handler, fcntl() will return -1
with errno set to EINTR, and the lock opera-
tion will not be done.

F_SETLK says:

F_SETLK Set or clear a file segment lock according to
the lock description pointed to by the third
argument, arg, taken as a pointer to type
struct flock, defined in <fcntl.h>. F_SETLK
is used to establish shared (or read) locks
(F_RDLCK) or exclusive (or write) locks
(F_WRLCK), as well as to remove either type
of lock (F_UNLCK). F_RDLCK, F_WRLCK and
F_UNLCK are defined in <fcntl.h>. If a
shared or exclusive lock cannot be set,
fcntl() will return immediately with a return
value of -1.

Other than that, without seeing code I can't help. Are you checking
errno or anything to see if anything else gets set?

Andrew_J ROBBIE

ungelesen,
04.09.1998, 03:00:0004.09.98
an
Chris Halverson (c...@CompleteIS.com) wrote:
[snip]
: Then, to get the file descriptor (the int you need), you can use the

: rdbuf()->fd() method, as in:

: ofstream oFile("/your/file/here");
: fcntl(oFile.rdbuf()->fd(), F_SETLKW, fileLock(F_WRLCK));

The only problem with this is that it is compiler dependent. The C++
standard doesn't say anything about being able to use any fileno()
equivalent on o{f}streams. Similarly, there are no Posix bindings for
C++, only C.

The problem is that there doesn't seem to be any resolution on the
horizon. Some compilers may support converting FILE* streams to C++
streams, but I haven't used any. For the moment, we are stuck doing
things like your example.

Regards,
Andrew


Thomas Linden

ungelesen,
05.09.1998, 03:00:0005.09.98
an
Chris Halverson wrote:
>
> Thomas Linden <t...@daemon.de> writes:
>
> [ review, I wrote: ]
>
> > > ofstream oFile("/your/file/here");
> > > fcntl(oFile.rdbuf()->fd(), F_SETLKW, fileLock(F_WRLCK));
>

Oh, the code is too big to post here. The file lock itself works! I run
my program and I pause it during editing a file, if I run the same
program once again, this second process could not write the file until
the first process is finished.
But the problem is, that another process may want to write the file, and
this process don't use the lock feature (it's the vi :-) )

So I will take a look to the mandatory option in kernel, or I will
rename the file to a temporary name during editing...

Thank you all for your help!

Klaus-Georg Adams

ungelesen,
08.09.1998, 03:00:0008.09.98
an

Thomas Linden <t...@daemon.de> writes:
>
> So I will take a look to the mandatory option in kernel, or I will
> rename the file to a temporary name during editing...
>
> Thank you all for your help!

You really want to look in W. R. Stevens' Advanced Progamming in the
Unix Environment. He has a chapter about locking. If vi does: copy
file to /tmp, edit file in /tmp, unlink original, mv /tmp/filecopy to
original (and I believe vi does something along these lines), than you
are lost anyways.

-- kga
-------------------------------------------------------------------------
Klaus-Georg Adams Email: Klaus-Ge...@chemie.uni-karlsruhe.de
Institut f. Anorg. Chemie, Lehrstuhl II Tel: 49(0)721 608 3485
Universität Karlsruhe, D-76128 Karlsruhe
-------------------------------------------------------------------------

0 neue Nachrichten