summaryrefslogtreecommitdiffstats
path: root/pintos-progos/utils/setitimer-helper.c
diff options
context:
space:
mode:
Diffstat (limited to 'pintos-progos/utils/setitimer-helper.c')
-rw-r--r--pintos-progos/utils/setitimer-helper.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/pintos-progos/utils/setitimer-helper.c b/pintos-progos/utils/setitimer-helper.c
new file mode 100644
index 0000000..772d736
--- /dev/null
+++ b/pintos-progos/utils/setitimer-helper.c
@@ -0,0 +1,49 @@
1#include <errno.h>
2#include <limits.h>
3#include <math.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <sys/time.h>
8#include <unistd.h>
9
10int
11main (int argc, char *argv[])
12{
13 const char *program_name = argv[0];
14 double timeout;
15
16 if (argc < 3)
17 {
18 fprintf (stderr,
19 "setitimer-helper: runs a program with a virtual CPU limit\n"
20 "usage: %s TIMEOUT PROGRAM [ARG...]\n"
21 " where TIMEOUT is the virtual CPU limit, in seconds,\n"
22 " and remaining arguments specify the program to run\n"
23 " and its argument.\n",
24 program_name);
25 return EXIT_FAILURE;
26 }
27
28 timeout = strtod (argv[1], NULL);
29 if (timeout >= 0.0 && timeout < LONG_MAX)
30 {
31 struct itimerval it;
32
33 it.it_interval.tv_sec = 0;
34 it.it_interval.tv_usec = 0;
35 it.it_value.tv_sec = timeout;
36 it.it_value.tv_usec = (timeout - floor (timeout)) * 1000000;
37 if (setitimer (ITIMER_VIRTUAL, &it, NULL) < 0)
38 fprintf (stderr, "%s: setitimer: %s\n",
39 program_name, strerror (errno));
40 }
41 else
42 fprintf (stderr, "%s: invalid timeout value \"%s\"\n",
43 program_name, argv[1]);
44
45 execvp (argv[2], &argv[2]);
46 fprintf (stderr, "%s: couldn't exec \"%s\": %s\n",
47 program_name, argv[2], strerror (errno));
48 return EXIT_FAILURE;
49}