ast_comparator/test/student_data/anonymized/reference_17_tsh.c
/*
 * Runs a child program using fork and cmd line arguments
 * takes care about waiting foreground job and
 * runs job in background if bg is set
 */
Runs a child program using fork and cmd line arguments
takes care about waiting foreground job and
runs job in background if bg is set
Runs a child program using fork and cmd line arguments takes care about waiting foreground job and runs job in background if bg is set
void run_child(char* cmdline, struct cmdline_tokens *tok, int bg)
{
    pid_t pid = 0;  
    int fd = 0;     
    sigset_t mask;
    sigemptyset(&mask);
    sigaddset(&mask, SIGCHLD);
    sigaddset(&mask, SIGINT);
    sigaddset(&mask, SIGTSTP);
    sigprocmask(SIG_BLOCK, &mask, NULL);
    if ((pid = Fork()) == 0) {
        setpgid(0,0);
        sigprocmask(SIG_UNBLOCK, &mask, NULL);
        if (tok->infile) {
            fd = open(tok->infile, O_RDONLY,0);
            dup2(fd, STDIN_FILENO);
            close(fd);
        }
        if (tok->outfile) {
            fd = creat(tok->outfile, 0644);
            dup2(fd, STDOUT_FILENO);
            close(fd);
        }
        if (execve(tok->argv[0], tok->argv, environ ) < 0) {
            printf("%s: Command not found. \n", tok->argv[0]);
            exit(1);
        }
    } else {
        addjob(job_list, pid, bg ? BG : FG, cmdline);
        sigprocmask(SIG_UNBLOCK, &mask, NULL);
        sigset_t mask2;
        sigemptyset(&mask2);
        if (!bg) Suspend(getjobpid(job_list, pid));
        else     printf("[%d] (%d) %s\n", maxjid(job_list), pid, cmdline);
        if (verbose) printf("exit_run_child %s\n",cmdline);
    }
}
void run_child ( char * cmdline , struct cmdline_tokens * tok , int bg ) { pid_t pid = 0 ; int fd = 0 ; sigset_t mask ; sigemptyset ( & mask ) ; sigaddset ( & mask , SIGCHLD ) ; sigaddset ( & mask , SIGINT ) ; sigaddset ( & mask , SIGTSTP ) ; sigprocmask ( SIG_BLOCK , & mask , NULL ) ; if ( ( pid = Fork ( ) ) == 0 ) { setpgid ( 0 , 0 ) ; sigprocmask ( SIG_UNBLOCK , & mask , NULL ) ; if ( tok -> infile ) { fd = open ( tok -> infile , O_RDONLY , 0 ) ; dup2 ( fd , STDIN_FILENO ) ; close ( fd ) ; } if ( tok -> outfile ) { fd = creat ( tok -> outfile , 0644 ) ; dup2 ( fd , STDOUT_FILENO ) ; close ( fd ) ; } if ( execve ( tok -> argv [ 0 ] , tok -> argv , environ ) < 0 ) { printf ( " \n " , tok -> argv [ 0 ] ) ; exit ( 1 ) ; } } else { addjob ( job_list , pid , bg ? BG : FG , cmdline ) ; sigprocmask ( SIG_UNBLOCK , & mask , NULL ) ; sigset_t mask2 ; sigemptyset ( & mask2 ) ; if ( ! bg ) Suspend ( getjobpid ( job_list , pid ) ) ; else printf ( " \n " , maxjid ( job_list ) , pid , cmdline ) ; if ( verbose ) printf ( " \n " , cmdline ) ; } }
Runs a child program using fork and cmd line arguments
 takes care about waiting foreground job and
 runs job in background if bg is set
Runs a child program using fork and cmd line arguments takes care about waiting foreground job and runs job in background if bg is set
/* child pid */ /* file descriptor */ /* avoid race condition with child */ /* fork the process */ /* child process */ /* do redirections */ /* exec new program */ /* parent process */