Use fork(2), execve(2), waitpid(2) instead of system
(which internally uses these syscalls). Read Advanced Linux Programming which explains the tricky details.
You could want to use setrlimit(2) in the child. Or in the parent kill(2) the child (first with SIGTERM
, then with SIGKILL
) on timeout.
You may want the child to make its own process group using setpgid(2) or setpgrp
, then kill the entire process group with killpg(2). This would kill both the child and any command it has started (unless these create there own process groups themselves).
You could handle the SIGCHLD
signal (see signal(7)) using sigaction(2). SIGCHLD
would notably be sent when the child process terminates.