Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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.

    // 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;
    		case EFAULT:
    		case EINVAL:
    		case EPERM:
    		case ESRCH:
    		return 0;
    	int count = CPU_COUNT(cpuset);
    	cerr << "cpus:" << count << endl;
    	for(int cpu=0; cpu<size || cpu<<count; cpu++){
    			cerr << "cpu is available:" << cpu <<endl;
    	return 0;

    if size=2 then I get the output
    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
    if size=100 then the output is correct/expected since I just have one processor but it has two cores:
    cpu is available:0
    cpu is available:1
    thanks for your advice in why these results happen,
    Quincy Mitchell

  2. #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++){
    by the way I beleive the right way to count CPUs is :

    have fun with thread !

  3. #3
    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?

  4. $spacer_open
  5. #4
    i also should have called sched_setaffinity() instead of sched_getaffinity()

  6. #5
    actually my code still needs an upper bound (what 'size' should be set)

    i think sysconf(_SC_NPROCESSORS_ONLN) is a good upper bound.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts