Commit b37c8610 authored by Andrei Paskevich's avatar Andrei Paskevich

retouch cpulimit.c

parent 6890dea1
......@@ -978,7 +978,9 @@ $(DISTRIB_TAR): doc/manual.pdf
headers:
headache -c misc/headache_config.txt -h misc/header.txt \
Makefile.in configure.in */*.ml */*/*.ml */*/*.ml[ily4]
Makefile.in configure.in src/*.ml* src/*/*.ml* \
src/tools/cpulimit.c
#########
# myself
......
/**************************************************************************/
/* */
/* The Why platform for program certification */
/* Copyright (C) 2002-2008 */
/* Romain BARDOU */
/* Jean-Franois COUCHOT */
/* Mehdi DOGGUY */
/* Jean-Christophe FILLITRE */
/* Thierry HUBERT */
/* Claude MARCH */
/* Yannick MOY */
/* Christine PAULIN */
/* Yann RGIS-GIANAS */
/* Nicolas ROUSSET */
/* Xavier URBAIN */
/* Copyright (C) 2010 */
/* François Bobot */
/* Jean-Christophe Filliâtre */
/* Claude Marché */
/* Andrei Paskevich */
/* */
/* This software is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Library General Public */
/* License version 2, with the special exception on linking */
/* License version 2.1, with the special exception on linking */
/* described in file LICENSE. */
/* */
/* This software is distributed in the hope that it will be useful, */
......@@ -39,36 +31,41 @@
int main(int argc, char *argv[]) {
int timelimit, memlimit;
int showtime, hidetime;
struct rlimit res;
if (argc < 5) {
fprintf(stderr, "usage: %s <time limit in seconds> \
<virtual memory limit in MiB> <-s|-h> <command>\n\
a null value sets no limit (keeps the actual limit)\n", argv[0]);
showtime = argc >= 4 && !strncmp("-s",argv[3],3);
hidetime = argc >= 4 && !strncmp("-h",argv[3],3);
if (argc < 5 || !(showtime || hidetime)) {
fprintf(stderr, "usage: %s <time limit in seconds> <virtual memory limit in MiB>\n"
" <show/hide cpu time: -s|-h> <command> <args>...\n\n"
"Zero sets no limit (keeps the actual limit)\n", argv[0]);
return EXIT_FAILURE;
}
/* Fork if requested */
if(strcmp("-s",argv[3]) == 0){
if (showtime) {
int pid = fork ();
if (pid == -1){
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0){
/* The child continues to execute the program */
} else {
/* The parent will not exit this condition */
}
if (pid > 0) {
int status;
waitpid(pid, &status, 0);
struct tms tms;
double time;
waitpid(pid, &status, 0);
times(&tms);
double time = (double)((tms.tms_cutime + tms.tms_cstime + 0.0)
time = (double)((tms.tms_cutime + tms.tms_cstime + 0.0)
/ sysconf(_SC_CLK_TCK));
fprintf(stdout, "why3cpulimit time : %f s\n",time);
if (WIFEXITED(status)){
return WEXITSTATUS(status);
}
fprintf(stdout, "why3cpulimit time : %f s\n", time);
if (WIFEXITED(status)) return WEXITSTATUS(status);
kill(getpid(),SIGTERM);
}
}
......@@ -97,6 +94,7 @@ a null value sets no limit (keeps the actual limit)\n", argv[0]);
execvp(argv[4],argv+4);
fprintf(stderr, "%s: exec of '%s' failed (%s)\n",
argv[0],argv[4],strerror(errno));
return EXIT_FAILURE;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment