linux编程实现:创建一个子进程,使用wait函数让父进程等待子进程的结束,并打印出父进程的等待信息“Parent is waiting!”,子进程中使用exec函数启动“ps -ef”命令的运行,然后结束进程。
时间: 2024-02-28 13:52:00 浏览: 14
下面是实现该功能的 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
int status;
pid_t pid = fork();
if (pid < 0) {
printf("Error: Failed to fork a child process!\n");
exit(1);
}
else if (pid == 0) {
printf("Child process starts!\n");
execlp("ps", "ps", "-ef", NULL);
exit(0);
}
else {
printf("Parent process is waiting!\n");
wait(&status);
printf("Child process ends!\n");
exit(0);
}
return 0;
}
```
该程序使用 fork() 函数创建了一个子进程,然后在子进程中使用 execlp() 函数启动 ps -ef 命令的运行,并在结束后退出进程。在父进程中,使用 wait() 函数等待子进程的结束,并在子进程结束后输出相应的信息。
相关问题
linux编程实现:创建一个子进程,使用wait函数让父进程等待子进程的结束,并打印出父进程的等待信息“Parent is waiting!”,子进程中使用exec函数启动“ps -ef”命令的运行,然后结束进程
好的,以下是实现该功能的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid;
// 创建子进程
pid = fork();
if (pid < 0) {
// fork失败
perror("fork failed");
exit(1);
}
else if (pid == 0) {
// 子进程中调用exec函数启动"ps -ef"命令的运行
execlp("ps", "ps", "-ef", NULL);
exit(0);
}
else {
// 父进程中使用wait函数等待子进程结束
wait(NULL);
printf("Parent is waiting!\n");
exit(0);
}
return 0;
}
```
运行该程序,可以看到父进程等待子进程结束后打印出了"Parent is waiting!"的信息,并且子进程成功执行了"ps -ef"命令。
我想要一段完整的linux下可运行的包含exec、wait、fork、exit函数的进程控制编程代码
下面是一个包含exec、wait、fork、exit函数的进程控制编程代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
pid_t child_pid;
int status;
child_pid = fork(); // 创建子进程
if (child_pid == -1) // fork 失败
{
perror("fork");
exit(EXIT_FAILURE);
}
if (child_pid == 0) // 子进程
{
printf("I am the child process, PID=%d\n", getpid());
char *args[] = {"ls", "-l", NULL}; // exec 的参数列表
execvp(args[0], args); // 执行 ls 命令
// 如果 execvp 返回,则表示执行失败
perror("execvp");
exit(EXIT_FAILURE);
}
else // 父进程
{
printf("I am the parent process, PID=%d\n", getpid());
waitpid(child_pid, &status, 0); // 等待子进程结束
if (WIFEXITED(status)) // 如果子进程正常结束
{
printf("Child process exited with status %d.\n", WEXITSTATUS(status));
}
else if (WIFSIGNALED(status)) // 如果子进程被信号终止
{
printf("Child process terminated by signal %d.\n", WTERMSIG(status));
}
}
return 0;
}
```
在这个示例中,父进程调用 fork() 函数创建一个子进程,然后在子进程中调用 execvp() 函数执行 ls 命令。父进程等待子进程结束,并根据子进程的退出状态打印相应的信息。注意,如果 execvp() 函数执行成功,则子进程将被替换为新的程序,这意味着子进程不会继续执行原有的代码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)