Back to index

glibc  2.9
tst-getcpu.c
Go to the documentation of this file.
00001 #include <errno.h>
00002 #include <stdio.h>
00003 #include <sched.h>
00004 #include <unistd.h>
00005 
00006 
00007 static int
00008 do_test (void)
00009 {
00010   cpu_set_t cs;
00011   if (sched_getaffinity (getpid (), sizeof (cs), &cs) != 0)
00012     {
00013       printf ("getaffinity failed: %m\n");
00014       return 1;
00015     }
00016 
00017   int result = 0;
00018   int cpu = 0;
00019   while (CPU_COUNT (&cs) != 0)
00020     {
00021       if (CPU_ISSET (cpu, &cs))
00022        {
00023          cpu_set_t cs2;
00024          CPU_ZERO (&cs2);
00025          CPU_SET (cpu, &cs2);
00026          if (sched_setaffinity (getpid (), sizeof (cs2), &cs2) != 0)
00027            {
00028              printf ("setaffinity(%d) failed: %m\n", cpu);
00029              result = 1;
00030            }
00031          else
00032            {
00033              int cpu2 = sched_getcpu ();
00034              if (cpu2 == -1 && errno == ENOSYS)
00035               {
00036                 puts ("getcpu syscall not implemented");
00037                 return 0;
00038               }
00039              if (cpu2 != cpu)
00040               {
00041                 printf ("getcpu results %d not possible\n", cpu2);
00042                 result = 1;
00043               }
00044            }
00045          CPU_CLR (cpu, &cs);
00046        }
00047       ++cpu;
00048     }
00049 
00050   return result;
00051 }
00052 
00053 #define TEST_FUNCTION do_test ()
00054 #include <test-skeleton.c>