* File Locking Problem
@ 1992-01-31 23:30 Mark Spruiell
0 siblings, 0 replies; only message in thread
From: Mark Spruiell @ 1992-01-31 23:30 UTC (permalink / raw)
Hello netters,
I've got a problem on a DECstation 5000 running Ultrix 4.2. It can be
summarized as follows:
I create a file, request a write lock, write some data, and
close the file without unlocking the file. Unfortunately, the
file remains locked after the program terminates!!!
The man page for fcntl states
All locks associated with a file for a given process are removed
when a file descriptor for that file is closed by that process or
the process holding that file descriptor terminates. Locks are
not inherited by a child process in a fork(a)2 system call.
I've written a small test program to demonstrate this problem. It
checks to see if its output file exists; if so, it displays the lock
status of that file. If the file doesn't exist, it creates it and
closes it (without unlocking it). Below is a transcript of my
experiments:
{mesa:85} locktest
Created file XXjunkXX with write lock
{mesa:86} locktest
XXjunkXX: File is write locked by process 23145
This seems too obvious to be a bug in the OS, so what's my problem?
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
main(int argc, char **argv)
{
int fd, i, err;
struct flock lock;
char *fname = "XXjunkXX";
/* if file exists, display its lock status */
if (!access (fname, F_OK)) {
fd = open (fname, O_RDONLY);
if (fd == -1) {
perror (argv[0]);
exit (1);
}
lock.l_whence = 0;
lock.l_start = 0L;
lock.l_len = 0L;
err = fcntl (fd, F_GETLK, &lock);
if (err) {
perror (argv[0]);
exit (1);
}
printf ("%s:\t", fname);
switch (lock.l_type) {
case F_UNLCK:
printf ("File is not locked\n");
break;
case F_RDLCK:
printf ("File is read locked by process %d\n", lock.l_pid);
break;
case F_WRLCK:
printf ("File is write locked by process %d\n", lock.l_pid);
break;
}
}
else { /* file doesn't exist, create it */
fd = open (fname, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
if (fd == -1) {
perror (argv[0]);
exit (1);
}
lock.l_whence = 0; /* write lock whole file */
lock.l_start = 0L;
lock.l_len = 0L;
lock.l_type = F_WRLCK;
err = fcntl (fd, F_SETLK, &lock);
if (err) {
perror (argv[0]);
exit (1);
}
write (fd, "Hello, world!\n", 14);
/* close file without unlocking... */
close (fd);
printf ("Created file %s with write lock\n", fname);
}
}
-----------------------------------
Mark Spruiell (mark@mesa.llnl.gov)
who works at but does not represent
Lawrence Livermore National Laboratory
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~1992-01-31 23:30 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1992-01-31 23:30 File Locking Problem Mark Spruiell
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox