zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    mysql的CURRENT_TIMESTAMP【转】
    php开发中emoji表情的问题3种方法轻松处理【转】
    JavaScript 正则表达式【转】
    使用 内置函数strtok()函数实现 loadrunner 字符串替换
    python打开文件失败,报错'gbk' codec can't decode byte 0xbf in position 2: illegal multibyte sequence
    txt文本程序 打开python文件 另存为原来的文件名,不能覆盖原来的文件解决
    linux 文件解压
    tar.xz 解压
    设置xampp开机自动启动
    Can’t connect to local MySQL server through socket的解决方法
  • 原文地址:https://www.cnblogs.com/kaishan1990/p/7047372.html
Copyright © 2011-2022 走看看