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;
}