0

我正在尝试在 C 中实现一个简单的 unix shell。但是,我的后台进程功能运行不佳。

当我运行 shell 并给出后台命令时,它会给出如下输出:

> sleep 4 &
> [10612]retval: 0 
> [10616]retval: 0 
> [10618]retval: 0 
> [10619]retval: 0 
> [10622]retval: 0 
> [10623]retval: 0

问题是它给出了一段睡眠时间的输出,而它应该只给出第一个 retval。

我的信号处理程序是

void handler(int s) {
    int pid;
    int status;
    pid = waitpid(-1, NULL, WNOHANG);
    printf("[%d]retval: %d \n", pid, WEXITSTATUS(status));
    fflush(stdout);
}

我的签名是

struct sigaction act;
memset(&act, 0, sizeof(act));

sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = handler;

那么,我的代码有什么问题?

提前致谢...

编辑:另外,我有一个 sigaction()

if (isBackground) {
        sigaction(SIGCHLD, &act, 0);
    }
4

0 回答 0