ast_comparator/test/student_data/handout/shlab-handout/cases/reference_32_tsh.c
/* 
* 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;
strcpy(buf, cmdline);
bg = parseline(buf, argv);
if (argv[0] == NULL) {
return;
}
if (bg != -1 && !builtin_cmd(argv)) {
mask_signal(SIG_BLOCK, SIGCHLD);
if ((pid = fork()) == 0) {
mask_signal(SIG_UNBLOCK, SIGCHLD);
if (setpgid(0, 0) < 0) { 
unix_error("eval: setpgid failed");
}
if (execve(argv[0], argv, environ) < 0) {
printf("%s: Command not found.\n", argv[0]);
exit(0);
}
} else {
if (!bg) {
addjob(jobs, pid, FG, cmdline);
} else {
addjob(jobs, pid, BG, cmdline);
}
mask_signal(SIG_UNBLOCK, SIGCHLD); 
if (!bg) {
waitfg(pid);
} else {
printf("[%d] (%d) %s", pid2jid(pid), pid, cmdline);
}
}
}
return;
}
void eval ( char * cmdline ) { char * argv [ MAXARGS ] ; char buf [ MAXLINE ] ; int bg ; pid_t pid ; strcpy ( buf , cmdline ) ; bg = parseline ( buf , argv ) ; if ( argv [ 0 ] == NULL ) { return ; } if ( bg != -1 && ! builtin_cmd ( argv ) ) { mask_signal ( SIG_BLOCK , SIGCHLD ) ; if ( ( pid = fork ( ) ) == 0 ) { mask_signal ( SIG_UNBLOCK , SIGCHLD ) ; if ( setpgid ( 0 , 0 ) < 0 ) { unix_error ( " " ) ; } if ( execve ( argv [ 0 ] , argv , environ ) < 0 ) { printf ( " \n " , argv [ 0 ] ) ; exit ( 0 ) ; } } else { if ( ! bg ) { addjob ( jobs , pid , FG , cmdline ) ; } else { addjob ( jobs , pid , BG , cmdline ) ; } mask_signal ( SIG_UNBLOCK , SIGCHLD ) ; if ( ! bg ) { waitfg ( pid ) ; } else { printf ( " " , pid2jid ( pid ) , pid , cmdline ) ; } } } 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.
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 .
//处理addjob与子进程结束的竞争条件 //child /* put the child in a new process group */ //addjob //fg //bg /* 添加完成，解除阻塞 */ //wait foreground to stop