简体   繁体   English

在我自己的shell中创建后台进程

[英]creating background processes in my own shell

I am writing my own unix shell as a part of my assignment and I couldn't handle creating background processes. 我正在编写自己的unix shell作为作业的一部分,我无法处理创建后台进程的问题。 I wrote a signal-handling function and implemented the necessary(from my point of view-but obviously not good enough) parts to my code as follows : 我编写了一个信号处理函数,并将必要的(从我的角度来看,但显然不够好)部分实现了我的代码,如下所示:

void handler(int sig)
{
    int pid;
    int status;
    pid = wait(NULL);
    printf("[%d]retval: %d \n", pid, WEXITSTATUS(status));
    fflush(stdout);
}

int main() { 
.....
....
struct sigaction sigchild; 
memset (&sigchild, 0, sizeof(sigchild)); 
sigchild.sa_handler = handler;
sigchild.sa_flags = SA_SIGINFO | SA_NOCLDWAIT;
...
...
if(isBackground)  //background process
{
    sigaction(SIGCHLD, &sigchild, 0);
}

When I enter "sleep 5 &" for example and then enter "ps" to see the processes there is no sleep. 例如,当我输入“ sleep 5&”,然后输入“ ps”以查看进程时,就没有睡眠。 What may be the problem? 可能是什么问题? Thanks in advance. 提前致谢。

The child is the background process. 子进程是后台进程。 The parent is the foreground process. 父级是前台进程。 SIGCHLD is a signal sent to a parent when the child terminates. SIGCHLD是当孩子终止时发送给父母的信号。 Change the if (isbackground to if(!background) if (isbackground更改为if(!background)

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

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