#include "windows.h"
#include "iostream"
#include "stdio.h"
using namespace std;
static LPCTSTR q_szMutexName="w2kdg.ProcTerm.mutex.Suicide";
HANDLE StartClone(){
TCHAR szFilename[MAX_PATH];
GetModuleFileName(NULL,szFilename,MAX_PATH);
TCHAR szCmdLine[MAX_PATH];
sprintf_s(szCmdLine,"\"%s\" \"child\"",szFilename);
STARTUPINFO si;
ZeroMemory(reinterpret_cast<void*>(&si),sizeof(si));
si.cb=sizeof(si);
PROCESS_INFORMATION pi;
BOOL bCreateOK=CreateProcess(
szFilename,
szCmdLine,
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi
);
if(bCreateOK){
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return pi.hProcess;
}
else return INVALID_HANDLE_VALUE;
}
void Parent(){
cout<<"Creating the child process and waited child process to quit."<<endl;
HANDLE hchild=StartClone();
if(hchild!=INVALID_HANDLE_VALUE)
{
WaitForSingleObject(hchild,INFINITE);
cout<<"The child process had quited."<<endl;
}
else
cout<<"Create child process failed."<<endl;
}
void Child(){
cout<<"Child id quiting"<<endl;
Sleep();
}
int main(int argc,char * argv[]){
if(argc>&&strcmp(argv[],"child")==){
Child();
}
else{
Parent();
}
return ;
}