diff options
author | bhackerozzo <bhackerozzo@3b445381-d045-0410-9223-e17ecdb95f4b> | 2008-02-10 23:32:35 +0000 |
---|---|---|
committer | bhackerozzo <bhackerozzo@3b445381-d045-0410-9223-e17ecdb95f4b> | 2008-02-10 23:32:35 +0000 |
commit | 05126ab3795764303dbe8d33b45b9b838ab7374e (patch) | |
tree | 3a8becf7b9252b963187061e3a6e6ecceff5984e | |
parent | 53e1b8a223b1bc06c5d2d1fe655917430074da98 (diff) | |
download | cpulimit-05126ab3795764303dbe8d33b45b9b838ab7374e.tar.gz |
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | loop.c | 9 | ||||
-rw-r--r-- | procutils.c | 71 |
3 files changed, 13 insertions, 69 deletions
@@ -11,7 +11,7 @@ processtest: processtest.c process.o gcc -o processtest processtest.c process.o -lrt $(CFLAGS) loop: loop.c - gcc -o loop loop.c $(CFLAGS) + gcc -o loop loop.c -lpthread $(CFLAGS) process.o: process.c process.h gcc -c process.c $(CFLAGS) @@ -1,9 +1,18 @@ #include <stdio.h> #include <sys/types.h> #include <unistd.h> +#include <pthread.h> + +void *loop() { + printf("thread PID: %d\n", getpid()); + while(1); +} int main() { printf("PID: %d\n", getpid()); + pthread_t th; + pthread_create(&th, NULL, loop, NULL); while(1); + return 0; } diff --git a/procutils.c b/procutils.c index 0e52b59..bcc8abf 100644 --- a/procutils.c +++ b/procutils.c @@ -145,13 +145,6 @@ int look_for_process_by_name(const char *process) } -static int is_2_6_kernel() { - //kernel version detection - struct utsname uts; - uname(&uts); - return (strncmp(uts.release, "2.6", 3) == 0); -} - static int getppid_of(int pid) { char file[20]; @@ -188,6 +181,8 @@ static void visit_tree(struct tree_node *node, int **children, int *children_cou return; } +//TODO: add a get_process_tree(), get_sub_processes(tree), update_process_tree() + int get_sub_processes(int pid, int **children) { //stuff for reading directories @@ -209,7 +204,7 @@ int get_sub_processes(int pid, int **children) //read in from /proc and seek for processes while ((dit = readdir(dip)) != NULL) { - //get pid + //parse pid int pid2 = atoi(dit->d_name); if (pid2 == 0) continue; //not a process //check if the array must be enlarged @@ -282,66 +277,6 @@ int get_sub_processes(int pid, int **children) //shrink children to the real size *children = realloc(*children, sizeof(int) * children_count); - //look for threads of our children (only for Linux 2.6.x) - if (is_2_6_kernel()) { - int thread_block = 8; - int thread_count = 0; - int *threads = malloc(sizeof(int)*thread_block); - char filename[30]; - for (i=-1; i<children_count; i++) { - int proc_pid = (i==-1 ? pid : (*children)[i]); - sprintf(filename, "/proc/%d/task", proc_pid); - //open a directory stream to /proc directory - if ((dip = opendir(filename)) == NULL) { - perror("opendir"); - return -20; - } - while ((dit = readdir(dip)) != NULL) { - //get pid - int task_pid = atoi(dit->d_name); - if (task_pid == 0) continue; //not a process - if (task_pid == proc_pid) continue; //the same process - //a new thread was found! - if (thread_count>0 && (thread_count % thread_block == 0)) { - //reallocate - threads = realloc(threads, sizeof(int) * (thread_count + thread_block)); - } - //add the pid of the thread - threads[thread_count++] = task_pid; - } - //close the dir stream and check for errors - if (closedir(dip) == -1) { - perror("closedir"); - return -11; - } - } - //shrink thread array - threads = realloc(threads, sizeof(int) * thread_count); - - /* printf("Process %d has %d children:\n", pid, children_count); - for (i=0; i<children_count; i++) { - printf(" %d\n", (*children)[i]); - } - - printf("Process and children %d have %d threads:\n", pid, thread_count); - for (i=0; i<thread_count; i++) { - printf(" %d\n", threads[i]); - } */ - - //unify children and threads in the same array - *children = realloc(*children, sizeof(int) * (children_count + thread_count)); - for (i=0; i<thread_count; i++) { - (*children)[children_count++] = threads[i]; - } - - free(threads); - - printf("Process %d has %d subprocesses:\n", pid, children_count); - for (i=0; i<children_count; i++) { - printf(" %d\n", (*children)[i]); - } - } - //memory cleanup for (i=0; i<proc_count; i++) { free(nodes[i].children); |