linux线程设置cpu亲和性与调度方式及优先级

cpus_set.c


#include "cpus_set.h"

int get_cpu_nums()
{
    return sysconf(_SC_NPROCESSORS_CONF);
}
int set_cpus_mask(int* cpus_arr, int nums, cpu_set_t *mask)
{
    CPU_ZERO(mask);
    if (nums > get_cpu_nums())
    {
        printf("%s:%d nums must less then cpu nums total\n", __FUNCTION__, __LINE__);
        return -1;
    }

    for (int i = 0; i < nums; i++)
    {
        if (cpus_arr[i] > (get_cpu_nums() - 1) || cpus_arr[i] < 0)
        {
            printf("cpus_arr[%d]: %d\n",i,cpus_arr[i]);
            printf("%s:%d cpus_arr error\n", __FUNCTION__, __LINE__);
            return -1;
        }
        CPU_SET(cpus_arr[i], mask); //将CPU号加入到mask集合
    }
    //printf("set_cpus_mask success\n");
    return 0;
}
int del_cpu_mask(int cpu_num, cpu_set_t *mask)
{
    if (!CPU_ISSET(cpu_num, mask))
    {
        printf("%s:%d cpu_num does not exist in mask\n", __FUNCTION__, __LINE__);
        return -1;
    }
    CPU_CLR(cpu_num, mask);
    return 0;
}
int bind_pthread_cpu_cores(cpu_set_t *mask, pthread_t pid)
{
    int ret = pthread_setaffinity_np(pid, sizeof(cpu_set_t), mask); /* 设置cpu 亲和性(affinity)*/
    if (ret < 0)
    {
        printf("%s:%d:", __FUNCTION__, __LINE__);
        perror("faild sched_setaffinity");
        return -1;
    }
    //printf("bind_cpu_cores success\n");
    return 0;
}
int bind_fork_cpu_cores(cpu_set_t *mask)
{

    int ret = sched_setaffinity(0, sizeof(cpu_set_t), mask);
    if (ret < 0)
    {
        printf("%s:%d:", __FUNCTION__, __LINE__);
        perror("faild sched_setaffinity");
        return -1;
    }
    //printf("bind_cpu_cores success\n");
    return 0;
}
void cpu_get(char* thread_name)
{
     usleep(10000);
    {
        int ret;
        cpu_set_t get_cpus;
        CPU_ZERO(&get_cpus);
        ret = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &get_cpus);
        if (ret < 0)
        {
            perror("faild sched_getaffinity");
        }
        printf("%s [%ld] is running in processor:",thread_name, pthread_self());
        for (int i = 0; i < 4; i++)
        {
            if (CPU_ISSET(i, &get_cpus))
            {
                printf("%d ", i);
            }
        }
        printf("\n");
    }
}
int create_pthread(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*start_routine)(void *), void *arg, int* cpus_arr,int nums)
{
    int ret;
    cpu_set_t mask;
    ret = set_cpus_mask(cpus_arr, nums, &mask);
    if (ret < 0)
    {
        printf("set_cpus_mask failed\n");
        return -1;
    }
    ret = pthread_create(thread, NULL, start_routine, arg);
    if (ret < 0)
    {
        printf("pthread_create failed\n");
        return -1;
    }
    ret = bind_pthread_cpu_cores(&mask, *thread);
    if (ret < 0)
    {
        printf("bind_pthread_cpu_cores failed\n");
        return -1;
    }
    
    usleep(100000);
}

int pthread_attribute_init(pthread_attr_t *attr){
    return pthread_attr_init(attr);
}
int pthread_attribute_destory(pthread_attr_t *attr){
    return pthread_attr_destroy(&attr);
}
int pthread_set_inheritsched(pthread_attr_t *attr,int __inherit){
    return pthread_attr_setinheritsched(attr, __inherit);
}
int pthread_set_schedpolicy(pthread_attr_t *attr,int policy){
    pthread_attr_setschedpolicy(attr, policy);
}

cpus_set.h


#ifndef __CPUS_SET_H__
#define __CPUS_SET_H__

#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sched.h>
#include <stdlib.h>

/*
    return : Number of CPU cores
*/
extern int get_cpu_num();
/*
    
*/
extern int set_cpus_mask(int *cpus_arr, int nums,cpu_set_t* mask);

extern int del_cpu_mask(int cpu_num,cpu_set_t* mask);

extern int bind_cpu_cores(cpu_set_t* mask,pthread_t pid);

extern int create_pthread(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*start_routine)(void *), void *arg, int *cpus_arr,int nunms);

#endif

main

#define _GNU_SOURCE

#include "cpus_set.h"
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sched.h>
#include <stdlib.h>
void printf_str(char *str)
{
    printf("%s", str);
}
void *thread_run1()
{
    usleep(10000);
    {
        int ret;
        cpu_set_t get_cpus;
        CPU_ZERO(&get_cpus);
        ret = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &get_cpus);
        if (ret < 0)
        {
            perror("faild sched_getaffinity");
        }
        printf("pthread_run1[%ld] is running in processor:", pthread_self());
        for (int i = 0; i < 4; i++)
        {
            if (CPU_ISSET(i, &get_cpus))
            {
                printf("%d ", i);
            }
        }
        printf("\n");
    }
    int policy;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    struct sched_param param;
    pthread_getschedparam(pthread_self(), &policy, &param);
    while (1)
    {
        //usleep(10000);
        //printf("pthtrad_run1 on cpu[%d] policy = %d ,param.__sched_priority = %d!\n", sched_getcpu(),policy,param.__sched_priority );
        printf_str("thread_run1 is running!\n");
    }
    pthread_exit(NULL);
}
void *thread_run2()
{
    usleep(10000);
    {
        int ret;
        cpu_set_t get_cpus;
        CPU_ZERO(&get_cpus);
        ret = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &get_cpus);
        if (ret < 0)
        {
            perror("faild sched_getaffinity");
        }
        printf("pthread_run2[%ld] is running in processor:", pthread_self());
        for (int i = 0; i < 4; i++)
        {
            if (CPU_ISSET(i, &get_cpus))
            {
                printf("%d ", i);
            }
        }
        printf("\n");
    }
    int policy;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    struct sched_param param;
    pthread_getschedparam(pthread_self(), &policy, &param);
    while (1)
    {
        //usleep(10000);
        //printf("pthtrad_run2 on cpu[%d] policy = %d ,param.__sched_priority = %d!\n", sched_getcpu(),policy,param.__sched_priority );
        printf_str("thread_run2 is running!\n");
    }
    pthread_exit(NULL);
}
void *thread_run3()
{
   
    int policy;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    struct sched_param param;
    pthread_getschedparam(pthread_self(), &policy, &param);
    while (1)
    {
        //usleep(10000);
        //printf("pthtrad_run3 on cpu[%d] policy = %d ,param.__sched_priority = %d!\n", sched_getcpu(),policy,param.__sched_priority );
        printf_str("thread_run3 is running!\n");
    }
    pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
    pthread_t th1, th2, th3;

    pthread_attr_t attr;
    struct sched_param sched;
    int ret;
    int cpu_arr1[1] = {0};
    cpu_set_t mask1;
    sched.__sched_priority = 99;
    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); //要使优先级其作用必须要有这句话
    pthread_attr_setschedpolicy(&attr, SCHED_RR);                //改变线程调度策略
    pthread_attr_setschedparam(&attr, &sched);                   //设置线程优先级
    ret = create_pthread(&th1, &attr, thread_run1, NULL, cpu_arr1,sizeof(cpu_arr1)/sizeof(cpu_arr1[0]));
    if (ret < 0)
    {
        printf("%s:%d:create_pthread1 fail\n", __FUNCTION__, __LINE__);
    }
    pthread_attr_destroy(&attr);

    int cpu_arr2[1] = {0};
    cpu_set_t mask2;
    sched.__sched_priority = 50;
    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); //要使优先级其作用必须要有这句话
    pthread_attr_setschedpolicy(&attr, SCHED_RR);                //改变线程调度策略
    pthread_attr_setschedparam(&attr, &sched);                   //设置线程优先级
    ret = create_pthread(&th2, &attr, thread_run2, NULL, cpu_arr2,sizeof(cpu_arr2)/sizeof(cpu_arr2[0]));
    if (ret < 0)
    {
        printf("%s:%d:create_pthread2 fail\n", __FUNCTION__, __LINE__);
    }
    pthread_attr_destroy(&attr);

    int cpu_arr3[1] = {0};
    cpu_set_t mask3;
    sched.__sched_priority = 1;
    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); //要使优先级其作用必须要有这句话
    pthread_attr_setschedpolicy(&attr, SCHED_RR);                //改变线程调度策略
    pthread_attr_setschedparam(&attr, &sched);                   //设置线程优先级
    ret = create_pthread(&th3, &attr, thread_run3, NULL, cpu_arr3,sizeof(cpu_arr3)/sizeof(cpu_arr3[0]));
    if (ret < 0)
    {
        printf("%s:%d:create_pthread3 fail\n", __FUNCTION__, __LINE__);
    }
    pthread_attr_destroy(&attr);

    pthread_join(th1, NULL);
    pthread_join(th2, NULL);
    pthread_join(th3, NULL);

    return 0;
}
#if 0
#define _GNU_SOURCE

#include "cpus_set.h"
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sched.h>
#include <stdlib.h>
void printf_str(char *str)
{
    printf("%s", str);
}
void *thread_run1()
{
    usleep(10000);
    {
        int ret;
        cpu_set_t get_cpus;
        CPU_ZERO(&get_cpus);
        ret = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &get_cpus);
        if (ret < 0)
        {
            perror("faild sched_getaffinity");
        }
        printf("pthread_run1[%ld] is running in processor:", pthread_self());
        for (int i = 0; i < 4; i++)
        {
            if (CPU_ISSET(i, &get_cpus))
            {
                printf("%d ", i);
            }
        }
        printf("\n");
    }
    int policy;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    struct sched_param param;
    pthread_getschedparam(pthread_self(), &policy, &param);
    while (1)
    {
        //usleep(10000);
        //printf("pthtrad_run1 on cpu[%d] policy = %d ,param.__sched_priority = %d!\n", sched_getcpu(),policy,param.__sched_priority );
        printf_str("thread_run1 is running!\n");
    }
    pthread_exit(NULL);
}
void *thread_run2()
{
    usleep(10000);
    {
        int ret;
        cpu_set_t get_cpus;
        CPU_ZERO(&get_cpus);
        ret = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &get_cpus);
        if (ret < 0)
        {
            perror("faild sched_getaffinity");
        }
        printf("pthread_run2[%ld] is running in processor:", pthread_self());
        for (int i = 0; i < 4; i++)
        {
            if (CPU_ISSET(i, &get_cpus))
            {
                printf("%d ", i);
            }
        }
        printf("\n");
    }
    int policy;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    struct sched_param param;
    pthread_getschedparam(pthread_self(), &policy, &param);
    while (1)
    {
        //usleep(10000);
        //printf("pthtrad_run2 on cpu[%d] policy = %d ,param.__sched_priority = %d!\n", sched_getcpu(),policy,param.__sched_priority );
        printf_str("thread_run2 is running!\n");
    }
    pthread_exit(NULL);
}
void *thread_run3()
{
   
    int policy;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    struct sched_param param;
    pthread_getschedparam(pthread_self(), &policy, &param);
    while (1)
    {
        //usleep(10000);
        //printf("pthtrad_run3 on cpu[%d] policy = %d ,param.__sched_priority = %d!\n", sched_getcpu(),policy,param.__sched_priority );
        printf_str("thread_run3 is running!\n");
    }
    pthread_exit(NULL);
}
int main(int argc, char const *argv[])
{
    pthread_t th1, th2, th3;

    pthread_attr_t attr;
    struct sched_param sched;
    int ret;
    int cpu_arr1[1] = {0};
    cpu_set_t mask1;
    sched.__sched_priority = 99;
    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); //要使优先级其作用必须要有这句话
    pthread_attr_setschedpolicy(&attr, SCHED_RR);                //改变线程调度策略
    pthread_attr_setschedparam(&attr, &sched);                   //设置线程优先级
    ret = create_pthread(&th1, &attr, thread_run1, NULL, cpu_arr1,sizeof(cpu_arr1)/sizeof(cpu_arr1[0]));
    if (ret < 0)
    {
        printf("%s:%d:create_pthread1 fail\n", __FUNCTION__, __LINE__);
    }
    pthread_attr_destroy(&attr);

    int cpu_arr2[1] = {0};
    cpu_set_t mask2;
    sched.__sched_priority = 50;
    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); //要使优先级其作用必须要有这句话
    pthread_attr_setschedpolicy(&attr, SCHED_RR);                //改变线程调度策略
    pthread_attr_setschedparam(&attr, &sched);                   //设置线程优先级
    ret = create_pthread(&th2, &attr, thread_run2, NULL, cpu_arr2,sizeof(cpu_arr2)/sizeof(cpu_arr2[0]));
    if (ret < 0)
    {
        printf("%s:%d:create_pthread2 fail\n", __FUNCTION__, __LINE__);
    }
    pthread_attr_destroy(&attr);

    int cpu_arr3[1] = {0};
    cpu_set_t mask3;
    sched.__sched_priority = 1;
    pthread_attr_init(&attr);
    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); //要使优先级其作用必须要有这句话
    pthread_attr_setschedpolicy(&attr, SCHED_RR);                //改变线程调度策略
    pthread_attr_setschedparam(&attr, &sched);                   //设置线程优先级
    ret = create_pthread(&th3, &attr, thread_run3, NULL, cpu_arr3,sizeof(cpu_arr3)/sizeof(cpu_arr3[0]));
    if (ret < 0)
    {
        printf("%s:%d:create_pthread3 fail\n", __FUNCTION__, __LINE__);
    }
    pthread_attr_destroy(&attr);

    pthread_join(th1, NULL);
    pthread_join(th2, NULL);
    pthread_join(th3, NULL);

    return 0;
}

#endif 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值