从Bash终端重启C程序

Jon*_*nes -1 c linux bash application-restart

我一直在研究我的学校项目,现在我已经坚持了几天.任何形式的帮助将非常感谢!  

到目前为止我尝试过的:  

  • 编译脚本.它正确编译,我可以通过键入./process.o来运行它,但是当我杀了它时它无法实现它,它会重新启动.我一直在谷歌搜索并尝试各种各样的东西,但似乎没有任何工作,它总是杀死过程,但不会重新启动它.
  • 杀死-SIGKILL(PID)
  • 杀2(PID)
  • 杀死1(PID)
  • 杀死-HUP 3155
  • 各种其他命令只杀了它,似乎没什么用.我是否必须修改代码或其他内容?我很困惑.  

这就是我要做的事情:

用C创建一个新文件.用名字保存process.c(这个)

#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; 
}
Run Code Online (Sandbox Code Playgroud)

将以下代码编译为调用的工作程序process.o并启动该过程.(这是否适用于此点)

使用kill重新启动的命令process.o(杀死进程有效,但不重启)

dbu*_*ush 5

您需要保持父进程运行以监视子进程.如果父级检测到该子级不再运行,则可以重新启动它.

父母可以使用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)