-1

I've been working on my school project and I've been stuck on this step for a few days now. Any kind of help would be highly appreciated!  

What I've tried so far:  

  • Compiling the script. It compiles correctly and I'm able to run it by typing ./process.o, however I cannot make it so when I kill it, it restarts. I've been googling and trying various things but nothing seems to work, it always kills the process but doesn't restart it.
  • kill -SIGKILL (PID)
  • kill 2 (PID)
  • kill 1 (PID)
  • kill -HUP 3155
  • Various other commands that only killed it, nothing seems to work. Do I have to modify the code or something? I'm deeply confused.  

Here's what I have to do:

Make a new file with C. Save it with name process.c (Did this)

#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; 
}

Compile the following code to working program called process.o and start the process. (Did this, works to this point)

Use a kill command which restarts the process.o (Killing the process works, but it doesn't restart it)

kirelagin
  • 13,248
  • 2
  • 42
  • 57
Jones
  • 41
  • 1
  • 1
  • 10
  • 1
    Are you sure that is exactly the task that you have to do? Because it doesn’t make much sense to me. The `kill` command won’t restart anything. – kirelagin Jan 29 '16 at 13:29
  • 1
    Also using the `.o` extension for the resulting _linked binary_ is kind of weird. – kirelagin Jan 29 '16 at 13:30
  • Yeah I'm kinda confused as well. I know the basics of Linux and C/C# and Java but this doesn't make any sense to me. I don't know if I should modify the code somehow but the task is to: Use the kill command to start process.o again. – Jones Jan 29 '16 at 13:32
  • In that case, I’d say that the right thing to do is to ask your professor for clarification. – kirelagin Jan 29 '16 at 13:33
  • Another way on a Unixy system: you can run a process under control of `init`, by putting entries in `/etc/inittab`. Then if the process is killed, it gets restarted by `init`. – gilez Jan 29 '16 at 15:39
  • What is it that makes you think this program should restart? – John Hascall Jan 29 '16 at 19:36
  • @John Hascall The assignment was unclear, it basically said I have to compile the code and run it, then restart it by using a kill command. It didn't point out I have to edit the code at all, and when you combine that with my small Linux base knowledge, there's no way how I could know what's wrong, is the code incorrect or am I trying to use the wrong command. But anyway, it's been solved thanks to the reply below. – Jones Jan 29 '16 at 19:43
  • @gilez Good to know, I'll remember that in the future. Thanks. :) – Jones Jan 29 '16 at 19:44
  • Perhaps the instructions are asking for something very simple: `./process.o` `ps ax | grep process (to find pid)` `kill pid` `./process.o` -- that is the thing that restarts it is you? – John Hascall Jan 29 '16 at 19:45

1 Answers1

5

You need to keep the parent process running to monitor the child process. If the parent detects that the child is no longer running, it can restart it.

The parent can use the wait system call to detect when the child exits.

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);
    }
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • I can't thank you enough, this solved my problem. Thank you! :) – Jones Jan 29 '16 at 14:06
  • @Jones Glad I could help. Feel free to [accept this answer](http://stackoverflow.com/help/accepted-answer) if you found it useful. – dbush Jan 29 '16 at 14:09