0

I have an assignment to write a program to use fork off a children. That child will the fork off its own child (grandchild of the original parent). The grandchild should exec() to do a ps -ef (for example). The child should wait for its child (grandchild of original parent) to finish successfully. If it didn't finish successfully (I assume that the status return code is 0), it should spawn off another grandchild until it is successful. Once this is complete, it should send the SIGINT signal to its parent.


This is what I was doing, the second time I fork the grandchild, I exec as specified. Here, I set up a signal handler too. In the child, I wait (wait(&status)) and loop while (status != 0). That was the idea.

But, still, I couldn't get the program works. I guess I have the problem with signal handling (?) Can you give me a hint?

Kohn J.
  • 25
  • 2
  • 6

1 Answers1

0

The return value of things that don't succeed is -1. It is never a positive value in C.

 The exec() functions only return if an error has occurred. The return value is -1, and      errno is set to indicate the error.

Basically what you want to do is wait on your grandchild in your child. If your grandchild calls exec, and it fails, you will return from exec and continue execution in that program(grandchild). If the grandchild succeeds, you won't get an indication except your child process will start again. In your gradchild process, if you fail, you want to throw a signal up to your parent. and when the parent starts, you can check to see if this signal was received. If not, you know it completed successfully.

KrisSodroski
  • 2,796
  • 3
  • 24
  • 39