Create a gist now

Instantly share code, notes, and snippets.

@darcyliu /hello.c Secret
Created Oct 6, 2012

monitor process resource usage
#include <stdio.h>
#include <stdlib.h>
int main(){
//while(1){};
//sleep(5);
char *p;
long x = 10000,y=1000000;
while(x-->0){
while(y-->0){
//printf("%ld %ld\n",x,y);
p=(char *)malloc(1024);
}
}
p=(char *)malloc(1024);
scanf("%s",p);
printf("Hello World %s\n",p);
fprintf(stderr,"err\n");
free(p);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <signal.h> // signal
#include <sys/time.h> // setrlimit
#include <sys/resource.h> // setrlimit
#include <sys/wait.h> // fork
#include <errno.h>
#include <pwd.h>
#include <fcntl.h>
static const char *optString = "t:m:e:i:o:h";
static const struct option longOpts[] = {
{"time", required_argument, NULL, 't'},
{"memory", required_argument, NULL, 'm'},
{"execute", required_argument, NULL, 'e'},
{"input", required_argument, NULL, 'i'},
{"output", required_argument, NULL, 'o'},
{"help", no_argument, NULL, 'h'},
{ NULL, no_argument, NULL, 0 }
};
struct limit_info {
const char *execute_filename;
const char *stdin_filename;
const char *stdout_filename;
const char *stderr_filename;
long int time_limit;
long int memory_limit;
long int vm_limit;
long int output_limit;
int stack_limit;
int proc_limit;
int file_limit;
int trace;
const char* working_dir;
};
int set_limit(int resource, unsigned int limit) {
struct rlimit t;
t.rlim_max = limit;
t.rlim_cur = limit;
if (setrlimit(resource, &t) == -1) {
printf("setrlimit error\n");
return -1;
}
return 0;
}
void help(){
printf("usage:\n");
printf("./monitor -e hello -i input.txt -o output.txt -t 1000 -m 32768\n");
}
int get_options(int argc, char *argv[], struct limit_info *info){
int longIndex = 0, opt = 0;
opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
while (opt != -1) {
switch (opt) {
case 't':
info->time_limit = atoi(optarg);
break;
case 'm':
info->memory_limit = atoi(optarg);
break;
case 'e':
info->execute_filename = optarg;
break;
case 'i':
info->stdin_filename = optarg;
break;
case 'o':
info->stdout_filename = optarg;
break;
case 'h':
help();
break;
default:
break;
}
opt = getopt_long(argc, argv, optString, longOpts, &longIndex);
}
return 0;
}
int get_time(struct timeval u,struct timeval s) {
//printf("u: %ld %ld\n",u.tv_usec,u.tv_sec);
//printf("s: %ld %ld\n",u.tv_usec,u.tv_sec);
long r = ((u.tv_usec+s.tv_usec)/1000) + (u.tv_sec+s.tv_sec) * 1000;
//printf("r %ld\n",r);
return r;
}
int execute(struct limit_info info){
int status;
int pid = fork();
const char* commands[] = {info.execute_filename, NULL};
struct rusage usage;
int ret;
pid_t w;
if(pid==0){
set_limit(RLIMIT_CPU, (int) (info.time_limit) / 1000);
set_limit(RLIMIT_DATA,(int) (info.memory_limit) * 1024);
set_limit(RLIMIT_AS, (int) (info.vm_limit) * 1024);
set_limit(RLIMIT_FSIZE, (int) (info.output_limit) * 1024);
set_limit(RLIMIT_STACK, (int) (info.stack_limit) * 1024);
set_limit(RLIMIT_NOFILE, (int) info.file_limit);
set_limit(RLIMIT_NPROC, info.proc_limit);
int fd_in = open(info.stdin_filename, O_RDONLY, 0777);
// printf("fd_in %d\n",fd_in);
int fd_out = open(info.stdout_filename, O_RDWR | O_CREAT | O_TRUNC, 0777);
// printf("fd_out %d\n",fd_out);
int fd_err= open(info.stderr_filename, O_RDWR | O_CREAT | O_TRUNC, 0777);
// printf("fd_err %d\n",fd_err);
int a = dup2(fd_in, STDIN_FILENO);
int b = dup2(fd_out, STDOUT_FILENO);
dup2(fd_err, STDERR_FILENO);
close(fd_in);
close(fd_out);
close(fd_err);
execv(commands[0], (char**)(commands + 1));
printf("command ls is not found, error code: %d(%s)", errno, strerror(errno));
}else{
w = wait3(&status, WUNTRACED, &usage);
int time = get_time(usage.ru_utime,usage.ru_stime);
long memory = usage.ru_minflt * (getpagesize() >> 10);
printf("time limit: %ld used: %ld\n",info.time_limit,time);
printf("memroy limit: %ld used: %ld\n",info.memory_limit,memory);
if(time > info.time_limit){
printf("Time Limit Exceeded\n");
}
if (memory > info.memory_limit) {
printf("Memory Limit Exceeded\n");
}
}
}
int main(int argc, char *argv[]){
struct limit_info info;
info.time_limit = 1000;
info.memory_limit = 32768;
info.vm_limit = info.memory_limit;
info.output_limit = 1024;
info.stack_limit = 8192;
info.proc_limit = 1;
info.file_limit = 10;
info.trace = 1;
info.stderr_filename = "/dev/null";
get_options(argc, argv,&info);
if(info.execute_filename==NULL || info.stdin_filename==NULL||info.stdout_filename==NULL){
help();
}else{
printf("---------------------------------------\n");
printf("time limit: %ld \n",info.time_limit);
printf("memory limit: %ld \n",info.memory_limit);
printf("execute_filename %s \n",info.execute_filename);
printf("input_filename %s \n",info.stdin_filename);
printf("output_filename %s \n",info.stdout_filename);
printf("error_filename %s \n",info.stderr_filename);
printf("-------------------execute--------------------\n");
execute(info);
}
return 0;
}
make hello
make monitor
echo "-------monitor--------"
./monitor -e hello -i input.txt -o output.txt -t 1000 -m 32768
echo "------Input------"
cat input.txt
echo "------Ouput------"
cat output.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment