?? procpool.h
字號:
//-----------------------------------------------------------------------------
//進程池基類,運行環境UNIX,LINUX
//使用方法:需要應用程序繼承并實現自己的WaitTask 和 doTask方法,
//主進程首先生成應用類對象,調用init方法啟動初始進程,然后循環在ScanChild處
//作者:gaozh
//------------20070727---------------------------------------------------------
#ifndef __PROC_POOL_H_
#define __PROC_POOL_H_
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
#include <assert.h>
#include <sys/ipc.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/sem.h>
#include <sys/wait.h>
#include "SimpleLog.h"
using namespace SIMPLELOG;
using namespace std;
//默認的一些參數
const int DEFAULT_MAXNUM=10;
const int DEFAULT_INITNUM=5;
const int DEFAULT_FREELOWNUM=1;
const int DEFAULT_FREEHIGHNUM=5;
const int DEFAULT_INCNUM=1;
const int DEFAULT_DECNUM=1;
typedef enum
{
CS_WAITING=0,
CS_PROCESSING,
CS_UNKNOWN
}CHILD_STATUS;
class ProcPool
{
class ChildInfo
{
public:
pid_t pid;//進程id
int pfd;//管道id
CHILD_STATUS state;//狀態
public:
ChildInfo(){pid=-1;pfd=-1;state=CS_UNKNOWN;};
~ChildInfo(){};
//只能由子進程調用,父進程調用無意義
void NotifyBusy(){
CHILD_STATUS status=CS_PROCESSING;
write(pfd,&status,sizeof(status));
}
//只能由子進程調用,父進程調用無意義
void NotifyFree(){
CHILD_STATUS status=CS_WAITING;
write(pfd,&status,sizeof(status));
}
};
class semaphore
{
private:
int _sid;//信號量的值
int _semnum;//信號量的數量
key_t _keyval;//鍵值
public:
semaphore( key_t& keyval,int& numsems );
semaphore( );
virtual ~semaphore( ){ remove( ); };
bool lock( int seq=0 );
bool unlock( int seq=0 );
void remove( );
bool create();
bool init(int seq ,int value);
};
private:
int MaxNum;//最大進程數目
int FreeLowNum;//最低空閑進程數目
int FreeHighNum;//最高空閑進程數目
int IncNum;//每次增加的進程數
int DecNum;//每次減少的進程數
int chld_num;//總的進程數
int chld_avail;//總的空閑進程數
int selectTimeOut;//select的超時時間,單位毫秒
ChildInfo* CldInfo;//子進程狀態
/**
* Private copy constructor, to prevent tampering.
*/
ProcPool(const ProcPool &t) {};
ProcPool &operator=(const ProcPool &t) {return *(this);}
protected:
static SimpleLog *__logfile;
semaphore *_acceptlock;
int InitNum;//初始子進程數目
public:
ProcPool(SimpleLog &logfile,int& max,int& init,int &low,int& high,int& inc,int& dec);
ProcPool(SimpleLog &logfile,int& max,int& init,int &low,int& high,int& inc,int& dec,int &timeout);
ProcPool(SimpleLog &logfile);
virtual ~ProcPool();
public:
virtual bool Init();//啟動初始數目的子進程
bool IncreaseChld(int &num);//增加指定數目的子進程
void KillChld(pid_t &pid,int idx=-1);//終止指定子進程
void KillIdle();//終止多余的空閑子進程
void KillAll();//終止所有的子進程
virtual void *WaitTask(){ return NULL; };//子進程任務獲取函數,需要子類重載此函數
virtual bool doTask(void *task){ return true; };//子進程任務獲取函數,需要子類重載此函數
int ScanChild();//掃描子進程并進行維護,主進程需要循環調用此函數
virtual int ClearUp();//清理回收函數,程序結束時調用
//ChildInfo *ViewPool();
virtual void PrintPool();
void warn();
virtual void ChildProc(int& idx);//子進程函數
};
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -