Linux保证运行一个实例

1.

const int PATH_MAX = 1024;   // 默认最大路径长度
inline std::string current_exe_name()
{
    char buf[PATH_MAX] = {0};

    int ret = readlink("/proc/self/exe", buf, PATH_MAX);
    if (ret < 0 || ret >= PATH_MAX) {
        return "";
    }

    std::string path(buf);
    std::size_t pos = path.find_last_of("/");
    if (pos == std::string::npos) {
        return "";
    }

    path = path.substr(pos + 1, path.size() - 1);

    return path;
}

inline bool check_single_instance()
{
    // 打开或创建一个文件
    std::string file_path = "./pid.lck";
    int fd = open(file_path.c_str(), O_RDWR | O_CREAT, 0666);
    if (fd < 0)  {
        printf("Open file failed, error : %s", strerror(errno));
        exit(1);
    }

    // 将该文件锁定
    // 锁定后的文件将不能够再次锁定
    struct flock fl;
    fl.l_type = F_WRLCK; // 写文件锁定
    fl.l_start = 0;
    fl.l_whence = SEEK_SET;
    fl.l_len = 0;
    int ret = fcntl(fd, F_SETLK, &fl);
    if (ret < 0) {
        if (errno == EACCES || errno == EAGAIN) {
            printf("%s already locked, error: %s
", file_path.c_str(), strerror(errno));
            close(fd);
            return false;
        }
    }

    // 锁定文件后,将该进程的pid写入文件
    char buf[16] = {0};
    sprintf(buf, "%d", getpid());
    ftruncate(fd, 0);
    ret = write(fd, buf, strlen(buf));
    if (ret < 0) {
        printf("Write file failed, file: %s, error: %s
", file_path.c_str(), strerror(errno));
        close(fd);
        exit(1);
    }

    // 函数返回时不需要调用close(fd)
    // 不然文件锁将失效
    // 程序退出后kernel会自动close
    return true;
}

2

inline bool check_single_instance()
{
    // 打开或创建一个文件
    std::string file_path = "./pid.lck";
    int fd = open(file_path.c_str(), O_RDWR | O_CREAT, 0666);
    if (fd < 0)  {
        printf("Open file failed, error : %s", strerror(errno));
        exit(1);
    }

    // 将该文件锁定
    // 锁定后的文件将不能够再次锁定
    int ret = lockf(fd, F_TLOCK, 0);
    if (ret < 0) {
        if (errno == EACCES || errno == EAGAIN) {
            printf("%s already locked, error: %s
", file_path.c_str(), strerror(errno));
            close(fd);
            return false;
        }
    }

    // 锁定文件后,将该进程的pid写入文件
    char buf[16] = {0};
    sprintf(buf, "%d", getpid());
    ftruncate(fd, 0);
    ret = write(fd, buf, strlen(buf));
    if (ret < 0) {
        printf("Write file failed, file: %s, error: %s
", file_path.c_str(), strerror(errno));
        close(fd);
        exit(1);
    }

    // 函数返回时不需要调用close(fd)
    // 不然文件锁将失效
    // 程序退出后kernel会自动close
    return true;
}
原文地址:https://www.cnblogs.com/kaishan1990/p/7047372.html