C++ Windows进程管理
功能:
1.各个进程启动、挂起、恢复、停止等
2.监听进程的运行状态,进程退出(正常、非正常)时,通知用户
3.异步队列
4.线程安全
进程管理器类:
#ifndef __ProcessManager_ProcessManager_H__
#define __ProcessManager_ProcessManager_H__ #include "Process.h" #define PROCESSMANAGER_AUTO_MUTEX boost::recursive_mutex PROCESSMANAGER_AUTO_MUTEX_NAME;
#define PROCESSMANAGER_LOCK_AUTO_MUTEX boost::recursive_mutex::scoped_lock AutoMutexLock(PROCESSMANAGER_AUTO_MUTEX_NAME); /** Process Manager
*/
class ProcessManager : public QObject, public Singleton<ProcessManager>
{
Q_OBJECT
public:
/** Default constructor.
*/
ProcessManager(Log* pLog); /** Default destructor.
*/
~ProcessManager(void); public:
/** start
*/
Process* Start(const string& strName, const string& strCmdLine, bool bShowWnd = true); /** suspend
*/
void Suspend(Process* pProc); /** resume
*/
void Resume(Process* pProc); /** stop
*/
void Stop(Process* pProc); /** suspend all
*/
void SuspendAll(); /** resume all
*/
void ResumeAll(); /** stop all
*/
void StopAll(); /** Override standard Singleton retrieval.
*/
static ProcessManager& GetSingleton(void); /** Override standard Singleton retrieval.
*/
static ProcessManager* GetSingletonPtr(void); protected slots:
/** Notify finish
*/
void _NoifyFinished(Process* pProc); Q_SIGNALS:
/** Fire finish
*/
void _FireFinished(const string&); private:
typedef map<string, Process*> ProcessMap;
ProcessMap m_Processs; Log* m_pLog; PROCESSMANAGER_AUTO_MUTEX
}; #endif // __ProcessManager_ProcessManager_H__
#include "ProcessManager.h" //-----------------------------------------------------------------------
template<> ProcessManager* Singleton<ProcessManager>::m_pSingleton = ; ProcessManager* ProcessManager::GetSingletonPtr(void)
{
return m_pSingleton;
} //-----------------------------------------------------------------------
ProcessManager& ProcessManager::GetSingleton(void)
{
assert(m_pSingleton);
return *m_pSingleton;
} //-----------------------------------------------------------------------
ProcessManager::ProcessManager(Log* pLog)
{
if (pLog == NULL)
{
THROW_EXCEPTION("Log can not be null!", "ProcessManager");
} m_pLog = pLog;
} //-----------------------------------------------------------------------
ProcessManager::~ProcessManager(void)
{
StopAll();
} //-----------------------------------------------------------------------
Process* ProcessManager::Start( const string& strName, const string& strCmdLine, bool bShowWnd /*= true*/ )
{
PROCESSMANAGER_LOCK_AUTO_MUTEX ProcessMap::iterator i = m_Processs.find(strName);
if (i != m_Processs.end())
{
return i->second;
}
else
{
Process* pProc = new Process(strName, m_pLog);
connect(pProc, SIGNAL(_FireFinished(Process*)), this, SLOT(_NoifyFinished(Process*)), Qt::QueuedConnection);
m_Processs[strName] = pProc; pProc->Start(strCmdLine, bShowWnd); return pProc;
}
} //-----------------------------------------------------------------------
void ProcessManager::Suspend(Process* pProc)
{
PROCESSMANAGER_LOCK_AUTO_MUTEX pProc->Suspend();
} //-----------------------------------------------------------------------
void ProcessManager::Resume(Process* pProc)
{
PROCESSMANAGER_LOCK_AUTO_MUTEX pProc->Resume();
} //-----------------------------------------------------------------------
void ProcessManager::Stop(Process* pProc)
{
PROCESSMANAGER_LOCK_AUTO_MUTEX pProc->Stop();
} //-----------------------------------------------------------------------
void ProcessManager::SuspendAll()
{
PROCESSMANAGER_LOCK_AUTO_MUTEX ProcessMap::iterator i = m_Processs.begin();
for ( ; i != m_Processs.end(); ++i)
{
i->second->Suspend();
}
} //-----------------------------------------------------------------------
void ProcessManager::ResumeAll()
{
PROCESSMANAGER_LOCK_AUTO_MUTEX ProcessMap::iterator i = m_Processs.begin();
for ( ; i != m_Processs.end(); ++i)
{
i->second->Resume();
}
} //-----------------------------------------------------------------------
void ProcessManager::StopAll()
{
PROCESSMANAGER_LOCK_AUTO_MUTEX ProcessMap::iterator i = m_Processs.begin();
for ( ; i != m_Processs.end(); ++i)
{
i->second->Stop();
}
} //-----------------------------------------------------------------------
void ProcessManager::_NoifyFinished(Process* pProc)
{
PROCESSMANAGER_LOCK_AUTO_MUTEX emit _FireFinished(pProc->GetName()); ProcessMap::iterator i = m_Processs.find(pProc->GetName());
if (i != m_Processs.end())
{
m_Processs.erase(i);
}
delete pProc;
}
进程类:
#ifndef __ProcessManager_Process_H__
#define __ProcessManager_Process_H__ #include "PreDefine.h" #define PROCESS_AUTO_MUTEX boost::recursive_mutex PROCESS_AUTO_MUTEX_NAME;
#define PROCESS_LOCK_AUTO_MUTEX boost::recursive_mutex::scoped_lock AutoMutexLock(PROCESS_AUTO_MUTEX_NAME); /** Process
*/
class Process : public QObject
{
Q_OBJECT friend class ProcessManager;
protected:
/** Default constructor - used by ProcessManager.
Warning Do not call directly
*/
Process(const string& strName, Log* pLog); /** Default destructor.
*/
~Process(); public:
/** start process
*/
bool Start(const string& strCmdLine, bool bShowWnd = true); /** suspend process
*/
void Suspend(); /** resume process
*/
void Resume(); /** stop process
*/
void Stop(); /** is process runing
*/
bool IsRuning(); /** get name
*/
string GetName(); private:
/** listen to process is stoped ?
*/
void Listener(); Q_SIGNALS:
/** Fire ProcessManager this process is stoped
*/
void _FireFinished(Process*); protected:
/* Gets the last loading error
*/
string ProcessError(void); private:
string m_strName;
Log* m_pLog; HANDLE m_hPId;
HANDLE m_hTId;
boost::thread* m_pListener; PROCESS_AUTO_MUTEX
}; #endif // __ProcessManager_Process_H__
#include "Process.h" //-----------------------------------------------------------------------
Process::Process(const string& strName, Log* pLog)
{
if (pLog == NULL)
{
THROW_EXCEPTION("Log can not be null!", "Process");
} m_strName = strName;
m_pLog = pLog; m_hPId = NULL;
m_hTId = NULL;
m_pListener = NULL;
} //-----------------------------------------------------------------------
Process::~Process()
{ } //-----------------------------------------------------------------------
bool Process::Start(const string& strCmdLine, bool bShowWnd /** = true */)
{
PROCESS_LOCK_AUTO_MUTEX if (m_hPId != NULL)
{
//log
m_pLog->LogMessage("Process " + m_strName + " is started.", LMT_OUTBAR);
return true;
} //-----------Create Process-----------
STARTUPINFOA si;
ZeroMemory(&si, sizeof(STARTUPINFOA));
si.cb = sizeof(STARTUPINFOA);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = bShowWnd ? SW_SHOW : SW_HIDE; PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); BOOL bSuccess = CreateProcessA(NULL, (LPSTR)(strCmdLine.c_str()), NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi);
if (!bSuccess)
{
//log
m_pLog->LogMessage("Could not start process " + m_strName + ". System Error: " + ProcessError(), LMT_OUTBAR); //notify process manager to remove me
emit _FireFinished(this); return false;
} //log
m_pLog->LogMessage("Start process " + m_strName + " successful.", LMT_OUTBAR); m_hPId = pi.hProcess;
m_hTId = pi.hThread; //-----------Listener-----------
boost::function0<void> listenerFunc = boost::bind(&Process::Listener, this);
m_pListener = new boost::thread(listenerFunc); //-----------Resume-----------
ResumeThread(pi.hThread);
WaitForInputIdle(pi.hProcess, ); return true;
} //-----------------------------------------------------------------------
void Process::Suspend()
{
PROCESS_LOCK_AUTO_MUTEX if (m_hPId == NULL) return; SuspendThread(m_hTId);
} //-----------------------------------------------------------------------
void Process::Resume()
{
PROCESS_LOCK_AUTO_MUTEX if (m_hPId == NULL) return; ResumeThread(m_hTId);
} //-----------------------------------------------------------------------
void Process::Stop()
{
PROCESS_LOCK_AUTO_MUTEX if (m_hPId == NULL) return; //Kill Listener
m_pListener->interrupt();
m_pListener->detach();
delete m_pListener;
m_pListener = NULL; //Kill Process
TerminateProcess(m_hPId, );
m_hPId = NULL;
m_hTId = NULL; //log
m_pLog->LogMessage("Process " + m_strName + " is stoped.", LMT_OUTBAR); //notify process manager to remove me
emit _FireFinished(this);
} //-----------------------------------------------------------------------
bool Process::IsRuning()
{
PROCESS_LOCK_AUTO_MUTEX if (m_hPId == NULL) return false; return true;
} //-----------------------------------------------------------------------
string Process::GetName()
{
return m_strName;
} //-----------------------------------------------------------------------
void Process::Listener()
{
try
{
WaitForSingleObject(m_hPId, INFINITE); {
//local : avoid to always lock
PROCESS_LOCK_AUTO_MUTEX m_pListener->detach();
delete m_pListener;
m_pListener = NULL; m_hPId = NULL;
m_hTId = NULL; //log
m_pLog->LogMessage("Process " + m_strName + " is stoped.", LMT_OUTBAR); //notify process manager to remove me
emit _FireFinished(this);
}
}
catch(const boost::thread_interrupted&)
{
//interrupted
}
catch(...)
{
//other
}
} string Process::ProcessError( void )
{
LPVOID lpMsgBuf;
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &lpMsgBuf, , NULL); string strError = (char*)lpMsgBuf; LocalFree(lpMsgBuf); return strError;
}
C++ Windows进程管理的更多相关文章
- windows 进程管理器中的内存是什么意思?
*内存 - 工作集:私人工作集中的内存数量与进程正在使用且可以由其他进程共享的内存数量的总和. *内存 - 峰值工作集:进程所使用的工作集内存的最大数量. *内存 - 工作集增量:进程所使用的工作集内 ...
- 进程管理三大扩展工具htop
三大进程管理监控工具 HTOP 介绍: Htop是一款运行于Linux系统监控与进程管理软件,htop提供所有进程的列表,并且使用彩色标识出处理器.swap和内存状态.用户一般可以在top无法提供详尽 ...
- 理解Docker容器的进程管理
摘要: Docker在进程管理上有一些特殊之处,如果不注意这些细节中的魔鬼就会带来一些隐患.另外Docker鼓励"一个容器一个进程(one process per container)&qu ...
- windows进程/线程创建过程 --- windows操作系统学习
有了之前的对进程和线程对象的学习的铺垫后,我们现在可以开始学习windows下的进程创建过程了,我将尝试着从源代码的层次来分析在windows下创建一个进程都要涉及到哪些步骤,都要涉及到哪些数据结构. ...
- 初探Linux进程管理机制
转至:http://ixdba.blog.51cto.com/2895551/543737 一 .进程的概念和分类1.进程的概念 Linux是一个多用户多任务的操作系统.多用户是指多个用户可以在同一时 ...
- Windows内存管理[转]
本文主要内容:1.基本概念:物理内存.虚拟内存:物理地址.虚拟地址.逻辑地址:页目录,页表2.Windows内存管理3.CPU段式内存管理4.CPU页式内存管理 一.基本概念1. 两个内存概念物理内存 ...
- psutil--跨平台的进程管理
原文地址:http://www.jianshu.com/p/64e265f663f6 Python处理Windows进程 psutil(Python system and process utilit ...
- MPICH2在两台Ubuntu上安装(用mpd做进程管理)
本文在经过大量的实验终于不负众望成功的在两台Ubuntu 12.04上部署MPI的一个小型集群,MPICH2所用版本为mpich2-1.4.1,下载地址:http://www.mcs.anl.gov/ ...
- windows内存管理方式以及优缺点
Windows内存管理方式:页式管理,段式管理,段页式管理 页式管理 将各进程的虚拟空间(逻辑地址)划分为若干个长度相等的页,业内管理把内存空间(物理内存)按照页的大小划分为片或者页面,从而实现了离散 ...
随机推荐
- 分页组件 - layui.laypage
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 解决eclipse之ADT与SDK版本不一致问题
This Android SDK requires Android Developer Toolkit version … .Please update ADT to the latest versi ...
- 常见http status code
常见http status code 常见的状态码: HTTP: Status200– 服务器成功返回网页 HTTP: Status404– 请求的网页不存在 HTTP: Status503– 服务不 ...
- Hbase关于Java常用API举例
1. HBase相关对Admin操作的的API封装在HBaseAdmin中,封装了HBase常用操作的API 使用方法: pom.xml <!-- https://mvnrepository.c ...
- document.all.wb.ExecWB
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri=&quo ...
- Js实现简单的洗牌
基础篇 洗牌采用的是,每一张牌,与后面随机一张牌来交换位置. 扑克牌采用编码制(如,0代表红桃A,依次类推)为了编码方便,扑克牌不含大小王,故52张. 一.扑克牌的了解 扑克(英文:Poker) 一副 ...
- zend studio 9 字体,颜色,快捷键等相关设置
1.zend studio 9可以破解吗? 可以的,具体破解步骤查看:http://www.geekso.com/ZendStudio9-key/ 2.如何将zend studio 9的默认GBK编码 ...
- Joomla软件功能介绍与开源程序大比拼Joomla,wordpress,Drupal哪个好?
Joomla 软件功能介绍: Joomla!是一套在国外相当知名的内容管理系统 (Content Management System, CMS),它属于Portal(企业入口网站)类型,顾名思义 ...
- split 分割 字符串(分隔符如:* ^ : | , .) 及注意点
http://blog.sina.com.cn/s/blog_b6487d470101g0hp.html [1]单个符号作为分隔符 String address="上海|上海 ...
- python二进制相关
https://docs.python.org/3/library/struct.html#module-struct