void CTcpClient::run() { #ifdef LINUX San2::Utils::san_ignore_sigpipe(); #endif errcode = runProc(); }
void ProcessMonitor::waitForProcess(){ //this function waits for process closing and restart process if needed do{ WaitForSingleObject(pHandle, INFINITE);//wait for process closing if (getLastAction() == LastAction::START || (getLastAction() == LastAction::RESTART && getStatus() != ProcessStatus::RESTARTING)){ //if (getLastAction() != LastAction::STOP){ //must restart process only if it was closed by itself logMessage(L"Process closed by itself", true); setProcessStatus(ProcessStatus::STOPPED);//update status crashCallbackMutex.lock(); if(onProcCrash)std::thread(onProcCrash).detach();//execute callback crashCallbackMutex.unlock(); if (!runProc()){ break;//if we can't start process back we leave } else { setProcessStatus(ProcessStatus::IS_WORKING);//if we can, update status } } else{ break;//we closed that process } } while (true); }
bool ProcessMonitor::start(){ //interface for starting a process std::unique_lock <std::mutex> uniqueOperationLock(operationMutex); while (!previousActionFinished) actionFinishedCondition.wait(uniqueOperationLock);//wait until another action(stop, restart) finish previousActionFinished = false; if (getStatus() == ProcessStatus::STOPPED){ //start a new process only if current is stopped setLastAction(LastAction::START); if (runProc()){ //if process was started setProcessStatus(ProcessStatus::IS_WORKING);//update status if (waitingThread.joinable()) waitingThread.join(); waitingThread = std::thread(&ProcessMonitor::waitForProcess, this);//wait for process closing in another thread previousActionFinished = true; actionFinishedCondition.notify_one();//next action can start working return true;//process was successfully started } } previousActionFinished = true; actionFinishedCondition.notify_one(); return false;//Process was already running or can't start a new process }
bool ProcessMonitor::restartProc(){ //restart already running process if (stopProc()){ return runProc();//if successfully stopped then result depends on start } return false;//process was not stopped; }
void PipeServer::run() { errcode = runProc(); }
int main(int argc, char *argv[]) { int status; int childPid; int grandChildPid; int parentChildPipe[2]; int childGrandChildPipe[2]; if(argc < 3 || argc > 7) { perror("Usage: './TwoProcs <command> [-arg..] <command> [-arg..] <command> [-arg]'"); return 1; } status = pipe(parentChildPipe); if(status == -1) { perror("Failed to create the pipe"); return 1; } childPid = fork(); if (childPid == -1) { perror("Creating child process failed"); return 1; } if (childPid == 0) { int grandChildPid = fork(); if(grandChildPid == 0) { if(argc == 7) { close(parentChildPipe[0]); close(parentChildPipe[1]); dup2(childGrandChildPipe[0], STDIN_FILENO); close(childGrandChildPipe[1]); printf("argv[5]: %s\nargv[6]: %s\n", argv[5], argv[6]); runProc(argv[5], argv[6]); } } else { if(dup2(parentChildPipe[0], STDIN_FILENO) < 0) { perror("Failed to redirect standard input"); return 1; } close(parentChildPipe[1]); /* if (argc == 7) { dup2(childGrandChildPipe[1], STDOUT_FILENO); } */ if(argc == 3) { runProc(argv[2], argv[2]); } else if(argc == 5) { runProc(argv[3], argv[4]); } else if(argc == 7) { dup2(childGrandChildPipe[1], STDOUT_FILENO); runProc(argv[3], argv[4]); } } } else { if(dup2(parentChildPipe[1], STDOUT_FILENO) < 0) { perror("Failed to redirect standard output"); return 1; } if(argc == 3) { runProc(argv[1], argv[1]); } if(argc == 5 || argc == 7) { runProc(argv[1], argv[2]); } } return 0; }