Find the answer to your Linux question:
Results 1 to 3 of 3
I'm using fork() and execv() to spawn a child process from an application. Spawning a child process allows me to easily seperate lengthy processor intesive builds while leaving the main ...
  1. #1
    Just Joined! mitchpotter's Avatar
    Join Date
    Jan 2008
    Location
    Orlando
    Posts
    19

    [SOLVED] C++: Can't kill child process - fork(), execv(). kill()

    I'm using fork() and execv() to spawn a child process from an application. Spawning a child process allows me to easily seperate lengthy processor intesive builds while leaving the main application free to check results and send status data back to a web server. Threading was an option but became too complicated and I needed something quick and easy.

    The problem is that the child process will not terminate until the main app is closed, which is very bad for me since I want the app to continue spawning the same child process. I can't even kill the process from the command line or from KDE's system tools Process Table. It only exits with the main app. The child application has finished processing correctly and returned from the main function before I try to terminate it. The main app does call the kill() function below but without effect.

    Please take a look at the code below. Since I can't paste all the code, here is what is relevant:

    Spawning:
    Code:
    // fork spawns a duplicate of the main app and continues processing here
    pID = fork();
    if( pID == 0 )
    {
    	// put args from a vector into a char ** array
    	char **args = new char *[vArgs.size() + 1];
    	for( size_t i = 0; i < vArgs.size(); i++ )
    	{
    		args[i] = new char[1000];
    		strncpy( args[i], vArgs[i].c_str(), vArgs[i].length() + 1 );
    	}
    	args[vArgs.size()] = NULL;
    
    	// execv replaces current process with szProgram (specified app)
    	execv( szProgram, args );
    
    	// This cleanup only happens if execv fails (I think)
    	for( size_t i = 0; i < vArgs.size(); i++ )
    		delete args[i];
    	delete args;
    
    	exit( -1 );
    }
    else if( pID < 0 )
    {
    	printf( "Failed to create process: %s\n", szProgram );
    }
    else
    	// Save the p_id so I can kill the process later (if needed)
    	//    Continue main application processing
    KillProcess:
    Code:
    kill( pID /*FromAbove*/, SIGTERM );

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    I think the parent program needs to call wait or waitpid to collect the child's exit code when it terminates, otherwise it turns into a zombie.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  3. #3
    Just Joined! mitchpotter's Avatar
    Join Date
    Jan 2008
    Location
    Orlando
    Posts
    19
    That did it. I used waitpid() and it exited correctly. So I'm guessing a child process will not terminate until the parent calls one of the wait functions. Seems strange but what about Linux isn't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...