computer-system/CMU15-213/labs/LAB5_tshlab/tsh.c
void eval(char *cmdline) {
    char *argv[MAXARGS];    /* Argument list execve() */
    char buf[MAXLINE];      /* Holds modified command line */
    int bg;                 /* Should the job run in bg? */
    pid_t pid;              /* Process id */

    sigset_t mask_all;      /* Block all signals vector */
    sigset_t mask_one;      /* Block SIGCHLD vector */
    sigset_t prev_all;      /* Previous blocked vector */
    Sigfillset(&mask_all);

    strcpy(buf, cmdline);
    bg = parseline(buf, argv);
    if(argv[0] == NULL)
        return;             /* Ignore empty lines */

    if(!builtin_cmd(argv)) {
        Sigprocmask(SIG_BLOCK, &mask_one, &prev_all);
        if((pid = fork()) == 0) {   /* Child runs user job */
            Sigprocmask(SIG_SETMASK, &prev_all, NULL);  /* Restore mask for child */
            setpgid(0, 0);
            Execve(argv[0], argv, environ);
        }
        
        /* Parent waits for foreground job to terminate */
        if(!bg) {
            /* Add process to job list */
            Sigprocmask(SIG_BLOCK, &mask_all, NULL);
            addjob(jobs, pid, FG, buf);
            Sigprocmask(SIG_SETMASK, &prev_all, NULL);
            /* Wait for job to finish */
            waitfg(pid);
            // TODO: Put SIG_SETMASK outside
        } 
        else {
            /* Add process to job list */
            Sigprocmask(SIG_BLOCK, &mask_all, NULL);
            addjob(jobs, pid, BG, buf);
            Sigprocmask(SIG_SETMASK, &prev_all, NULL);
            printf("[%d] (%d) %s", pid2jid(pid), pid, buf);
        }
    }    

    return;
}
/* 
 * eval - Evaluate the command line that the user has just typed in
 * 
 * If the user has requested a built-in command (quit, jobs, bg or fg)
 * then execute it immediately. Otherwise, fork a child process and
 * run the job in the context of the child. If the job is running in
 * the foreground, wait for it to terminate and then return.  Note:
 * each child process must have a unique process group ID so that our
 * background children don't receive SIGINT (SIGTSTP) from the kernel
 * when we type ctrl-c (ctrl-z) at the keyboard.  
*/
Evaluate the command line that the user has just typed in

If the user has requested a built-in command (quit, jobs, bg or fg)
then execute it immediately. Otherwise, fork a child process and
run the job in the context of the child. If the job is running in
the foreground, wait for it to terminate and then return.  Note:
each child process must have a unique process group ID so that our
background children don't receive SIGINT (SIGTSTP) from the kernel
when we type ctrl-c (ctrl-z) at the keyboard.
Evaluate the command line that the user has just typed in If the user has requested a built - in command ( quit jobs bg or fg ) then execute it immediately . Otherwise fork a child process and run the job in the context of the child . If the job is running in the foreground wait for it to terminate and then return . Note : each child process must have a unique process group ID so that our background children don ' t receive SIGINT ( SIGTSTP ) from the kernel when we type ctrl - c ( ctrl - z ) at the keyboard .
void eval(char *cmdline) {
    char *argv[MAXARGS];    
    char buf[MAXLINE];      
    int bg;                 
    pid_t pid;              
    sigset_t mask_all;      
    sigset_t mask_one;      
    sigset_t prev_all;      
    Sigfillset(&mask_all);
    strcpy(buf, cmdline);
    bg = parseline(buf, argv);
    if(argv[0] == NULL)
        return;             
    if(!builtin_cmd(argv)) {
        Sigprocmask(SIG_BLOCK, &mask_one, &prev_all);
        if((pid = fork()) == 0) {   
            Sigprocmask(SIG_SETMASK, &prev_all, NULL);  
            setpgid(0, 0);
            Execve(argv[0], argv, environ);
        }
        if(!bg) {
            Sigprocmask(SIG_BLOCK, &mask_all, NULL);
            addjob(jobs, pid, FG, buf);
            Sigprocmask(SIG_SETMASK, &prev_all, NULL);
            waitfg(pid);
        } 
        else {
            Sigprocmask(SIG_BLOCK, &mask_all, NULL);
            addjob(jobs, pid, BG, buf);
            Sigprocmask(SIG_SETMASK, &prev_all, NULL);
            printf("[%d] (%d) %s", pid2jid(pid), pid, buf);
        }
    }    
    return;
}
void eval ( char * cmdline ) { char * argv [ MAXARGS ] ; char buf [ MAXLINE ] ; int bg ; pid_t pid ; sigset_t mask_all ; sigset_t mask_one ; sigset_t prev_all ; Sigfillset ( & mask_all ) ; strcpy ( buf , cmdline ) ; bg = parseline ( buf , argv ) ; if ( argv [ 0 ] == NULL ) return ; if ( ! builtin_cmd ( argv ) ) { Sigprocmask ( SIG_BLOCK , & mask_one , & prev_all ) ; if ( ( pid = fork ( ) ) == 0 ) { Sigprocmask ( SIG_SETMASK , & prev_all , NULL ) ; setpgid ( 0 , 0 ) ; Execve ( argv [ 0 ] , argv , environ ) ; } if ( ! bg ) { Sigprocmask ( SIG_BLOCK , & mask_all , NULL ) ; addjob ( jobs , pid , FG , buf ) ; Sigprocmask ( SIG_SETMASK , & prev_all , NULL ) ; waitfg ( pid ) ; } else { Sigprocmask ( SIG_BLOCK , & mask_all , NULL ) ; addjob ( jobs , pid , BG , buf ) ; Sigprocmask ( SIG_SETMASK , & prev_all , NULL ) ; printf ( " " , pid2jid ( pid ) , pid , buf ) ; } } return ; }
eval - Evaluate the command line that the user has just typed in
eval - Evaluate the command line that the user has just typed in
/* Argument list execve() */ /* Holds modified command line */ /* Should the job run in bg? */ /* Process id */ /* Block all signals vector */ /* Block SIGCHLD vector */ /* Previous blocked vector */ /* Ignore empty lines */ /* Child runs user job */ /* Restore mask for child */ /* Parent waits for foreground job to terminate */ /* Add process to job list */ /* Wait for job to finish */ // TODO: Put SIG_SETMASK outside /* Add process to job list */