diff options
author | Angelo Marletta <angelo.marletta@gmail.com> | 2012-07-02 12:06:11 +0100 |
---|---|---|
committer | Angelo Marletta <angelo.marletta@gmail.com> | 2012-07-02 12:06:11 +0100 |
commit | 0e7272f3896788bbc53614de1a1b30b082a988d0 (patch) | |
tree | 69bb7e4949a84a6fbff9b3cbc1dcaf0ffa163ba6 | |
parent | 95bcebd20c124701a4eb2233213938d0f2441c37 (diff) | |
download | cpulimit-0e7272f3896788bbc53614de1a1b30b082a988d0.tar.gz |
added function get_pid_max() to detect the maximum pid value
-rw-r--r-- | src/cpulimit.c | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/src/cpulimit.c b/src/cpulimit.c index e7d5309..f35a954 100644 --- a/src/cpulimit.c +++ b/src/cpulimit.c @@ -147,6 +147,26 @@ static int get_ncpu() { return ncpu; } +int get_pid_max() +{ +#ifdef __linux__ + //read /proc/sys/kernel/pid_max + static char buffer[1024]; + FILE *fd = fopen("/proc/sys/kernel/pid_max", "r"); + if (fd==NULL) return -1; + if (fgets(buffer, sizeof(buffer), fd)==NULL) { + fclose(fd); + return -1; + } + fclose(fd); + return atoi(buffer); +#elif defined __FreeBSD__ + return 99998; +#elif defined __APPLE__ + return 99998; +#endif +} + void limit_process(pid_t pid, double limit, int include_children) { //slice of the slot in which the process is allowed to run @@ -347,7 +367,7 @@ int main(int argc, char **argv) { } } while(next_option != -1); - if (pid_ok && (pid <= 1 || pid >= 65536)) { + if (pid_ok && (pid <= 1 || pid >= get_pid_max())) { fprintf(stderr,"Error: Invalid value for argument PID\n"); print_usage(stderr, 1); exit(1); |