?? modify.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 + -