在CentOS系统中,对readdir(读取目录)进行并发控制的方法主要有以下几种:
文件锁是一种常见的并发控制机制,可以确保在同一时间只有一个进程或线程访问特定的资源。
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main() {
int fd = open("example_dir", O_RDONLY);
if (fd == -1) {
perror("open");
return 1;
}
// 获取共享锁
struct flock lock;
lock.l_type = F_WRLCK; // 写锁
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
if (fcntl(fd, F_SETLK, &lock) == -1) {
perror("fcntl");
close(fd);
return 1;
}
// 执行readdir操作
struct dirent *entry;
DIR *dir = opendir("example_dir");
if (dir == NULL) {
perror("opendir");
close(fd);
return 1;
}
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
closedir(dir);
// 释放锁
lock.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &lock) == -1) {
perror("fcntl unlock");
}
close(fd);
return 0;
}
在多线程环境中,可以使用互斥锁(mutex)来保护对目录的访问。
#include <pthread.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
pthread_mutex_t dir_mutex = PTHREAD_MUTEX_INITIALIZER;
void *read_dir(void *arg) {
pthread_mutex_lock(&dir_mutex);
DIR *dir = opendir("example_dir");
if (dir == NULL) {
perror("opendir");
pthread_mutex_unlock(&dir_mutex);
return NULL;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
closedir(dir);
pthread_mutex_unlock(&dir_mutex);
return NULL;
}
int main() {
pthread_t threads[5];
for (int i = 0; i < 5; ++i) {
if (pthread_create(&threads[i], NULL, read_dir, NULL) != 0) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < 5; ++i) {
pthread_join(threads[i], NULL);
}
return 0;
}
信号量是一种更高级的同步机制,可以用来控制对共享资源的访问。
#include <semaphore.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
sem_t dir_sem;
void *read_dir(void *arg) {
sem_wait(&dir_sem);
DIR *dir = opendir("example_dir");
if (dir == NULL) {
perror("opendir");
sem_post(&dir_sem);
return NULL;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
closedir(dir);
sem_post(&dir_sem);
return NULL;
}
int main() {
sem_init(&dir_sem, 1, 1); // 初始化信号量,初始值为1
pthread_t threads[5];
for (int i = 0; i < 5; ++i) {
if (pthread_create(&threads[i], NULL, read_dir, NULL) != 0) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
for (int i = 0; i < 5; ++i) {
pthread_join(threads[i], NULL);
}
sem_destroy(&dir_sem);
return 0;
}
某些文件系统支持文件级别的锁,可以通过fcntl系统调用实现。
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main() {
int fd = open("example_dir", O_RDONLY);
if (fd == -1) {
perror("open");
return 1;
}
// 获取文件锁
struct flock lock;
lock.l_type = F_WRLCK; // 写锁
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
if (fcntl(fd, F_SETLK, &lock) == -1) {
perror("fcntl");
close(fd);
return 1;
}
// 执行readdir操作
struct dirent *entry;
DIR *dir = opendir("example_dir");
if (dir == NULL) {
perror("opendir");
close(fd);
return 1;
}
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
closedir(dir);
// 释放锁
lock.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &lock) == -1) {
perror("fcntl unlock");
}
close(fd);
return 0;
}
选择哪种方法取决于具体的应用场景和需求。文件锁和线程同步机制适用于大多数情况,而信号量和文件系统级别的锁则提供了更高级的控制能力。在实际应用中,应根据具体情况选择最合适的并发控制方法。