[Linux] 检查是否已有进程在运行
出处:sblim-sfcb-1.4.9 / sfcBroker.c
int process_is_running() { #define STRBUF_LEN 512 #define BUF_LEN 30 struct dirent *dp = NULL; char *strbuf = malloc(STRBUF_LEN); char *buf = malloc(BUF_LEN); int mypid = getpid(); int ret = 0; DIR *dir = opendir("/proc"); while ((dp = readdir(dir)) != NULL) { if (isdigit(dp->d_name[0])) { sprintf(buf, "/proc/%s/exe", dp->d_name); memset(strbuf, 0, STRBUF_LEN); if (readlink(buf, strbuf, STRBUF_LEN) == -1) continue; if (strstr(strbuf, "进程名字") != NULL) { ret = strtol(dp->d_name, NULL, 0); if (ret == mypid) { ret = 0; continue; } break; } } } closedir(dir); free(buf); free(strbuf); return(ret); }