From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1164f8,f0daca78db95c667,start X-Google-Attributes: gid1164f8,public X-Google-ArrivalTime: 1992-01-31 15:40:20 PST Path: sparky!uunet!cs.utexas.edu!uwm.edu!linac!att!pacbell.com!lll-winken!bryce.llnl.gov!mark From: mark@mesa.llnl.gov (Mark Spruiell) Newsgroups: comp.sys.dec Subject: File Locking Problem Message-ID: <117034@lll-winken.LLNL.GOV> Date: 31 Jan 92 23:30:31 GMT Sender: usenet@lll-winken.LLNL.GOV Reply-To: mark@mesa.llnl.gov Organization: Lawrence Livermore National Laboratory Nntp-Posting-Host: bryce.llnl.gov 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 #include #include #include 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