1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
/*
* This is a simple program that calls BC and terminates it if it takes longer
* than five seconds to complete. There is no real reason for this to be done
* in C instead of Python, however there are some key advantages:
*
* - Mango gets to play with C making him happy
* - C is compiled instead of interpreted so this is technically more efficient
* - Ok mayyyybe is slower because it uses a seperate process?
* - It makes us look smarter?
*/
#define _POSIX_SOURCE
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include "math.h"
bool timeout = false, child_done = false;
void timeout_hander(int UNUSED(sig))
{
timeout = true;
}
void child_handler(int UNUSED(sig))
{
child_done = true;
}
int main(void)
{
pid_t pid = fork();
switch (pid) {
case 0:
execl("/bin/bc", "", "-q", "bc_funcs/lib.bc", "bc_funcs/init.bc",
FILE_NAME, "bc_funcs/exit.bc", NULL);
fallthrough;
case -1:
perror("calc");
return EXIT_FAILURE;
default:
/* Setup signal handlers after fork so the child doesnt inherit them */
signal(SIGALRM, timeout_hander);
signal(SIGCHLD, child_handler);
alarm(TIMEOUT);
pause();
/*
* Wait for either the child process to terminate, or for 5 seconds to
* have passed
*/
if (timeout) {
puts("Error: 5 second timeout reached");
kill(pid, 9);
}
break;
}
return EXIT_SUCCESS;
}
|