Messages in this thread |  | | | Date | Sun, 2 Aug 1998 00:27:25 +1000 | | From | Richard Gooch <> | | Subject | Races in open(2) |
| |
Hi, all. Appended below is a programme which demonstrates a race condition in the open(2) code. I pass the O_CREAT | O_EXCL flags, and sometimes I get ENOENT returned, which I believe should never happen. The test programme launches a number of threads which alternatively create and unlink the file. A typical output from the programme (on my dual PPro system), with kernel 2.1.113 is: Running test with 10 threads Error opening file No such file or directory Failure on iteration 21 Error opening file No such file or directory Failure on iteration 47 Error opening file No such file or directory Failure on iteration 39 Error opening file No such file or directory Failure on iteration 27 Error opening file No such file or directory Failure on iteration 16
Test code appended. Does anyone have any inkling as to why this is happening?
Regards,
Richard.... =============================================================================== /* exclusive-open.c Main file for exclusive-open (stress test for O_EXCL file open).
Copyright (C) 1998 Richard Gooch
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Richard Gooch may be reached by email at karma-request@atnf.csiro.au The postal address is: Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia. */
/* This programme will stress tests the open(2) system call when using the O_EXCL flag for locking.
Written by Richard Gooch 1-AUG-1998
Last updated by Richard Gooch 1-AUG-1998
*/ #define _REENTRANT #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sched.h> #include <errno.h>
#define NUM_THREADS 10 #define NUM_ITER 100 #define ERRSTRING strerror (errno) #define LOCKFILE "/tmp/O_EXCL-test"
/* Private functions */ static void *thread_main (void *info); static void delay ();
/* Private data */ static volatile int open_count = 0;
int main (int argc, char **argv) { int num_threads = NUM_THREADS; int count; pthread_t thread; char usage_string[] = "Usage:\texclusive-open [num_threads]"; unlink (LOCKFILE); if (argc > 2) { fprintf (stderr, "%s\n", usage_string); exit (1); } if (argc == 2) num_threads = atoi (argv[1]); fprintf (stderr, "Running test with %d threads\n", num_threads); for (count = 0; count < num_threads - 1; ++count) { if (pthread_create (&thread, NULL, thread_main, NULL) != 0) { fprintf (stderr, "Error creating thread\t%s\n", ERRSTRING); exit (1); } } thread_main (NULL); } /* End Function main */
/* Private functions follow */ static void *thread_main (void *info) { int fd = -1; int count; for (count = 0; count < NUM_ITER; ++count) { while (fd < 0) { if ( ( fd = open (LOCKFILE, O_RDWR | O_CREAT | O_EXCL, S_IRWXU) ) < 0 ) { if (errno != EEXIST) { fprintf (stderr, "Error opening file\t%s\n", ERRSTRING); fprintf (stderr, "Failure on iteration %d\n", count); exit (1); } } if (fd < 0) { /*sched_yield ();*/ continue; } } if (open_count != 0) { fprintf (stderr, "open_count: %d !\n", open_count); exit (1); } ++open_count; delay (); --open_count; close (fd); fd = -1; if (unlink (LOCKFILE) != 0) { fprintf (stderr, "Error unlinking\t%s\n", ERRSTRING); exit (1); } } return (NULL); } /* End Function thread_main */ static void delay () { sched_yield (); } /* End Function delay */ - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.altern.org/andrebalsa/doc/lkml-faq.html
|  |