C语言 使用waitpid在后台运行进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14548367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using waitpid to run process in background?
提问by user1411893
I am trying to mimic the bash feature of running process in the background if "&" is found at the end of the command. I have the following function...and I don't think it's doing what I want it to do
如果在命令末尾找到“&”,我试图模仿在后台运行进程的 bash 功能。我有以下功能……我不认为它在做我想做的事
int execute(char* args[],int background,int *cstatus){
pid_t child;
pid_t ch; /*Pid of child returned by wait*/
if ((child = fork()) == 0){ /*Child Process*/
execvp(args[0],args);
fprintf(stderr, "RSI: %s: command not found\n",args[0]); /*If execvp failes*/
exit(1);
}else{ /*Parent process*/
if (child== (pid_t)(-1)) {
fprintf(stderr,"Fork failed\n"); exit(1);
}else{
if (background==0){ /*If not running in background..wait for process to finish*/
ch = wait(cstatus);
}else{
printf("%ld Started\n",(long)getpid());
/* printf("Parent: Child %ld exited with status = %ld\n", (long) ch, (long)cstatus);
*/ }}
}
return 0;
}
int wait_and_poll(int *cstatus){
pid_t status;
status = waitpid(-1,cstatus,WNOHANG);
if (status>0){
fprintf(stdout,"%ld Terminated.\n",(long) status);
}
return 0;
}
If i just run "ls -l " it works as expected..but if I want to run ls in the background..and have the program keep accepting new commands I call the function with background flag set as 1 and I want it to run the process in the background, tell me that it has created the process..and then prompt to accept next command.
如果我只运行“ls -l”它会按预期工作..但是如果我想在后台运行 ls..并且让程序继续接受新命令我调用该函数并将背景标志设置为 1 并且我希望它在后台运行进程,告诉我它已经创建了进程..然后提示接受下一个命令。
采纳答案by n. 'pronouns' m.
I don't think waitpid(-1, &cstatus, WNOHANG);does what you think it does. You need to check its return value. If it is > 0, that's the PID of the child process that has exited. If it's 0or -1, no child process has changed state.
我不认为waitpid(-1, &cstatus, WNOHANG);你认为它会做什么。您需要检查其返回值。如果是> 0,那就是退出的子进程的PID。如果是0或-1,则没有子进程改变状态。
You can just call waitpid(-1, &cstatus, WNOHANG);before and/or after each command you run. Call it in a loop to catch more than one child exit.
您可以waitpid(-1, &cstatus, WNOHANG);在运行的每个命令之前和/或之后调用。在循环中调用它以捕获多个子出口。
You can also handle SIGCHILD. Your process will receive this signal immediately after a child exit, which is good if you want to report child process termination immediately, without waiting for user's input.
你也可以处理SIGCHILD。您的进程将在子进程退出后立即收到此信号,如果您想立即报告子进程终止而不等待用户输入,这很好。
回答by Aakash Anuj
That is simple. Consider you have a process P with id pid.
那很简单。假设您有一个进程 P,其 ID 为 pid。
If you want it to run in the background(which can be recognized by & at the end of your input string to the shell/program ), you should do this
如果您希望它在后台运行(可以在 shell/program 的输入字符串末尾通过 & 识别),您应该这样做
//some code
id=fork();
if(id==0)
{
//child does work here
}
else
{
//Parent does work here
if(!strcmp(last string,"&")==0)waitpid(id,&status,0);
}
Thus the parent does not wait if you request for background execution, else it waits.
因此,如果您请求后台执行,父级不会等待,否则它会等待。

