문제

I'm implementing a Terminal for a Home Work.
I almost finished, I just need to implement a bg ( Background ) and a fg ( Foreground ) commands.
my code looks like this:

void run(){

    string command[] = parseMyInput( getInput() );  
    int fork_result = fork();
    if( -1 == fork_result )
        //handle error
    else if( 0 == fork_result ){  // child

         setpgrp();  // I don't want the children to get the signals

         if( -1 == execvp( command[0], makeArgs(command) ) )
             //handle error
    }
    else {  // parent

        if( command[ length - 1 ] != "&" ){

            int status;
            waitpid( fork_result, &status, 0 );

            //continue after child is finished
           //( need to be here also after a SIGTSTP is raised by ctrl+z )
        }
    }

If it is a foreground process ( if there wasn't a '&' sign at the end ) then I need to be able to stop the foreground process ( the child ) with ctrl+z ( SIGTSTP ) and then to return the control to the father ( my terminal ) from the point it stopped ( waitpid ).

the problem is that after a ctrl+z is pressed, ( and after the parent get the control in the signal handle method and stop the child using kill( child_pid, SIGTSTP ) ) the parent don't continue from where it stopped ( the waitpid ). I don't know where it continue after the signal handling method is finished.

If I call run() at the signal handling method it will work, but I don't want the recursion. I'm guessing I'll get a StackOverFlow very soon...

here is the code of the Signal Handling Method:

void sigtstp_handel( int signal ){  

    if( is_foreground_process_alive() )
        kill( foreground_process_pid, SIGTSTP );

    // run();
}  

EDIT: I don't know if it will matter, but I'm using a Linux Ubuntu 12.10. nevertheless, for the Home Work, I'll need it to work on other systems.

Thanks!

도움이 되었습니까?

해결책

Reading the official POSIX reference for waitpid:

WUNTRACED

The status of any child processes specified by pid that are stopped,
and whose status has not yet been reported since they stopped, shall
also be reported to the requesting process.

So if you add the WUNTRACED flag the waitpid call should return when the process it waits for is stopped.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top