Linux 判断进程是否已经运行的程序
2016-07-15 21:34
645 查看
bool ServerProcess::isAlreadyRunning() { #ifndef __linux__ WarningLog(<<"can't check if process already running on this platform (not implemented yet)"); return false; #else if(mPidFile.size() == 0) { // if no PID file specified, we do not make any check return false; } pid_t running_pid; std::ifstream _pid(mPidFile.c_str(), std::ios_base::in); if(!_pid.good()) { // if the file doesn't exist or can't be opened, just ignore return false; } _pid >> running_pid; _pid.close(); StackLog(<< mPidFile << " contains PID " << running_pid); Data ourProc = Data("/proc/self/exe"); Data otherProc = Data("/proc/") + Data(running_pid) + Data("/exe"); char our_exe[513], other_exe[513]; int buf_size; buf_size = readlink(ourProc.c_str(), our_exe, 512); if(buf_size < 0 || buf_size == 512) { // if readlink fails, just ignore return false; } our_exe[buf_size] = 0; buf_size = readlink(otherProc.c_str(), other_exe, 512); if(buf_size < 0 || buf_size == 512) { // if readlink fails, just ignore return false; } other_exe[buf_size] = 0; if(strcmp(our_exe, other_exe) == 0) { ErrLog(<<"already running PID: " << running_pid); return true; } return false; #endif }
相关文章推荐
- Linux下安装OMNeT++
- centos6.6安装hadoop2.6.0集群
- 分割flac文件
- linux 软件常见问题以及解决方案
- linux 分区、目录、挂载点
- 浅谈linux下find的最基本用法
- Linux-GCC学习(C/C++)
- 【linux c】c语言十进制数转换为十六进制输出_学习笔记_005
- WINDOWS环境下通过Xmanger远程连接CENTOS图形桌面【转】
- Linux下控制免驱USB摄像头的信息
- 模拟linux的 cat命令 读取文件
- 微软发布新版 Skype Linux 客户端
- Linux常用指令
- Linux基础篇十二——管道
- Linux基础(14)Linux的特殊权限
- 简单c/s程序(Linux c)
- Linux 服务器上建立用户并分配权限
- gdb调试学习(一) list命令的学习
- 如何在 Linux 上永久挂载一个 Windows 共享
- centos6.5 下安装 sqlplus