Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have a program called <code>timeout</code> that does that - written in C, originally in 1989 but updated periodically since then.</p> <p><hr> Update: this code fails to compile on MacOS X because SIGRTMIN is not defined, and fails to timeout when run on MacOS X because the <code>signal()</code> function there resumes the <code>wait()</code> after the alarm times out - which is not the required behaviour. I have a new version of <code>timeout.c</code> which deals with both these problems (using <code>sigaction()</code> instead of <code>signal()</code>). As before, contact me for a 10K gzipped tar file with the source code and a manual page (see my profile).</p> <hr> <pre><code>/* @(#)File: $RCSfile: timeout.c,v $ @(#)Version: $Revision: 4.6 $ @(#)Last changed: $Date: 2007/03/01 22:23:02 $ @(#)Purpose: Run command with timeout monitor @(#)Author: J Leffler @(#)Copyright: (C) JLSS 1989,1997,2003,2005-07 */ #define _POSIX_SOURCE /* Enable kill() in &lt;unistd.h&gt; on Solaris 7 */ #define _XOPEN_SOURCE 500 #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;signal.h&gt; #include &lt;errno.h&gt; #include &lt;unistd.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/wait.h&gt; #include "stderr.h" #define CHILD 0 #define FORKFAIL -1 static const char usestr[] = "[-vV] -t time [-s signal] cmd [arg ...]"; #ifndef lint /* Prevent over-aggressive optimizers from eliminating ID string */ const char jlss_id_timeout_c[] = "@(#)$Id: timeout.c,v 4.6 2007/03/01 22:23:02 jleffler Exp $"; #endif /* lint */ static void catcher(int signum) { return; } int main(int argc, char **argv) { pid_t pid; int tm_out; int kill_signal; pid_t corpse; int status; int opt; int vflag = 0; err_setarg0(argv[0]); opterr = 0; tm_out = 0; kill_signal = SIGTERM; while ((opt = getopt(argc, argv, "vVt:s:")) != -1) { switch(opt) { case 'V': err_version("TIMEOUT", &amp;"@(#)$Revision: 4.6 $ ($Date: 2007/03/01 22:23:02 $)"[4]); break; case 's': kill_signal = atoi(optarg); if (kill_signal &lt;= 0 || kill_signal &gt;= SIGRTMIN) err_error("signal number must be between 1 and %d\n", SIGRTMIN - 1); break; case 't': tm_out = atoi(optarg); if (tm_out &lt;= 0) err_error("time must be greater than zero (%s)\n", optarg); break; case 'v': vflag = 1; break; default: err_usage(usestr); break; } } if (optind &gt;= argc || tm_out == 0) err_usage(usestr); if ((pid = fork()) == FORKFAIL) err_syserr("failed to fork\n"); else if (pid == CHILD) { execvp(argv[optind], &amp;argv[optind]); err_syserr("failed to exec command %s\n", argv[optind]); } /* Must be parent -- wait for child to die */ if (vflag) err_remark("time %d, signal %d, child PID %u\n", tm_out, kill_signal, (unsigned)pid); signal(SIGALRM, catcher); alarm((unsigned int)tm_out); while ((corpse = wait(&amp;status)) != pid &amp;&amp; errno != ECHILD) { if (errno == EINTR) { /* Timed out -- kill child */ if (vflag) err_remark("timed out - send signal %d to process %d\n", (int)kill_signal, (int)pid); if (kill(pid, kill_signal) != 0) err_syserr("sending signal %d to PID %d - ", kill_signal, pid); corpse = wait(&amp;status); break; } } alarm(0); if (vflag) { if (corpse == (pid_t) -1) err_syserr("no valid PID from waiting - "); else err_remark("child PID %u status 0x%04X\n", (unsigned)corpse, (unsigned)status); } if (corpse != pid) status = 2; /* Dunno what happened! */ else if (WIFEXITED(status)) status = WEXITSTATUS(status); else if (WIFSIGNALED(status)) status = WTERMSIG(status); else status = 2; /* Dunno what happened! */ return(status); } </code></pre> <p>If you want the 'official' code for 'stderr.h' and 'stderr.c', contact me (see my profile).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload