0

I'm writing a C program that pipes the output of a first program into the input of a second program. The program needs to terminate with the use of sigaction and SIGKILL if either program takes longer than 3 seconds to reach termination.

My program works on "good" inputs. I'm not sure how to implement it so that it behaves as it should for processes that exceed 3 seconds. This is my first time working with interprocess communication and I'm forced to use sigaction to facilitate it. I'm completely lost with the implementation of this part of the program.

I create two child processes: one for each program. How can I use system calls, sigaction, and SIGKILL to terminate the entire program if one of my child processes takes longer than 3 seconds to terminate?

  • Which calls have you considered using? Which calls implement timing functions? Could you use `sleep()`? Does your program need to terminate as the programs do if they're quicker than 3 seconds, or can it wait 3 seconds unconditionally? – Jonathan Leffler May 25 '19 at 22:20
  • I'm not sure which calls to use. I think I would need to use alarm(3), but generally I don't know how to accomplish what I need. I am allowed to use sleep(). The program should not unconditionally take 3 seconds to execute. – agraw1o1 May 25 '19 at 22:30
  • Given that you only need a timer for an integral number of seconds, using `alarm()` is reasonable. So, go for it — what does your code look like when you use `alarm()` and `sigaction()`? We won't write your code for you; we will help you fix problems in an honest attempt to solve the problem. (I'll note in passing that SIGKILL is brutal. It would often be better to try SIGHUP and/or SIGTERM first, only using SIGKILL if the child ignores the other death threats.) – Jonathan Leffler May 25 '19 at 22:33
  • I haven't developed anything yet because I'm trying to figure out how to begin. Can you suggest an approach that can give me direction? – agraw1o1 May 25 '19 at 23:19
  • Create a handler for SIGALRM. Use `sigaction()` to catch the signal. Set an alarm call. Go into a loop waiting for the children to die. If the `wait()` or `waitpid()` calls is interrupted because of the alarm, break the loop and kill the kids. If both children exit first, break the loop without killing the (already dead) kids. All this after starting the two child processes. – Jonathan Leffler May 25 '19 at 23:29
  • That seems like it makes a lot of sense. Thank you! – agraw1o1 May 25 '19 at 23:39

0 Answers0