进程间互斥的实现方法

在Linux中,对进程加锁的主要目的是防止多个实例的进程同时运行,或者是对共享资源进行互斥访问。可以使用以下的方式来实现进程间锁定:

一、文件锁

使用flock函数来实现文件锁定的机制,用于实现进程间的互斥锁定。可以在文件描述符上加锁,确保同一时间只有一个进程可以访问共享资源。
例如:如果你有多个进程需要创建同一个文件夹,且希望这些进程在创建文件夹的操作上互斥(即同时只有一个进程能够创建文件夹),可以使用文件锁定机制来实现互斥。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/file.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>

#define LOCK_FILE "/tmp/mylockfile"
#define DIR_TO_CREATE "/tmp/mydir"

void create_directory() {
    if (mkdir(DIR_TO_CREATE, 0755) == -1) {
        if (errno == EEXIST) {
            printf("Directory already exists\n");
        } else {
            perror("mkdir");
            exit(EXIT_FAILURE);
        }
    } else {
        printf("Directory created successfully\n");
    }
}

int main() {
    int fd = open(LOCK_FILE, O_CREAT | O_RDWR, 0666);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    if (flock(fd, LOCK_EX) == -1) {
        perror("flock");
        close(fd);
        exit(EXIT_FAILURE);
    }

    create_directory();

    if (flock(fd, LOCK_UN) == -1) {
        perror("flock");
        close(fd);
        exit(EXIT_FAILURE);
    }

    close(fd);
    return 0;
}

二、使用 PID 文件(简单互斥方法)

使用一个 PID 文件来实现进程间的互斥。每个进程在尝试创建文件夹之前,先检查并创建 PID 文件,如果文件已经存在并且进程还在运行,则退出。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <sys/stat.h>
#include <errno.h>

#define PID_FILE "/tmp/myapp.pid"
#define DIR_TO_CREATE "/tmp/mydir"

void create_directory() {
    if (mkdir(DIR_TO_CREATE, 0755) == -1) {
        if (errno == EEXIST) {
            printf("Directory already exists\n");
        } else {
            perror("mkdir");
            exit(EXIT_FAILURE);
        }
    } else {
        printf("Directory created successfully\n");
    }
}

int check_pid_file(const char *pid_file) {
    int fd = open(pid_file, O_RDWR | O_CREAT, 0666);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    char buf[16];
    if (read(fd, buf, sizeof(buf) - 1) > 0) {
        buf[sizeof(buf) - 1] = '\0';
        pid_t pid = strtol(buf, NULL, 10);
        if (pid > 0 && kill(pid, 0) == 0) {
            close(fd);
            return -1;
        }
    }

    snprintf(buf, sizeof(buf), "%d", getpid());
    ftruncate(fd, 0);
    lseek(fd, 0, SEEK_SET);
    write(fd, buf, strlen(buf));
    close(fd);
    return 0;
}

int main() {
    if (check_pid_file(PID_FILE) == -1) {
        printf("Another instance is already running\n");
        exit(EXIT_FAILURE);
    }

    create_directory();

    unlink(PID_FILE);
    printf("Process finished, PID file removed\n");

    return 0;
}

总结
flock 和 fcntl 是基于文件描述符的锁定机制,适用于跨进程的互斥。
PID 文件 提供了一种简单的方法来确保只有一个进程实例在运行,适用于独占运行的进程。
选择合适的方法取决于具体的使用场景。如果需要对文件操作进行互斥访问,flock 和 fcntl 是不错的选择。如果只需要确保单实例运行,PID 文件是一个简单有效的方法。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Comedy_宁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值