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

📄 modify.cpp

📁 多进程例子程序,IO操作在不同的进程中!
💻 CPP
字号:
#include <iostream.h>
#ifndef WIN32
#include<unistd.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/wait.h>
#endif


#include "IOHandler.h"
#include "ModifyPassword.h"
#include <stdlib.h>
#ifndef WIN32
#include <signal.h>
#else
typedef unsigned int pid_t;
#endif
const int UP_RECORD = 20000;
void sigCleanup(int);

bool bRuning = true;

class Child 
{
public:
    Child(pid_t pid, IOHandler* h)
	{
		pid_ = pid;
		handler_ = h;
	}

	virtual ~Child()
	{
		if(handler_ != NULL)
		{
			delete handler_;
			handler_ = NULL;
		}
	}
public:
	pid_t	pid_;
private:
    		
    int		status_;		
    IOHandler*	handler_;
    
};


typedef list<Child*>ChildList;
typedef list<Child*>::iterator ChildIterator;

class FindChild
{
public:
	FindChild(pid_t pid)
		:pid_(pid)
	{

	}

	bool IsEQ(Child*pInfo)
	{
			
		return pid_ == pInfo->pid_;
		
	}
	
protected:
private:
	pid_t pid_;
};
#define PROCESSCOUNT 5
#define ONCECOUNT 1000
#define DEFAULTCOUNT 80 * ONCECOUNT

int main(int argc,char* argv[])
{

	if(argc == 1)
	{
		cout << "User: modify domaintype" << endl;
		return 0;
	}
#ifndef WIN32
	 if(fork() != 0)
	 {

		 exit(0);
	 }
#endif




	int nCount = UP_RECORD;	
	
	ChildList processlist;
	int nReadCount = 0;
	srand(time(NULL));
	int nErrorTime = 0;
	
	IOHandler* pIOHandle = NULL;
	while(bRuning && nReadCount < DEFAULTCOUNT)
	{

		if(processlist.size() < PROCESSCOUNT )
		{
		    pid_t pid = -1;
			if(NULL == pIOHandle)
			{
				pIOHandle = new ModifyPassword;
				pIOHandle->Init(argv[1]);
				int nReaded = pIOHandle->input(ONCECOUNT);
				if(nReaded == 0)
				{
					bRuning = false;
					continue;
				}
				nReadCount += nReaded;
#ifndef WIN32
				pid = fork();
#endif			
		
			}
			
			switch(pid)
			{
			case 0:
				{
#ifndef WIN32
					sleep(2);
#endif
					
					pIOHandle->output();
					delete pIOHandle;

					exit(101);
				}
				break;
			case -1:
				{
					if(nErrorTime ++ > 3)
					{
						exit(0);
					}
					cout << "fork error!" << endl;
				}
				break;
			default:
				{
					nErrorTime = 0;
					Child* pChild = new Child(pid,pIOHandle);					
					processlist.push_back(pChild);
					pIOHandle = NULL;
					
				
					

				}
				break;
			}

		}
		else
		{
			pid_t pid = -1;
			int status = 0;
#ifndef WIN32
			if( (pid = wait(&status)) > 0)
			{
				
				ChildIterator cit = processlist.begin();
				
				for(;cit != processlist.end(); )
				{
					Child * pChild = *cit;
					if(FindChild(pid).IsEQ(pChild))
					{
						processlist.erase(cit);			
						delete pChild;
						break;
					}
					else
					{
						cit ++;
					}
				
					
				}
				
			}
#endif
		
		}

	}


	
	
	
	
	
	pid_t pid = -1;
	int status = 0;
#ifndef WIN32
	while(processlist.size() > 0)
	{
		if ((pid = wait(&status)) > 0)
		{
			/*
			ChildIterator cit = find_if(processlist.begin(),processlist.end(),FindChild(pid));
			
			if(cit != processlist.end())
			{
				Child * pChild = *cit;
				processlist.erase(cit);			
				delete pChild;
				cout << "process " << pid << " deathed" << endl;
			}
			//*/


				ChildIterator cit = processlist.begin();
				
				for(;cit != processlist.end(); )
				{
					Child * pChild = *cit;
					if(FindChild(pid).IsEQ(pChild))
					{
						processlist.erase(cit);			
						delete pChild;
						break;
					}
					else
					{
						cit ++;
					}
				
					
				}
			
		}
	}

#endif




	
	return 0;
}


void sigCleanup(int sig)
{ 
	
} 



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -