SMP系统Linux下的进程绑定指定CPU

Linux大全评论350 views阅读模式

小注: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");
        }

企鹅博客
  • 本文由 发表于 2020年8月5日 02:34:25
  • 转载请务必保留本文链接:https://www.qieseo.com/172117.html

发表评论