سؤال

I have a parent process and I'd like to spawn the child process for a certain duration ( say N milli seconds). I'm doing this in c++ (and windows).

Can I provide some parameter to CreateProcess that would kill it after a while and return control to the parent application ? If not, is there any other way to do this ?

هل كانت مفيدة؟

المحلول

Can I provide some parameter to CreateProcess...?

Yes, you may pass the desired duration (N) with the parameter lpCommandLine to the process to be started.

The child process may parse lpCommandLine and set up a timer with the desired duration. This timer could for example be a Waitable Timer or simply a thread with

Sleep(duration);               // waits the duration
ExitProcess(GetLastError());   // exits the "remote" process

The thread (a thread inside the the child process) terminates the entire child process after duration. The WaitableTimer idea would need a frequent call to a wait function. See Using Waitable Timer Objects for details.

But: The parent process will remain "in control" all the time. However: You may additionally enter a wait state in the parent process, using a wait function (e.g. WaitForSingleObject, waiting on the child processes handle to actually hibernate the parent process until the child process terminates. On top you may evaluate the return value of the child process by a call to the GetExitCodeProcess function.

The described schemes ensures that the desired duration is best performed, however, you may also control the duration from the parent process by means of a named event. See Using Event Objects for details. In such an approach, the parent process may set an event when the duration "is consumed". The child process waits for that event and terminates when the event was set. This approach may be a little less accurate since the parent doesn't quite know when the childs duration started.

Example with waitable timer:

Parent process:

...
#define CHILD_DURATION 2000  // 2000 ms    

HANDLE hProcess;
char ChildName[MAX_PATH];
char CommandLine[MAX_PATH];

sprintf_s(ChildName,MAX_PATH,"MyChild.exe");
sprintf_s(CommandLine,MAX_PATH,"%d",CHILD_DURATION);

// start the child 
hProcess = CreateProcess(ChildProcessName,CommandLine,....
if (0 == hProcess)
{
  // error with process creation
  printf("CreateProcessfailed (%d)\n", GetLastError());
  return GetLastError();
}

// and wait for it to finish
if (WAIT_OBJECT_0 == WaitForSingleObject(hProcess,INFINITE) 
{
  // child finished
} else
{
  // error with wait
  printf("WaitForSingleObject failed (%d)\n", GetLastError());
  return GetLastError();
}  
...

Child process:

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

    HANDLE hWaitableTimer CreateWaitableTimer(NULL,TRUE,NULL);
    if (NULL == hWaitableTimer)
    {
        printf("CreateWaitableTimer failed (%d)\n", GetLastError());
        return GetLastError();
    }

    DWORD dwDuration = atoi(argv[1]);
    LARGE_INTEGER liDueTime = -10000 * dwDuration; 
    // waitable timer due time is in 100 nano second units
    // negative values indicate relative time
    if (!SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, 0))
    {
        printf("SetWaitableTimer failed (%d)\n", GetLastError());
        return GetLastError();
    }

    DWORD dwKeepGoing = TRUE;
    // enter the "to do" loop while waiting for the timer...
    While (dwKeepGoing) 
    {
        switch (WaitForSingleObject(hTimer,0) 
        {
            case WAIT_OBJECT_0:
                // duration over, 
                // optionally finalize "to do" stuff here 
                // and end child
                dwKeepGoing = FALSE;

            case WAIT_TIMEOUT:
                // do stuff here
                break;

            case WAIT_FAILED:
                // the wait function has failed                     
                printf("WaitForSingleObject failed (%d)\n", GetLastError());
                return GetLastError();
        }
     }
     return 0;
}

نصائح أخرى

Noone will kill the process for you, unless you either do it on your own (e.g. from parent process), or the process exits itself.

Note that there is also no such thing as "and return control to the parent application". Once child process is started, both parent and child processes have their own "control" and run simultaneously, consuming CPU time given to the processes by the operating system.

All in all, you decide who will be terminating the process. If you do it from parent process, then once started you keep child process handle and use (not a good idea though) TerminateProcess API toward the child process.

Otherwise, and this is the better one, the child process itself keep looking at runtime and just exits when it's time to do so:

main() {
  time_to_exit = now() + N;
  while(now() < time_to_exit) {
    keep_doing_my_stuff();
  }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top