A way to time-limit a program in C - Unix

This is a discussion on A way to time-limit a program in C - Unix ; Is there a simple way to limit the amount of time that a program (vfork()+execvp()) can run? i.e., can you do this: main thread:...

+ Reply to Thread
Results 1 to 4 of 4

Thread: A way to time-limit a program in C

  1. A way to time-limit a program in C

    Is there a simple way to limit the amount of time that a program
    (vfork()+execvp()) can run?
    i.e., can you do this:
    main thread:

  2. Re: A way to time-limit a program in C

    andreyvul wrote:
    > Is there a simple way to limit the amount of time that a program
    > (vfork()+execvp()) can run?
    > i.e., can you do this:
    > main thread:

    apropos limit
    or: man 2 setrlimit

  3. Re: A way to time-limit a program in C

    On Apr 8, 11:51 pm, Frank Mertens wrote:
    > andreyvul wrote:
    > > Is there a simple way to limit the amount of time that a program
    > > (vfork()+execvp()) can run?
    > > i.e., can you do this:
    > > main thread:

    >
    > apropos limit
    > or: man 2 setrlimit

    Second granularity is too loose for me. A better solution (which I
    have tested) is this:
    int main(int argc, char **argv) {
    pid_t pid;
    clock_t cl;
    ....
    pid = fork();
    if (pid == 0) {
    ...
    execvp(...)
    } else {
    cl = clock();
    if (cl - clock() >= timeout * 1000)
    if (waitpid(pid, WNOHANG))
    kill(pid, 9);
    }
    ....
    return 0;
    }

    If it stops an infinite loop in timeout milliseconds, I'm happy.

  4. Re: A way to time-limit a program in C

    andreyvul wrote:
    > Is there a simple way to limit the amount of time that a program
    > (vfork()+execvp()) can run?
    > i.e., can you do this:
    > main thread:


    man signal(), SIGALRM, alarm().

    --
    Regards,
    Stan Milam.
    --
    "The distractions of constant e-mails, text and phone messages
    are a greater threat to IQ and concentration than taking cannabis."
    Glen Wilson

+ Reply to Thread