diff options
Diffstat (limited to 'src/cpulimit.c')
-rw-r--r-- | src/cpulimit.c | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/src/cpulimit.c b/src/cpulimit.c index 9a9ce1b..e7d5309 100644 --- a/src/cpulimit.c +++ b/src/cpulimit.c @@ -27,22 +27,20 @@ * */ -#include <getopt.h> #include <stdio.h> -#include <fcntl.h> #include <stdlib.h> -#include <time.h> #include <unistd.h> +#include <getopt.h> +#include <time.h> #include <signal.h> #include <string.h> -#include <dirent.h> #include <errno.h> #include <string.h> #include <sys/stat.h> #include <sys/time.h> -#include <sys/types.h> #include <sys/sysctl.h> #include <sys/resource.h> +#include <sys/types.h> #include <sys/wait.h> #include "process_group.h" @@ -226,13 +224,19 @@ void limit_process(pid_t pid, double limit, int include_children) } //resume processes - for (node = pgroup.proclist->first; node != NULL; node = node->next) { + node = pgroup.proclist->first; + while (node != NULL) + { + struct list_node *next_node = node->next; struct process *proc = (struct process*)(node->data); - if (kill(proc->pid,SIGCONT)!=0) { + if (kill(proc->pid,SIGCONT) != 0) { //process is dead, remove it from family - if (verbose) fprintf(stderr, "Process %d dead!\n", proc->pid); - //remove_process_from_family(&pf, proc->pid); + if (verbose) fprintf(stderr, "SIGCONT failed. Process %d dead!\n", proc->pid); + //remove process from group + delete_node(pgroup.proclist, node); + remove_process(&pgroup, proc->pid); } + node = next_node; } //now processes are free to run (same working slice for all) @@ -248,14 +252,20 @@ void limit_process(pid_t pid, double limit, int include_children) } if (tsleep.tv_nsec>0) { - //stop only if tsleep>0 - for (node = pgroup.proclist->first; node != NULL; node = node->next) { + //stop processes only if tsleep>0 + node = pgroup.proclist->first; + while (node != NULL) + { + struct list_node *next_node = node->next; struct process *proc = (struct process*)(node->data); if (kill(proc->pid,SIGSTOP)!=0) { //process is dead, remove it from family - if (verbose) fprintf(stderr,"Process %d dead!\n", proc->pid); - //remove_process_from_family(&pf, proc->pid); + if (verbose) fprintf(stderr, "SIGSTOP failed. Process %d dead!\n", proc->pid); + //remove process from group + delete_node(pgroup.proclist, node); + remove_process(&pgroup, proc->pid); } + node = next_node; } //now the processes are sleeping nanosleep(&tsleep,NULL); @@ -432,7 +442,7 @@ int main(int argc, char **argv) { else { //limiter code if (verbose) printf("Limiting process %d\n",child); - limit_process(child, limit, ignore_children); + limit_process(child, limit, !ignore_children); exit(0); } } @@ -471,7 +481,7 @@ int main(int argc, char **argv) { } printf("Process %d found\n", pid); //control - limit_process(pid, limit, !ignore_children); + limit_process(pid, limit, ignore_children); } if (lazy) break; sleep(2); |