Jon*_*nes -1 c linux bash application-restart
我一直在研究我的学校项目,现在我已经坚持了几天.任何形式的帮助将非常感谢!
到目前为止我尝试过的:
这就是我要做的事情:
用C创建一个新文件.用名字保存
process.c(这个)Run Code Online (Sandbox Code Playgroud)#include <stdio.h> #include <unistd.h> int main() { printf("Creating a background process..\n"); pid_t pid = fork(); if (pid > 0) return 0; /* Host process ends */ if (pid < 0) return -1; /* Forking didn't work */ while(1) { } /* While loop */ return 0; }将以下代码编译为调用的工作程序
process.o并启动该过程.(这是否适用于此点)使用
kill重新启动的命令process.o(杀死进程有效,但不重启)
您需要保持父进程运行以监视子进程.如果父级检测到该子级不再运行,则可以重新启动它.
父母可以使用wait系统调用来检测孩子何时退出.
while (1) {
pid_t pid = fork();
if (pid < 0) {
return -1;
} else if (pid > 0) {
// parent waits for child to finish
// when it does, it goes back to the top of the loop and forks again
wait(NULL);
} else {
// child process
while (1);
}
}
Run Code Online (Sandbox Code Playgroud)