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;
    }
  • 相关阅读:
    整数的位数,及反转
    判断1~n有多少个1
    C语言常用字符串函数
    C语言类型转换原理
    printf()函数压栈a++与++a的输出
    49 丑数( 时间空间效率的平衡)
    42 连续子数组的最大和(时间效率)
    41 数据流中的中位数(时间效率)
    40 最小的K个数(时间效率)
    一、简介 ELO商户类别推荐有助于了解客户忠诚度
  • 原文地址:https://www.cnblogs.com/kaishan1990/p/7047372.html
Copyright © 2011-2022 走看看