Linux环境下C++只允许单个程序运行
来源:互联网 发布:ubuntu设置图形界面 编辑:程序博客网 时间:2024/04/29 23:32
本程序在Ubuntu和Centos系统下能够正常使用!
具体实现的代码:
bool isSingleProcess (){ long pid; char full_name[1024] = {0}; char proc_name[1024] = {0}; int fd; pid = getpid(); cout << "pid = " << pid; sprintf(full_name, "/proc/%ld/cmdline", pid); if (access(full_name, F_OK) == 0) { fd = open (full_name, O_RDONLY); if (fd == -1) return false; read (fd, proc_name, 1024); close (fd); } else return false; char self_proc_name[512] = {0}; char * p = proc_name; int pt = 0; while (*p != ' ' && *p != '\0') { self_proc_name[pt] = *p; p++; pt++; } string self_final_name = basename(self_proc_name); cout << "self_final_name = " << self_final_name << endl << flush; DIR *dir; struct dirent * result; dir = opendir ("/proc"); while ((result = readdir (dir)) != NULL) { if (! strcmp(result->d_name, ".") || ! strcmp (result->d_name, "..") || ! strcmp (result->d_name, "thread-self") || ! strcmp (result->d_name, "self") || atol (result->d_name) == pid) continue; memset(full_name, 0, sizeof(full_name)); memset(proc_name, 0, sizeof(proc_name)); sprintf(full_name, "/proc/%s/cmdline", result->d_name); if (access(full_name, F_OK) == 0) { fd = open (full_name, O_RDONLY); if (fd == -1) continue; read (fd, proc_name, 1024); close (fd); char *q = proc_name; pt = 0; memset(self_proc_name, 0, sizeof (self_proc_name)); while (*q != ' ' && *q != '\0') { self_proc_name[pt] = *q; q++; pt++; } string other_final_name = basename(self_proc_name); if (self_final_name == other_final_name) { cout << "full_name = " << full_name << endl << flush; cout << "other_final_name: " << other_final_name << endl; return true; } } } return false;}
在main函数中的开始添加以下内容:
int main (int argc, char ** argv){// judge single if (isSingleProcess()) { cout << "process is running in another place" << endl << flush; return 0; } ...... return 0;}
2 0
- Linux环境下C++只允许单个程序运行
- linux下c/c++实例之三只允许单个程序运行
- linux下C程序:运行单个实例
- winform只允许单个运行
- 程序只允许运行一次
- 只允许运行一个程序
- C#winform中只允许程序运行一个实例
- 只允许程序运行一个实例
- c#只允许运行一个程序(转载)
- VC程序只允许运行一次
- 只允许程序运行一次的代码
- Wince 程序只允许同时运行一个
- 只允许运行一个C#程序
- Linux/Unix环境下计算C程序运行时间
- Linux/Unix环境下计算C程序运行时间
- Linux/Unix环境下计算C程序运行时间
- Linux/Unix环境下计算C程序运行时间
- 只允许运行一个实例(C#)
- nodejs群聊和私聊
- Demo-jQuery获取焦点&文本框大小变化
- hdu 1217 Arbitrage (Floyd算法)
- 修改UITextfield的Placeholder字体的颜色
- 【HDU 1069】Monkey and Banana(DP)
- Linux环境下C++只允许单个程序运行
- input标签file类型,选择多个文件进行上传
- HBuilder标题栏如何穿标题呢
- 从今天开始,做一个测试工程师
- 2821 天使之城
- iOS TableView给力动画的简单实现
- Centos 6.5 压缩-解压方法
- readOGR加载shape文件时报错
- SQL Server 2008 数据库同步的两种方式(SQL JOB)