1
votes

Basically the code below was intended for use on linux and maybe thats the reason I get the error because I'm using windows XP, but I figure that pthreads should work just as well on both machines. I'm using gcc as my compiler and I did link with -lpthread but I got the following error anyways.

|21|undefined reference to sched_setaffinity'| |30|undefined reference tosched_setaffinity'|

If there is another method to setting the thread affinity using pthreads (on windows) let me know. I already know all about the windows.h thread affinity functions available but I want to keep things multiplatform. thanks.

#include <stdio.h>
#include <math.h>
#include <sched.h>

double waste_time(long n)
{
    double res = 0;
    long i = 0;
    while(i <n * 200000)
    {
        i++;
        res += sqrt (i);
    }
    return res;
}
int main(int argc, char **argv)
{
    unsigned long mask = 1; /* processor 0 */

/* bind process to processor 0 */
    if (sched_setaffinity(0, sizeof(mask), &mask) <0)//line 21
    {
        perror("sched_setaffinity");
    }

/* waste some time so the work is visible with "top" */
    printf ("result: %f\n", waste_time (2000));

    mask = 2; /* process switches to processor 1 now */
    if (sched_setaffinity(0, sizeof(mask), &mask) <0)//line 30
    {
        perror("sched_setaffinity");
    }

/* waste some more time to see the processor switch */
    printf ("result: %f\n", waste_time (2000));
}
1

1 Answers

1
votes

sched_getaffinity() and sched_setaffinity() are strictly Linux-specific calls. Windows provides its own set of specific Win32 API calls that affect scheduling. See this answer for sample code for Windows.