#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h> /* sysconf */
#include <stdlib.h> /* exit */
#include <stdio.h>
int main(int argc, char *argv[])
{
int i, nrcpus, testpid;
cpu_set_t mask;
unsigned long bitmask = 0;
if (argc < 2) {
perror("argc too less");
exit(EXIT_FAILURE);
}
if((testpid = atoi(argv[1])) < 1) {
perror("argument[2]");
exit(EXIT_FAILURE);
}
nrcpus = sysconf(_SC_NPROCESSORS_CONF);
if (nrcpus > 0)
printf("---There are %d cpus online---\n", nrcpus);
CPU_ZERO(&mask);
CPU_SET(0, &mask); /* add CPU0 to cpu set */
CPU_SET(2, &mask); /* add CPU2 to cpu set */
/* Set the CPU affinity for a pid */
if (sched_setaffinity(testpid, sizeof(cpu_set_t), &mask) == -1)
{
perror("sched_setaffinity");
exit(EXIT_FAILURE);
}
CPU_ZERO(&mask);
/* Get the CPU affinity for a pid */
if (sched_getaffinity(testpid, sizeof(cpu_set_t), &mask) == -1)
{
perror("sched_getaffinity");
exit(EXIT_FAILURE);
}
/* get logical cpu number */
//nrcpus = sysconf(_SC_NPROCESSORS_CONF);
for (i = 0; i < nrcpus; i++)
{
if (CPU_ISSET(i, &mask))
{
bitmask |= (unsigned long)0x01 << i;
printf("processor #%d is set\n", i);
}
}
printf("bitmask = %#lx\n", bitmask);
exit(EXIT_SUCCESS);
}