Locking function does not lock.

Hi all,

I’m doing some tests on the locking() function in order to integrate it
in my development. Now I know locks are ignored bu functions such as
open(), read(), write() and a few others but according to the tests I have
done even the locking() functions ignores any lock on files.

I have set a lock on the first byte of a file and tried setting another
lock from another process on that same byte. On the second lock I’m
receiving and success status from the locking() function even though the
byte is already locked.

I’m my missing something or have I overlooked anything?

Sys config:

Intel P2 266, 128MB RAM;
QNX 4.25;
Phindows 1.20;
Watcom comiler 10.6;

Duy Tran Duc
Software Engineer
Bombardier Transportation
dtranduc@bombardier.transportation

Yes, you’ve probably overlooked the fact that locks are only advisory. I.e., if
part of file is locked by one application, an attempt to lock it by another
apllication will fail. This way applications can indicate each other that
certain file regions are busy (locked) to avoid conflicts. However, this does
not prevent or block access to the file by open(), write(), etc. Please see
documentation for details.

Duy Tran Duc wrote:

Hi all,

I’m doing some tests on the locking() function in order to integrate it
in my development. Now I know locks are ignored bu functions such as
open(), read(), write() and a few others but according to the tests I have
done even the locking() functions ignores any lock on files.

I have set a lock on the first byte of a file and tried setting another
lock from another process on that same byte. On the second lock I’m
receiving and success status from the locking() function even though the
byte is already locked.

I’m my missing something or have I overlooked anything?

Sys config:

Intel P2 266, 128MB RAM;
QNX 4.25;
Phindows 1.20;
Watcom comiler 10.6;

Duy Tran Duc
Software Engineer
Bombardier Transportation
dtranduc@bombardier.transportation


Best regards,
Alexander Koppel

///Data Kinetics Ltd.
QNX Training and Consulting (http://www.dkl.com)

Yes that is a possibility but then how would a get advised that a byte
or more is locked since even the locking() function doesn’t seem to block
two consecutive lock attempts on a same byte.

How would I protect myself from file corruption if it is not possible to
know if a portion of a file is locked or not?

Duy


“Alexander Koppel” <akoppel@dkl.com> wrote in message
news:39746DDA.31D42CBB@dkl.com

Yes, you’ve probably overlooked the fact that locks are only advisory.
I.e., if
part of file is locked by one application, an attempt to lock it by
another
apllication will fail. This way applications can indicate each other that
certain file regions are busy (locked) to avoid conflicts. However, this
does
not prevent or block access to the file by open(), write(), etc. Please
see
documentation for details.

Duy Tran Duc wrote:

Hi all,

I’m doing some tests on the locking() function in order to integrate
it
in my development. Now I know locks are ignored bu functions such as
open(), read(), write() and a few others but according to the tests I
have
done even the locking() functions ignores any lock on files.

I have set a lock on the first byte of a file and tried setting
another
lock from another process on that same byte. On the second lock I’m
receiving and success status from the locking() function even though the
byte is already locked.

I’m my missing something or have I overlooked anything?

Sys config:

Intel P2 266, 128MB RAM;
QNX 4.25;
Phindows 1.20;
Watcom comiler 10.6;

Duy Tran Duc
Software Engineer
Bombardier Transportation
dtranduc@bombardier.transportation


Best regards,
Alexander Koppel

///Data Kinetics Ltd.
QNX Training and Consulting (> http://www.dkl.com> )

Previously, Duy Tran Duc wrote in comp.os.qnx:

Hi all,

I’m doing some tests on the locking() function in order to integrate it
in my development. Now I know locks are ignored bu functions such as
open(), read(), write() and a few others but according to the tests I have
done even the locking() functions ignores any lock on files.

I have set a lock on the first byte of a file and tried setting another
lock from another process on that same byte. On the second lock I’m
receiving and success status from the locking() function even though the
byte is already locked.

I’m my missing something or have I overlooked anything?

Sys config:

Intel P2 266, 128MB RAM;
QNX 4.25;
Phindows 1.20;
Watcom comiler 10.6;

Uh, you’re missing some snippets of example code…

Here’s a program I wrote once to play with locking. locking() is
a Watcom-unique, or possibly legace DOS, function, I used the
standard Unix ones.

The -s option is to stop after getting a lock, then run
another lck to see if a different process can grab it.

– lck.c –

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

void serr(const char* call)
{
fprintf(stderr, “%s failed: [%d] %s\n”, call, errno, strerror(errno));
if(errno == EAGAIN) return;
exit(1);
}

int main(int argc, char* argv[])
{
char* file;
int opt;
int fd;
struct flock f = {
F_UNLCK, /* l_type /
SEEK_SET, /
l_whence /
0, /
l_start /
0, /
l_len: 0 == inf /
0, /
l_sysid? /
0, /
l_pid? */
};
struct flock q = { F_WRLCK, 0, };

while((opt = getopt(argc, argv, “f:wrqskh”)) != -1)
{
switch(opt)
{
case ‘f’:
file = optarg;
fd = open(file, O_RDWR|O_CREAT, 0664);
if(fd == -1) serr(“open”);
break;

case ‘w’:
f.l_type = F_WRLCK;
if(fcntl(fd, F_SETLK, &f) == -1) serr(“fcntl/wr lock”);
break;

case ‘r’:
f.l_type = F_RDLCK;
if(fcntl(fd, F_SETLK, &f) == -1) serr(“fcntl/rd lock”);
break;

case ‘q’:
if(fcntl(fd, F_GETLK, &q) == -1) serr(“fcntl/query”);
printf("%s: type %d start %d len %d sysid %#x pid %d\n",
file, q.l_type, q.l_start, q.l_len, q.l_sysid, q.l_pid
);
break;

case ‘s’:
if(kill(getpid(), SIGSTOP) == -1) serr(“kill”);
break;

case ‘k’:
kill(getpid(), SIGKILL);

default:
printf(
“usage: lck [options…]\n”
" -f file file\n"
" -w write lock\n"
" -r read lock\n"
" -q query lock\n"
" -s stop via SIGSTOP\n"
);
exit(1);
}
}
return 0;
}

I’m afraid that there may be something wrong with your experiment. You may wish
to post the source code.

Duy Tran Duc wrote:

Yes that is a possibility but then how would a get advised that a byte
or more is locked since even the locking() function doesn’t seem to block
two consecutive lock attempts on a same byte.

How would I protect myself from file corruption if it is not possible to
know if a portion of a file is locked or not?

Duy

“Alexander Koppel” <> akoppel@dkl.com> > wrote in message
news:> 39746DDA.31D42CBB@dkl.com> …
Yes, you’ve probably overlooked the fact that locks are only advisory.
I.e., if
part of file is locked by one application, an attempt to lock it by
another
apllication will fail. This way applications can indicate each other that
certain file regions are busy (locked) to avoid conflicts. However, this
does
not prevent or block access to the file by open(), write(), etc. Please
see
documentation for details.

Duy Tran Duc wrote:

Hi all,

I’m doing some tests on the locking() function in order to integrate
it
in my development. Now I know locks are ignored bu functions such as
open(), read(), write() and a few others but according to the tests I
have
done even the locking() functions ignores any lock on files.

I have set a lock on the first byte of a file and tried setting
another
lock from another process on that same byte. On the second lock I’m
receiving and success status from the locking() function even though the
byte is already locked.

I’m my missing something or have I overlooked anything?

Sys config:

Intel P2 266, 128MB RAM;
QNX 4.25;
Phindows 1.20;
Watcom comiler 10.6;

Duy Tran Duc
Software Engineer
Bombardier Transportation
dtranduc@bombardier.transportation


Best regards,
Alexander Koppel

///Data Kinetics Ltd.
QNX Training and Consulting (> http://www.dkl.com> )


Best regards,
Alexander Koppel