#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main() { pid_t pid; pid = fork(); if (pid < 0) { perror("fork fail\n"); exit(-1); } else if ( pid == 0 ) // 子进程1 { printf("b\n"); sleep(5); exit(0); } else { printf("a\n"); // 父进程 waitpid(pid,NULL,0); printf("my son has exit!\n"); exit(0); } }的waitpid(pid,NULL,0)有什么用
时间: 2024-01-10 11:01:55 浏览: 117
waitpid(pid,NULL,0)是父进程等待子进程结束的一种方式。其中,pid是子进程的进程ID,NULL表示不需要获取子进程的结束状态,0表示等待任何子进程结束。
在该代码中,父进程通过waitpid函数等待子进程结束,以确保子进程在父进程之前结束。如果不等待子进程结束,父进程可能会在子进程之前退出,导致子进程变成孤儿进程,从而由init进程接管,造成资源浪费和系统性能降低。
因此,使用waitpid函数可以保证父子进程的正确协同执行。
相关问题
完善这段代码:#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> int main(void){ pid_t a; a = fork(); if (a < 0) { perror("fork failed"); exit(1); } if (a == 0) { execlp ("ps" ,"ps",NULL); execlp ("ls" ,"ls","-al","/etc/passwd ",NULL); return 1; } else { wait(NULL); printf("father exit\n");} return 0; }
这段代码可以通过以下方式完善:
1. 在子进程中使用 `exit(0)` 来结束进程,代替 `return 1`。
2. 在父进程中使用 `WIFEXITED` 和 `WEXITSTATUS` 宏来判断子进程是否正常结束,并获取子进程的返回值。
修改后的代码如下:
```c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void){
pid_t a;
a = fork();
if (a < 0) {
perror("fork failed");
exit(1);
}
if (a == 0) {
execlp("ps", "ps", NULL);
execlp("ls", "ls", "-al", "/etc/passwd", NULL);
exit(0);
} else {
int status;
wait(&status);
if (WIFEXITED(status)) {
printf("child process exited with status: %d\n", WEXITSTATUS(status));
}
printf("father exit\n");
}
return 0;
}
```
请注意,`execlp` 函数在执行时,如果成功,则不会返回原进程,因此需要使用 `exit` 函数来结束子进程。
include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> int main(void){ pid_t a; a = fork(); if (a < 0) { perror("fork failed"); exit(1); } if (a == 0) { execlp ("ps" ,"ps",NULL); execlp ("ls" ,"ls","-al","/etc/passwd ",NULL); return 1; } else { wait(NULL); printf("father exit\n");} return 0; }改正以上代码
根据你提供的代码,我们可以看出,子进程中有两个 `execlp` 函数调用,这意味着子进程将执行两个程序,而不是一个程序。此外,在第二个 `execlp` 函数调用中,你在 `/etc/passwd` 后面添加了一个空格,这可能会导致程序无法正常运行。
下面是改正后的代码:
```c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void) {
pid_t pid;
pid = fork();
if (pid < 0) {
perror("fork failed");
exit(1);
} else if (pid == 0) {
execlp("ps", "ps", NULL);
perror("execlp failed");
exit(1);
} else {
wait(NULL);
printf("child process finished\n");
}
return 0;
}
```
在上面的代码中,我们只调用了一个 `execlp` 函数,并且没有在 `/etc/passwd` 后面添加空格。如果 `execlp` 调用失败,我们使用 `perror` 函数输出错误信息。此外,我们还将 `printf` 语句修改为 "child process finished",以更清晰地表达程序的意图。
阅读全文
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/x-bzip2](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)