⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 procpool.h

📁 自动调整大小的进程池类的实现。 一个运用进程池的server类。 一个socket类。
💻 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 + -