日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

在Linux下防止某個程序被運(yùn)行兩次的方法

;通過文件鎖來實(shí)現(xiàn),在程序運(yùn)行的一開始,檢查某文件是否存在,如果存在則說明改程序已經(jīng)在運(yùn)行了,如果不存在則利用open語句創(chuàng)建該文件,程序退出時關(guān)閉并刪除此文件 。static char file_lock[sizeof(ctl_addr.sun_path)] = /var/run/file.pid;static bool file_lock_created = FALSE;static intcreate_lock(void){ int fd = open(file_lock, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, S_IRUSR | S_IRGRP | S_IROTH); if (fd < 0) { if (errno == EEXIST) { fprintf(stderr, "file: lock file "%s" already existsn", file_lock); exit_file(10); } else { fprintf(stderr, "file: unable to create lock file "%s" (%d %s)n" , file_lock, errno, strerror(errno)); exit_file(1); } } file_lock_created = TRUE; return fd;}static boolfill_lock(int lockfd){ char buf[30]; /* holds "n" */ pid_t pid; int len; pid = getpid(); len = snprintf(buf, sizeof(buf), "\un", (unsigned int) pid); bool ok = len > 0 && write(lockfd, buf, len) == len; close(lockfd); return ok;}static voiddelete_lock(void){ if (file_lock_created) { //delete_ctl_socket(); unlink(file_lock); /* is noting failure useful? */ }}

    推薦閱讀