繁体   English   中英

Shell作业控制

[英]Shell job control

对于我的学校项目,我正在实施一个shell,我需要有关工作控制的帮助。 如果我们输入一个命令,比如说cat & ,那么因为&它应该在后台运行,但它不起作用。 我有这个代码:

{
  int pid;  
  int status;  
  pid = fork();  
  if (pid == 0) {  
    fprintf(stderr, "Child Job pid = %d\n", getpid());  
    execvp(arg1, arg2);  
  } 
  pid=getpid();  
  fprintf(stderr, "Child Job pid is = %d\n", getpid());      
  waitpid(pid, &status, 0);  
}

您应该为SIGCHLD信号设置信号处理程序,而不是直接等待。 只要子进程停止或终止,就会发送SIGCHLD。 查看流程完成的GNU描述。

本文的结尾有一个示例处理程序(我或多或少地复制并粘贴在下面)。 尝试从中建模代码。

 void sigchld_handler (int signum) {
     int pid, status, serrno;
     serrno = errno;
     while (1) {
         pid = waitpid(WAIT_ANY, &status, WNOHANG);
         if (pid < 0) {
             perror("waitpid");
             break;
         }
         if (pid == 0)
           break;
         /* customize here.
            notice_termination is in this case some function you would provide
            that would report back to your shell.
         */             
         notice_termination (pid, status);
     }
     errno = serrno;
 }

关于这个主题的另一个很好的信息来源是UNIX环境中的高级编程 ,第8章和第10章。

父进程正在调用子进程上的waitpid ,这将阻塞子进程改变状态(即终止)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM