/*
* 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.
*/
void eval(char *cmdline)
{
char *argv[MAXARGS];
char buf[MAXLINE];
int bg;
pid_t pid;
sigset_t mask_child,mask_all,prev_all;
sigfillset(&mask_all);
sigemptyset(&mask_child);
sigaddset(&mask_child,SIGCHLD);
strcpy(buf,cmdline);
bg=parseline(buf,argv);
if(argv[0]==NULL)
return;
if(!builtin_cmd(argv)){
sigprocmask(SIG_BLOCK, &mask_child, &prev_all);
if((pid=fork())<0){
sigprocmask(SIG_SETMASK, &prev_all, NULL);
fprintf(stderr,"fork error: %s\n",strerror(errno));
exit(0);
}
else if(pid==0){
//child
setpgid(0,0);//last hint,很重要!!!
sigprocmask(SIG_SETMASK, &prev_all, NULL);
if(execve(argv[0],argv,environ)<0){
printf("%s: Command not found.\n",argv[0]);
exit(0);
}
}
//parent
sigprocmask(SIG_BLOCK, &mask_all, NULL);
addjob(jobs,pid,bg?BG:FG,cmdline);
sigprocmask(SIG_SETMASK, &prev_all, NULL);
【深入理解计算机系统csapp lab】shell lab
最新推荐文章于 2023-12-07 00:00:00 发布