C++, linux, fork, execvp, waitpid and SIGTSP

元气小坏坏 提交于 2019-12-22 14:00:54

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/13485462/c-linux-fork-execvp-waitpid-and-sigtsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!