0

比如时间限制设置为 15 秒,然后程序在 15 秒后自动关闭,如何在 C 中做到这一点,提前致谢

4

1 回答 1

4

如果您使用的是 POSIX 系统,请使用alarm(3)(感谢@Duck 在他的评论中首先提出这一建议):

#include <stdio.h>
#include <signal.h>
#include <unistd.h>

static void sighandler(int sig) {
    if (sig == SIGALRM)
        exit(0);
}

int main(int argc, const char **argv) {
    signal(SIGALRM, sighandler);
    alarm(15);

    // do things here
}
于 2013-09-04T06:10:15.997 回答