/* * cpulimiter - run a program with a CPU time limit, printing a * student-oriented message if the time limit is reached. * Someone must have written and circulated a program like this, but I can't * find it. * Alan J Rosenthal, 13 October 2000. * (Revised 2003, 2007 for the CSC 209/B09 web page.) */ #include #include #include #include void sigchld(int x) { _exit(0); } int main(int argc, char **argv) { int pid; if (argc < 2) { fprintf(stderr, "usage: limiter command\n"); return(1); } if ((pid = fork()) == -1) { perror("fork"); return(1); } if (pid) { signal(SIGCHLD, sigchld); sleep(10); /* won't get here if SIGCHLD happened */ signal(SIGCHLD, SIG_DFL); if (kill(pid, 9) == 0 || errno != ESRCH /* No such process */) fprintf(stderr, "Time limit exceeded. Probably in an infinite loop.\n"); } else { argv++; if (execv(argv[0], argv)) perror(argv[0]); } return(0); }