【深入理解计算机系统csapp lab】shell lab

/*
 * 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);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值