PKUOS - Pintos
Pintos source browser for PKU Operating System course
setitimer-helper.c
Go to the documentation of this file.
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}
#define EXIT_FAILURE
Unsuccessful execution.
Definition: syscall.h:20
#define LONG_MAX
Definition: limits.h:26
int main(int argc, char *argv[])
#define NULL
Definition: stddef.h:4