编写一C语言个程序,创建一个孤儿进程并用ps命令显示该进程的状态。
时间: 2024-05-12 22:16:41 浏览: 15
好的,以下是C语言程序代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
pid_t pid;
pid = fork();
if (pid == -1) {
printf("Error: Failed to create child process.\n");
exit(EXIT_FAILURE);
} else if (pid == 0) {
printf("Child process is running with PID %d.\n", getpid());
printf("Parent process ID is %d.\n", getppid());
sleep(10);
printf("Child process is exiting.\n");
exit(EXIT_SUCCESS);
} else {
printf("Parent process is running with PID %d.\n", getpid());
printf("Child process ID is %d.\n", pid);
system("ps -o pid,ppid,state -p $pid");
sleep(15);
printf("Parent process is exiting.\n");
exit(EXIT_SUCCESS);
}
return 0;
}
```
该程序创建了一个孤儿进程,并使用ps命令显示该进程的状态。请在终端中编译并运行此程序。当子进程运行时,您可以在终端中使用ps命令查看该进程的活动状态。