Hi,
I need to run a program that runs under certain limitations( like
memory and execution time) and also I need to redirect its input and
output streams
I have this code below which limits time and memmory but I dont know
how to fit in the redirection. I know you can use "<" and ">" operators
in shell but how do I do it in C/C++?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
struct rlimit lim;
int status;
if ( argc < 4 ) {
fprintf(stderr, "Usage: %s <time> <memory> <exe>\n",
argv[0]);
exit(EXIT_FAILURE);
}
if (fork() == 0) {
lim.rlim_cur = lim.rlim_max = atoi(argv[1]);
setrlimit(RLIMIT_CPU, &lim);
lim.rlim_cur = lim.rlim_max = atoi(argv[2]) * 1024 * 1024;
setrlimit(RLIMIT_AS, &lim);
if (execl(argv[3], NULL) == -1)
fprintf(stderr, "I can't run the program.\n");
} else {
wait(&status);
if (WIFEXITED(status))
fprintf(stderr, "Program enden successfully with exit code: %d\n",
WEXITSTATUS(status));
else if (WIFSIGNALED(status))
fprintf(stderr, "Program ended with signal #%d\n",
WTERMSIG(status));
}
return EXIT_SUCCESS;
}
TIA,
sklitzz