启动新的可执行文件后,C ++程序没有退出

时间:2016-04-04 15:25:49

标签: c++ system c++98

考虑两个C ++项目:

项目1:

// projectOne.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
    Sleep(5000);
    system("projectTwo.exe");
    return 0;
}

项目2:

// projectTwo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
    Sleep(5000);
    system("projectOne.exe");
    return 0;
}

我寻求的行为是:projectOne starts =>开始projectTwo => projectOne结束=> projectTwo将启动projectOne => projectTwo结束=> projectOneprojectTwo开始。

然而,节目并未结束。例如,当projectOne启动projectTwo时,projectOnereturn 0;内运行时不会projectOne结束。所以几分钟后,会有多个版本的程序同时运行。我以为它与system命令有关。也许它等到项目完成后才会进入下一行代码,这导致盘旋,但我不确定。我怎样才能解决这个问题?我需要在使用system命令调用其中一个程序后结束程序。我希望这个问题很清楚。

2 个答案:

答案 0 :(得分:1)

system blocks the running thread until system returns and system will not return until the executed process has terminated.

There are many ways to solve this problem. The simplest and most likely to be portable is to use a std::thread to run system in a thread that runs concurrent to the main processing thread.

std::thread procthread([processToRun] {system(processToRun.c_str());});
procthread.detach(); 

Short, sweet, and as portable as anything calling system can be. The first line creates a thread and executes a lambda function that runs system on the provided process name. The second line disconnects the thread from the std::thread object and allows the thread to run free. Otherwise if procthread goes out of scope the thread will be terminated and bad things will very likely happen.

If you can't do this because your development system does not support C++11 or better, you can use operating system-specific threading, but if you have to use system-specific thread creation calls, you might as well use system-specific process creation calls to directly create the new process.

In POSIX systems, posix_spawn will likely be the go-to function. I don't have a machine at my disposal to test this on at the moment, so I'll just link to Starting a process using posix_spawn.

Under Windows, use CreateProcess or your variant of choice. The following code is based on Microsoft's Creating Processes documentation page and modified to be a little less Microsoft specific and not wait for the spawned process to complete before continuing execution.

char processToRun[] = "process to run"; //NOTE: Not a std::string!
STARTUPINFO si;
PROCESS_INFORMATION pi;

memset(&si, 0, sizeof(si));
si.cb = sizeof(si);
memset(&pi, 0, sizeof(pi));

// Start the child process.
if (!CreateProcess(NULL, // No module name (use command line)
                   processToRun, // Command line DANGER! won't accept const char* 
                                 // cannot use std::string::c_str
                   NULL, // Process handle not inheritable
                   NULL, // Thread handle not inheritable
                   FALSE, // Set handle inheritance to FALSE
                   0, // No creation flags
                   NULL, // Use parent's environment block
                   NULL, // Use parent's starting directory
                   &si, // Pointer to STARTUPINFO structure
                   &pi)) // Pointer to PROCESS_INFORMATION structure
{
    std::cerr << "CreateProcess failed ("<<GetLastError()<<").\n";
    return false;
}

// do stuff
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return true;

答案 1 :(得分:-1)

你的方法无限循环,结束!!

  

你正在产生多个projectOne和projectTwo的实例,而这些实例又创造了更多......这是递归的-_-

修改

  

系统等待

<强>解

int execl(char * pathname, char * arg0, arg1, ..., argn, NULL);