win and linux 代码
#ifndef __PUBLIC_SINGLEPROCINSTANCE_H__
#define __PUBLIC_SINGLEPROCINSTANCE_H__
#include <string>
#include <iostream>
#include "pub_utility_api/UtilityCommon.h"
namespace kbd_public
{
class PUB_UTILITY_API CSingleProcInstance
{
public:
CSingleProcInstance();
virtual ~CSingleProcInstance();
/**
* @brief 判断进程是否已经运行
* @param strUniqueName唯一标识
* @param bCaseSensitive大小写是否敏感,默认不敏感
* @return 已经运行返回true,未运行返回false
*/
static bool hasInstanceRunning(const std::string& strUniqueName, const bool bCaseSensitive = false);
};
}
#endif // __PUBLIC_SINGLEPROCINSTANCE_H__
@file SingleProcInstance.cpp
#include "pub_utility_api/SingleProcInstance.h"
#include <algorithm>
#ifdef WIN32
#include <Windows.h>
#else
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
#define TMPDIR "/dev/shm"
#endif
using namespace std;
#ifdef WIN32
class CGlobalCriticalSection
{
public:
CGlobalCriticalSection()
{
m_objMutex = NULL;
}
virtual ~CGlobalCriticalSection()
{
}
/*
@brief 加锁
@param const int nTimeOutMsec
@return
*/
bool enter(const int nTimeOutMsec)
{
int rc = WaitForSingleObject(m_objMutex, nTimeOutMsec);
return (rc == WAIT_OBJECT_0 || rc == WAIT_ABANDONED);
}
/*
@brief 解锁
@return
*/
void leave()
{
ReleaseMutex(m_objMutex);
}
/*
@brief 创建有名锁
@param char const * name 锁名
@return
*/
bool create(char const* name)
{
m_objMutex = CreateMutexA(NULL, false, name);
return m_objMutex != NULL;
}
/*
@brief 打开有名锁
@param char const * name 锁名
@return
*/
bool open(char const* name)
{
m_objMutex = OpenMutexA(MUTEX_ALL_ACCESS, true, name);
return m_objMutex != NULL;
}
/*
@brief 关闭锁
*/
void close()
{
CloseHandle(m_objMutex);
}
private:
HANDLE m_objMutex;
};
static CGlobalCriticalSection g_objGlobalMutex_;
kbd_public::CSingleProcInstance::CSingleProcInstance()
{
}
kbd_public::CSingleProcInstance::~CSingleProcInstance()
{
g_objGlobalMutex_.close();
}
bool kbd_public::CSingleProcInstance::hasInstanceRunning(const string& strUniqueName, const bool bCaseSensitive /*= false*/)
{
string strName = strUniqueName;
if (!bCaseSensitive)
{
std::transform(strName.begin(), strName.end(), strName.begin(), ::tolower);
}
strName = "__kbdct_" + strName;
if (!g_objGlobalMutex_.create(strName.c_str()))
{
//_assert(false);
return false;
}
if (!g_objGlobalMutex_.enter(0))
{
return true;
}
return false;
}
#else
kbd_public::CSingleProcInstance::CSingleProcInstance()
{
}
kbd_public::CSingleProcInstance::~CSingleProcInstance()
{
}
bool kbd_public::CSingleProcInstance::hasInstanceRunning(const string& strUniqueName, const bool bCaseSensitive /*= false*/)
{
string strName = strUniqueName;
if (!bCaseSensitive)
{
std::transform(strName.begin(), strName.end(), strName.begin(), ::tolower);
}
strName = TMPDIR"/__kbdct_" + strName;
int fdLockFile;
struct flock fl;
/* 打开锁文件 */
int nOldMask = umask(0);
fdLockFile = open(strName.c_str(), O_RDWR | O_CREAT, LOCKMODE);
umask(nOldMask);
if (fdLockFile < 0)
{
return true;
}
/* 对整个锁文件加写锁 */
fl.l_type = F_WRLCK; //记录锁类型:独占性写锁
fl.l_whence = SEEK_SET; //加锁区域起点:距文件开始处l_start个字节
fl.l_start = 0;
fl.l_len = 0; //加锁区域终点:0表示加锁区域自起点开始直至文件最大可能偏移量为止,不管写入多少字节在文件末尾,都属于加锁范围
if (fcntl(fdLockFile, F_SETLK, &fl) < 0)
{
if (EACCES == errno || EAGAIN == errno) //系统中已有该守护进程的实例在运行
{
close(fdLockFile);
return true;
}
else
{
return true;
}
}
return false;
}
#endif
浙公网安备 33010602011771号