diff options
author | Angelo Marletta <angelo.marletta@gmail.com> | 2012-06-01 14:57:44 -0700 |
---|---|---|
committer | Angelo Marletta <angelo.marletta@gmail.com> | 2012-06-01 14:57:44 -0700 |
commit | 0ba6a181d84c196c9d9854ded7528baac66af461 (patch) | |
tree | 5f398d8d841d1e5483ed385740b0220dbcfd734f | |
parent | 22ba146f7b9719f83dd40134326f6408c5ade917 (diff) | |
parent | 4e8c6f90e70acb1ce717e2bc9d1a7f278deb78cc (diff) | |
download | cpulimit-0ba6a181d84c196c9d9854ded7528baac66af461.tar.gz |
Merge pull request #1 from opsengine/develop
Develop
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | LICENSE | 16 | ||||
-rw-r--r-- | Makefile | 25 | ||||
-rw-r--r-- | src/Makefile | 22 | ||||
-rw-r--r-- | src/cpulimit.c (renamed from cpulimit.c) | 1 | ||||
-rw-r--r-- | src/list.c (renamed from list.c) | 0 | ||||
-rw-r--r-- | src/list.h (renamed from list.h) | 0 | ||||
-rw-r--r-- | src/process.c (renamed from process.c) | 0 | ||||
-rw-r--r-- | src/process.h (renamed from process.h) | 0 | ||||
-rw-r--r-- | src/procutils.c (renamed from procutils.c) | 24 | ||||
-rw-r--r-- | src/procutils.h (renamed from procutils.h) | 0 | ||||
-rw-r--r-- | tests/Makefile | 13 | ||||
-rwxr-xr-x | tests/busy | bin | 0 -> 8672 bytes | |||
-rw-r--r-- | tests/busy.c | 29 | ||||
-rw-r--r-- | tests/run_tests.sh | 0 |
15 files changed, 105 insertions, 28 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d390bb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +*~ +src/cpulimit @@ -0,0 +1,16 @@ +cpulimit - a CPU usage limiter for Linux +Copyright (C) 2012 Angelo Marletta + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. @@ -1,22 +1,5 @@ -CC?=gcc -CFLAGS?=-Wall -O2 -DPOSIXLY_CORRECT -TARGETS=cpulimit -LIBS=process.o procutils.o list.o - -all:: $(TARGETS) - -cpulimit: cpulimit.c $(LIBS) - $(CC) -o cpulimit cpulimit.c $(LIBS) $(CFLAGS) - -process.o: process.c process.h - $(CC) -c process.c $(CFLAGS) - -procutils.o: procutils.c procutils.h - $(CC) -c procutils.c $(CFLAGS) - -list.o: list.c list.h - $(CC) -c list.c $(CFLAGS) - -clean: - rm -f *~ *.o $(TARGETS) +default: all +.DEFAULT: + cd src && $(MAKE) $@ + cd tests && $(MAKE) $@ diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..33c632d --- /dev/null +++ b/src/Makefile @@ -0,0 +1,22 @@ +CC?=gcc +CFLAGS?=-Wall -O2 +TARGETS=cpulimit +LIBS=process.o procutils.o list.o + +all:: $(TARGETS) + +cpulimit: cpulimit.c $(LIBS) + $(CC) -o cpulimit cpulimit.c $(LIBS) $(CFLAGS) + +process.o: process.c process.h + $(CC) -c process.c $(CFLAGS) + +procutils.o: procutils.c procutils.h + $(CC) -c procutils.c $(CFLAGS) + +list.o: list.c list.h + $(CC) -c list.c $(CFLAGS) + +clean: + rm -f *~ *.o $(TARGETS) + diff --git a/cpulimit.c b/src/cpulimit.c index 0bf3d9c..300d320 100644 --- a/cpulimit.c +++ b/src/cpulimit.c @@ -496,6 +496,7 @@ int main(int argc, char **argv) { } else if (child > 0) { //parent code + free(cmd_args); int limiter = fork(); if (limiter < 0) { exit(EXIT_FAILURE); diff --git a/process.c b/src/process.c index 398d9e9..398d9e9 100644 --- a/process.c +++ b/src/process.c diff --git a/process.h b/src/process.h index 0280ad6..0280ad6 100644 --- a/process.h +++ b/src/process.h diff --git a/procutils.c b/src/procutils.c index 83f3e58..fb7aaae 100644 --- a/procutils.c +++ b/src/procutils.c @@ -192,8 +192,19 @@ int init_process_iterator(struct process_iterator *i) { i->c = 0; #endif - i->current = (struct process*)malloc(sizeof(struct process)); - memset(i->current, 0, sizeof(struct process)); + i->current = (struct process*)calloc(1, sizeof(struct process)); + return 0; +} + +int close_process_iterator(struct process_iterator *i) { +#ifdef __linux__ + if (closedir(i->dip) == -1) { + perror("closedir"); + return 1; + } +#elif defined __APPLE__ + //TODO +#endif return 0; } @@ -274,7 +285,7 @@ int create_process_family(struct process_family *f, pid_t father) ppid = getppid_of(ppid); } //allocate process descriptor - struct process *p = (struct process*)malloc(sizeof(struct process)); + struct process *p = (struct process*)calloc(1, sizeof(struct process)); //init process process_init(p, pid); if (ppid==1) { @@ -319,7 +330,7 @@ int update_process_family(struct process_family *f) exit(1); } //allocate and insert the process - struct process *p = (struct process*)malloc(sizeof(struct process)); + struct process *p = (struct process*)calloc(1, sizeof(struct process)); //init process process_init(p, pid); if (ancestor->member) { @@ -405,8 +416,6 @@ int look_for_process_by_name(const char *process_name) init_process_iterator(&iter); pid_t pid = 0; -printf("name\n"); - while (read_next_process(&iter)==0) { pid = iter.current->pid; @@ -435,6 +444,7 @@ printf("name\n"); } } } + if (close_process_iterator(&iter) != 1) exit(1); if (found == 1) { //ok, the process was found return pid; @@ -448,6 +458,6 @@ printf("name\n"); return -pid; } //this MUST NOT happen - return 0; + exit(2); } diff --git a/procutils.h b/src/procutils.h index b5a91a3..b5a91a3 100644 --- a/procutils.h +++ b/src/procutils.h diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..11f7e2a --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,13 @@ +CC?=gcc +CFLAGS?=-Wall -O2 +TARGETS=busy +LIBS?=-lpthread + +all:: $(TARGETS) + +busy: busy.c $(LIBS) + $(CC) -o busy busy.c $(LIBS) $(CFLAGS) + +clean: + rm -f *~ *.o $(TARGETS) + diff --git a/tests/busy b/tests/busy Binary files differnew file mode 100755 index 0000000..1a8a589 --- /dev/null +++ b/tests/busy diff --git a/tests/busy.c b/tests/busy.c new file mode 100644 index 0000000..bb2e2ed --- /dev/null +++ b/tests/busy.c @@ -0,0 +1,29 @@ +#include <stdio.h> +#include <stdlib.h> +#include <pthread.h> +#include <unistd.h> + +void *loop() +{ + while(1); +} + +int main(int argc, char **argv) { + + int i = 0; + int num_threads = 1; + if (argc == 2) num_threads = atoi(argv[1]); + for (i=0; i<num_threads; i++) + { + pthread_t thread; + int ret; + if ((ret = pthread_create(&thread, NULL, loop, NULL)) != 0) + { + printf("pthread_create() failed. Error code %d\n", ret); + exit(1); + } + } + printf("Press ENTER to exit..."); + getchar(); + return 0; +}
\ No newline at end of file diff --git a/tests/run_tests.sh b/tests/run_tests.sh new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/run_tests.sh |