How to check if a file is open in C

I am working on a multi-threaded system where a file can be split between different streams based on file permissions. How to check if a file is open by another thread. thanks in advance

+8
c linux
source share
4 answers

You can use int flock (int fd, int operation); mark the file as locked, and also check if it is locked.

Apply or remove an advisory lock on the open file specified by fd. The argument operation is one of the following: LOCK_SH Place a shared lock. More than one process may hold a shared lock for a given file at a given time. LOCK_EX Place an exclusive lock. Only one process may hold an exclusive lock for a given file at a given time. LOCK_UN Remove an existing lock held by this process. 

flock should work in a threaded application if you open a file separately in each stream: several threads that can simultaneously receive a herd

There is more information about the pack and its potential flaws here :

+3
source share

To find out if a named file is already open on linux , you can scan the /proc/self/fd directory to see if the file is associated with a file descriptor. In the program below you can find a solution:

 DIR *d = opendir("/proc/self/fd"); if (d) { struct dirent *entry; struct dirent *result; entry = malloc(sizeof(struct dirent) + NAME_MAX + 1); result = 0; while (readdir_r(d, entry, &result) == 0) { if (result == 0) break; if (isdigit(result->d_name[0])) { char path[NAME_MAX+1]; char buf[NAME_MAX+1]; snprintf(path, sizeof(path), "/proc/self/fd/%s", result->d_name); ssize_t bytes = readlink(path, buf, sizeof(buf)); buf[bytes] = '\0'; if (strcmp(file_of_interest, buf) == 0) break; } } free(entry); closedir(d); if (result) return FILE_IS_FOUND; } return FILE_IS_NOT_FOUND; 

From your comment, it seems that you want to get the existing FILE * if it was already created by a previous call to fopen() in the file. The mechanism provided by the standard C library is not used to repeat all currently open FILE * . If there was such a mechanism, you could get its file descriptor using fileno() , and then request /proc/self/fd/# using readlink() , as shown above.

This means that you will need to use a data structure to manage public FILE * s. You will probably find a hash table using the file name as the key.

+2
source share

I don’t know many ways of multithreading in Windows, but you have many options if you work in Linux. Here is the FANTASTIC resource. You can also take advantage of any file locking features offered by the OS or the OS that are explicit (for example: fcntl ). Learn more about Linux locks here . Creating and manually managing your own mutex gives you more flexibility than otherwise. user814064 comment about flock() looks like the perfect solution, but there are never any settings issues!

Added sample code:

 #include <stdio.h> #include <stdlib.h> #include <pthread.h> FILE *fp; int counter; pthread_mutex_t fmutex = PTHREAD_MUTEX_INITIALIZER; void *foo() { // pthread_mutex_trylock() checks if the mutex is // locked without blocking //int busy = pthread_mutex_trylock(&fmutex); // this blocks until the lock is released pthread_mutex_lock(&fmutex); fprintf(fp, "counter = %d\n", counter); printf("counter = %d\n", counter); counter++; pthread_mutex_unlock(&fmutex); } int main() { counter = 0; fp = fopen("threads.txt", "w"); pthread_t thread1, thread2; if (pthread_create(&thread1, NULL, &foo, NULL)) printf("Error creating thread 1"); if (pthread_create(&thread2, NULL, &foo, NULL)) printf("Error creating thread 2"); pthread_join(thread1, NULL); pthread_join(thread2, NULL); fclose(fp); return 0; } 
+1
source share

If you do this in a shell, you can simply use lsof $filename .

0
source share

All Articles