SMP系统Linux下的进程绑定指定CPU
小注:SMP系统Linux下的进程绑定指定CPU
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/sysinfo.h>
#include<unistd.h>
#define __USE_GNU
#include<sched.h>
#include<ctype.h>
#include<string.h>
int main(int argc, char* argv[])
{
int num = sysconf(_SC_NPROCESSORS_CONF);
pid_t pid;
int status;
int myid;
cpu_set_t mask;
//cpu_set_t get;
char * strv[ ]={"insmod","/root/linux-bcm-core.ko","init=all",(char *)0};
char * strp[ ]={"PATH=/sbin",0};
if (argc != 2)
{
printf("usage : ./cpu num\n");
exit(1);
}
pid = fork();
if(pid < 0)
{
perror("Process creation failed\n");
exit(1);
}
else if(0 == pid)
{
printf("child process is running\n");
printf("My pid = %d ,parentpid = %d\n",getpid(),getppid());
myid = atoi(argv[1]);
printf("system has %i processor(s). \n", num);
CPU_ZERO(&mask);
CPU_SET(myid, &mask);
if (sched_setaffinity(getpid(), sizeof(mask), &mask) == -1)
{
printf("warning: could not set CPU affinity, continuing...\n");
}
//execl("/sbin/insmod","insmod","/root/linux-bcm-core.ko","init=all",(char * )0);
//system("/sbin/insmod /root/linux-bcm-core.ko init=all");
execve("/sbin/insmod",strv,strp);
exit(0);
}
else
{
printf("Parent process is runnig\n");
}
wait(&status);
exit(0);
}
关键名词CPU亲合力