Unable to lock files

Hi,

Here is a small piece of code that I thought would allow me to lock a
file by running seperately on two consoles or pterms. I can’t get the
first one to lock the file and I can’t figure out why. Any clues as to
what I’m doing wrong would be appreciated. lockdemo.c is simply an
arbitrary filename.

The OS is stock/standard QNX4.25 using Watcom 10.6. This snippet works
in QNX6. Is there something wrong with file locking in QNX4?

Thanks,

Geoff.


#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <unix.h>


int main(void)
{
struct flock fl;
int fd, l;

fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 1;
fl.l_len = 400;
fl.l_pid = getpid();

if ((fd = open(“lockdemo.c”, O_RDWR)) == -1)
return;

printf("Press to try and get lock: ");fflush(stdout);
getchar();
printf(“Trying to get lock…”);fflush(stdout);

if ((l = fcntl(fd, F_SETLKW, &fl)) == -1)
{
perror(“fcntl”);
exit(-1);
}

printf(“got lock\n”);
printf("Press to release lock: ");fflush(stdout);
getchar();

fl.l_type = F_UNLCK;

if ((l = fcntl(fd, F_SETLK, &fl)) == -1)
{
perror(“fcntl”);
exit(-1);
}

printf(“Unlocked\n”);

close(fd);
}

The problem appears to be something to do with my QNX OS configuration.
I tried it on a system made from the May 2001 Product Suite CD (latest
that I have) and the test program worked. So there is a subtle
difference between my [rather old] development system and other QNX box
that prevents the lock from occuring. It’s not the compiler as the code
compiled/linked on the “bad” box works on the new system. Fsys on both
machines are the same (4.24V). So I’m a little perplexed. I’m not sure
what could provoke this behaviour outside of the Watcom compiler and
Fsys environments. Both are running Proc32.425L Apart from these,
everything in the sin ver are the same.

Any ideas?

Geoff Roberts

Geoff Roberts wrote:

Hi,

Here is a small piece of code that I thought would allow me to lock a
file by running seperately on two consoles or pterms. I can’t get the
first one to lock the file and I can’t figure out why. Any clues as to
what I’m doing wrong would be appreciated. lockdemo.c is simply an
arbitrary filename.

The OS is stock/standard QNX4.25 using Watcom 10.6. This snippet works
in QNX6. Is there something wrong with file locking in QNX4?

Thanks,

Geoff.

#include <stdio.h
#include <unistd.h
#include <errno.h
#include <fcntl.h
#include <unix.h

int main(void)
{
struct flock fl;
int fd, l;

fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 1;
fl.l_len = 400;
fl.l_pid = getpid();

if ((fd = open(“lockdemo.c”, O_RDWR)) == -1)
return;

printf("Press to try and get lock: ");fflush(stdout);
getchar();
printf(“Trying to get lock…”);fflush(stdout);

if ((l = fcntl(fd, F_SETLKW, &fl)) == -1)
{
perror(“fcntl”);
exit(-1);
}

printf(“got lock\n”);
printf("Press to release lock: ");fflush(stdout);
getchar();

fl.l_type = F_UNLCK;

if ((l = fcntl(fd, F_SETLK, &fl)) == -1)
{
perror(“fcntl”);
exit(-1);
}

printf(“Unlocked\n”);

close(fd);
}

Realtime Technology Systems Pty Ltd
2 Hadleigh Circuit
Isabella Plains
ACT 2905
AUSTRALIA

Phone: 61-2-6291 3833
Fax: 61-2-6291 3838
Mobile: 0408 673 993
Email: ger2@rtts.com.au