Fork(), wait(), and SIGTSTP

Discussion in 'OT Technology' started by acidix, Feb 19, 2007.

  1. acidix

    acidix Ua mau, ke ea o ka `aina, i ka pono

    Feb 9, 2007
    Likes Received:
    I can smile when its raining.
    hey guys, I was wondering if you could help me with a problem I am having.

    I am writing my own shell. If i type in a string it'll assume that string is a path to a program, and it runs that program. I assign the program a new PID with fork() and keep track of it in a global array job_table, and since its running in the foreground i set a variable fg_job to be the index in the job table.

    This is all so I can kill the process.

    I wrote a signal handler to catch the ctrl-z (SIGTSTP) signal and instead of stopping the shell, it'll stop the foreground job held in job_table[fg_job].

    The problem is this:
    When I implemented the fork(), i had the parent process wait(0) so that it would wait until the program was finished before reprompting the user. That works fine if i use SIGKILL to completely eliminate the program. When i use my handler to SIGTSTP the program, the parent process continues to wait!

    Is there anything i can do to make the parent process stop waiting if the program is exited OR stopped? Here is my code for the fork().

    The bolded text is the part im having trouble with. All of that was to try to mess with the WAITPID function. Previously it was just wait(0);

    pid_t pid;

    int status;
    pid_t retpid;
    pid = fork();
    if (pid > 0){

    //sets the job table index to the pid of the child
    job_table[fg_job] = pid;
    //sets the current job to the index of the new process
    //forces the parent program to wait

    while((retpid = waitpid(pid, &status,0))>0){
    printf("The process was exited\n");

    job_table[fg_job] = -1;
    fg_job = -1;
    else if(WIFSTOPPED(status)){
    printf("The process was stopped\n");
    fg_job = -1;

    job_table[fg_job] = -1;
    fg_job = -1;
    if (pid == 0){
    //runs the program, or outputs an invalid pathname line
    if(execve(cmd, argv, environ)<0) printf("invalid pathname\n");

Share This Page