Results 1 to 5 of 5
I want to implement my own thread library.
One step is asking the system how many cpus are available.
I'm using sched_getaffinity() to get this information. However I need some ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 09-10-2010 #1Just Joined!
- Join Date
- Sep 2010
- Posts
- 4
Implementing Linux Thread Library.
I want to implement my own thread library.
One step is asking the system how many cpus are available.
I'm using sched_getaffinity() to get this information. However I need some advice on unexpected output of the following short program I wrote.
Code://============================================================================ // Name : prcess.cpp // Author : //============================================================================ #include <sched.h> #include <assert.h> #include <stdio.h> #include <iostream> #include <errno.h> using namespace std; int main() { // [0,size) is the range of cpus the system should load balance on. size_t size=2; cpu_set_t *cpuset = CPU_ALLOC(size); /* FROM man 2 sched_setaffinity * After a call to sched_setaffinity(), the set of CPUs on which the process will actually run is the intersection of the set specified in the mask argument and the set of CPUs actually present on the system. The system may further restrict the set of CPUs on which the process runs if the "cpuset" mechanism described in cpuset(7) is being used. These restrictions on the actual set of CPUs on which the process will run are silently imposed by the kernel. * */ /* FROM man 3 cpu_set * Since CPU sets are bitsets allocated in units of long words, the actual number of CPUs in a dynamically allocated CPU set will be rounded up to the next multiple of sizeof(unsigned long). An application should con‐ sider the contents of these extra bits to be undefined. * */ if(sched_getaffinity(0, size*sizeof(unsigned long), cpuset)){ int err = errno; perror(""); switch(err){ case EFAULT: break; case EINVAL: break; case EPERM: break; case ESRCH: break; } return 0; } int count = CPU_COUNT(cpuset); cerr << "cpus:" << count << endl; for(int cpu=0; cpu<size || cpu<<count; cpu++){ if(CPU_ISSET(cpu,cpuset)){ cerr << "cpu is available:" << cpu <<endl; } } return 0; }
if size=2 then I get the output
if size=100 then the output is correct/expected since I just have one processor but it has two cores:Code:cpus:12 cpu is available:0 cpu is available:1 cpu is available:96 cpu is available:100 cpu is available:101 cpu is available:102 cpu is available:103 cpu is available:104 cpu is available:105 cpu is available:106 cpu is available:107 cpu is available:113
thanks for your advice in why these results happen,Code:cpus:2 cpu is available:0 cpu is available:1
Quincy Mitchell
- 09-16-2010 #2Just Joined!
- Join Date
- Sep 2010
- Posts
- 2
Hello Quincy,
I think it's a mere typo : you typed :
for(int cpu=0; cpu<size || cpu<<count; cpu++){
instead of :
for(int cpu=0; cpu<size || cpu<count; cpu++){
;-P
by the way I beleive the right way to count CPUs is :
sysconf(_SC_NPROCESSORS_ONLN)
have fun with thread !
- 09-16-2010 #3Just Joined!
- Join Date
- Sep 2010
- Posts
- 4
haha, oh boy. I think it was just a typo. I will look if that is true later.
I don't know how to start a process with the allowance of being able to only run on subset of processors but I think its possible. My function detects how many processors are available for the kernel to context switch on to for this specific process. The sysconf thing describes how many processors are available system wide. I think my function is better.
Am I right?
- 09-16-2010 #4Just Joined!
- Join Date
- Sep 2010
- Posts
- 4
i also should have called sched_setaffinity() instead of sched_getaffinity()
- 09-16-2010 #5Just Joined!
- Join Date
- Sep 2010
- Posts
- 4
actually my code still needs an upper bound (what 'size' should be set)
i think sysconf(_SC_NPROCESSORS_ONLN) is a good upper bound.


Reply With Quote
