Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit the execution time in C

I'm not a big C expert, and that's why I ask this (probably) easy question. I got a script written in C, and I'd like to limit the execution time with input.

Like: ./script.c input1 input2 input3 input4

Input4 has to be the time limit in seconds.

In php, it would be something like this:

$_GET['time'] = $time;

max_execution_time($time); or set_time_limit($time) And then I would run it like this: http://domain.com/script.php?time=60

I think that init_rand(time(NULL)); should work for the time limiting but how can I take the variable from the "GET" (input4)?

like image 431
user2298995 Avatar asked Jan 25 '26 04:01

user2298995


1 Answers

May be you can run this program as a child of another program.

if ((pid = fork()) < 0) {
    exit(1);
} else if (pid == 0) {
    execvp("this prog", argv);
} else  {
    sleep(atoi(argv[4]);
    kill(pid, SIGTERM);
}

Or may be you can use bash scripts to do the job.

./thisprog $1 $2 $3 &
sleep $4 
kill $! 2>/dev/null 
like image 79
mohit Avatar answered Jan 26 '26 18:01

mohit