So my objective is to spawn n child processes and let them run concurrently(each one exec's a different program).
The tricky thing is that, for each one of them, I have to assure that they don't exceed a predetermined amount of time executing.(not globally, but relative to it's start time).
I only have a working code that spawns the processes, execs each one and then waits for all of them to finish.(based on this answer).
I've tried to use SIGALRM
but I can not figure out how to set one alarm per fork
so it does timeout each process relative to its start time and not based on the parent's starting time.
Regarding time measuring I'm not sure how could I get the execution time of each fork.
In a regular situation I would just get the delta of start and finishing time inside the child code, but in this case and if I'm not mistaken all of them execute something so I lose any reference of the following code.
for (int j = 0; j < prog_count; j++) {
pid_t monitor_pid = fork();
if(monitor_pid==0){
execvp(programs[j]->executable, programs[j]->exec_args);
}
}
while ((wpid = wait(&status)) > 0);
I've seen a lot of examples that spawn one child and control it's execution time with a sleeping timer process that runs in parallel, but i can't figure out how to extend that solution to my situation.