real time

#if 1
#include <string.h>
#include <limits.h>
#include <sys/mman.h>

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <unistd.h>

#define __USE_GNU
#include <sched.h>
#include <ctype.h>
#include <string.h>
#include <pthread.h>

#define THREAD_MAX_NUM 5 // 1个CPU内的最多进程数

int CPU_NUM = 0; // cpu中核数
int CPU = 5;     // CPU编号

void *threadFun(void *arg)
{

    int idx = 0;

    cpu_set_t affinity; //获取在集合中的CPU
    CPU_ZERO(&affinity);
    pthread_t thread = pthread_self();
    // 获取当前进程的CPU Affinity
    if (pthread_getaffinity_np(thread, sizeof(affinity), &affinity) == -1)
    {
        printf("warning: cound not get Process affinity, continuing...\n");
    }

    while (idx < 100000)
    {
        int i = 0;
        for (i = 0; i < CPU_NUM; i++)
        {
            if (CPU_ISSET(i, &affinity)) //判断线程与哪个CPU有亲和力
            {
                printf("thread=%d idx=%d  is running processor : %d\n", *((int *)arg), idx, i);
            }
        }
        idx++;
    }

    return NULL;
}

int main(int argc, char *argv[])
{
    // 获取核数
    CPU_NUM = sysconf(_SC_NPROCESSORS_CONF);
    printf("System has %i processor(s). \n", CPU_NUM);
    cpu_set_t mask; // CPU核的集合

    CPU_ZERO(&mask);
    // set CPU MASK
    CPU_SET(CPU, &mask);

    struct sched_param param;
    pthread_attr_t attr;

    int ret;

    int tid[THREAD_MAX_NUM];
    pthread_t thread[THREAD_MAX_NUM];

    /* Lock memory */
    if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
    {
        printf("mlockall failed: %m\n");
        exit(-2);
    }

    /* Initialize pthread attributes (default values) */
    ret = pthread_attr_init(&attr);
    if (ret)
    {
        printf("init pthread attributes failed\n");
        goto out;
    }

    /* Set a specific stack size  */
    printf("PTHREAD_STACK_MIN=%d\n", PTHREAD_STACK_MIN); // 16384
    ret = pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
    if (ret)
    {
        printf("pthread setstacksize failed\n");
        goto out;
    }

    /* Set scheduler policy and priority of pthread */
    ret = pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
    if (ret)
    {
        printf("pthread setschedpolicy failed\n");
        goto out;
    }

#if 0
    /* Create a pthread with specified attributes */
    ret = pthread_create(&thread, &attr, threadFun, NULL);
    if (ret)
    {
        printf("create pthread failed\n");
        goto out;
    }

    /* Join the thread and wait until it is done */
    ret = pthread_join(thread, NULL);
    if (ret)
    {
        printf("join pthread failed: %m\n");
    }

#endif

#if 1
    int i = 0;
    for (i = 0; i < THREAD_MAX_NUM; i++)
    {
        tid[i] = i;
        //param.sched_priority = 99; //[0,99]实时内核,值越大,优先级越高
         param.sched_priority = (i * 10 + 10);//[0,99]实时内核,值越大,优先级越高
        // param.sched_priority = 100-(i * 10 + 10);//[0,99]实时内核,值越大,优先级越高
        ret = pthread_attr_setschedparam(&attr, &param);
        if (ret)
        {
            printf("pthread setschedparam failed\n");
            goto out;
        }
        /* Use scheduling parameters of attr */
        ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
        if (ret)
        {
            printf("pthread setinheritsched failed\n");
            goto out;
        }
        ret = pthread_create(&thread[i], &attr, threadFun, &tid[i]);
        if (ret)
        {
            printf("create pthread failed\n");
            goto out;
        }
#if 1
        //设置当前进程的CPU Affinity

        if (pthread_setaffinity_np(thread[i], sizeof(mask), &mask) != 0)
        {
            printf("warning: could not set CPU affinity, continuing...\n");
        }
#endif
    }

    for (i = 0; i < THREAD_MAX_NUM; i++)
    {
        ret = pthread_join(thread[i], NULL);
        if (ret)
        {
            printf("join pthread failed: %m\n");
        }
    }

#endif

out:
    return ret;
}

#endif

#if 0
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <unistd.h>

#define __USE_GNU
#include <sched.h>
#include <ctype.h>
#include <string.h>
#include <pthread.h>

#define THREAD_MAX_NUM 1 // 1个CPU内的最多进程数

int CPU_NUM = 0; // cpu中核数
int CPU = 5;     // CPU编号

void *threadFun(void *arg)
{
    cpu_set_t affinity; //获取在集合中的CPU
    CPU_ZERO(&affinity);
    pthread_t thread = pthread_self();
    // 获取当前进程的CPU Affinity
    if (pthread_getaffinity_np(thread, sizeof(affinity), &affinity) == -1)
    {
        printf("warning: cound not get Process affinity, continuing...\n");
    }

while(1)
{
    int i = 0;
    for (i = 0; i < CPU_NUM; i++)
    {
        if (CPU_ISSET(i, &affinity)) //判断线程与哪个CPU有亲和力
        {
            printf("this thread %d is running processor : %d\n", *((int *)arg), i);
        }
    }

}


    return NULL;
}

int main(int argc, char *argv[])
{
    int tid[THREAD_MAX_NUM];
    pthread_t thread[THREAD_MAX_NUM];
    // 获取核数
    CPU_NUM = sysconf(_SC_NPROCESSORS_CONF);
    printf("System has %i processor(s). \n", CPU_NUM);
    cpu_set_t mask; // CPU核的集合

    CPU_ZERO(&mask);
    // set CPU MASK
    CPU_SET(CPU, &mask);

    int i = 0;
    for (i = 0; i < THREAD_MAX_NUM; i++)
    {
        tid[i] = i;
        pthread_create(&thread[i], NULL, threadFun, &tid[i]);
        //设置当前进程的CPU Affinity

#if 1
        if (pthread_setaffinity_np(thread[i], sizeof(mask), &mask) != 0)
        {
            printf("warning: could not set CPU affinity, continuing...\n");
        }
#endif
    }

    for (i = 0; i < THREAD_MAX_NUM; i++)
    {
        pthread_join(thread[i], NULL);
    }

    return 0;
}

#endif

#if 0
#include <stdio.h>
#include <stdlib.h>
#define __USE_GNU
#include <sched.h>
#include <pthread.h>

void thread1(void)
{
    printf("this is thread1\n");
    while (1)
        ;
}

void thread2(void)
{
    printf("this is thread2\n");
    while (1)
        ;
}
int main(void)
{
    pthread_t id1, id2;
    int ret;
    cpu_set_t cpuset;

    ret = pthread_create(&id1, NULL, (void *)thread1, NULL);
    if (ret != 0)
    {
        printf("create thread err!\n");
        exit(1);
    }

    ret = pthread_create(&id2, NULL, (void *)thread2, NULL);
    if (ret != 0)
    {
        printf("create thread err!\n");
        exit(1);
    }
    CPU_ZERO(&cpuset);
    CPU_SET(0, &cpuset);

    if (pthread_setaffinity_np(id1, sizeof(cpu_set_t), &cpuset) != 0)
    {
        printf("warning: could not set CPU affinity, continuing...\n");
    }

    CPU_ZERO(&cpuset);
    CPU_SET(1, &cpuset);

    if (pthread_setaffinity_np(id2, sizeof(cpu_set_t), &cpuset) != 0)
    {
        printf("warning: could not set CPU affinity, continuing...\n");
    }



    if (pthread_getaffinity_np(id1, sizeof(cpu_set_t), &cpuset) == -1)
    {
        printf("warning: cound not get Process affinity, continuing...\n");
    }
    printf("thread1 affinity:%x\n", cpuset);

    if (pthread_getaffinity_np(id2, sizeof(cpu_set_t), &cpuset) == -1)
    {
        printf("warning: cound not get Process affinity, continuing...\n");
    }
    printf("thread2 affinity:%x\n", cpuset);


    printf("id1=%lu\n", id1);
    printf("id2=%lu\n", id2);



    while (1)
        ;
}
#endif

#if 0

#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define handle_error_en(en, msg) \
    do                           \
    {                            \
        errno = en;              \
        perror(msg);             \
        exit(EXIT_FAILURE);      \
    } while (0)

int
main(int argc, char *argv[])
{
    int s, j;
    cpu_set_t cpuset;
    pthread_t thread;

    thread = pthread_self();

    /* Set affinity mask to include CPUs 0 to 7 */

    CPU_ZERO(&cpuset);
    for (j = 0; j < 8; j++)
        CPU_SET(j, &cpuset);

    s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
    if (s != 0)
        handle_error_en(s, "pthread_setaffinity_np");

    /* Check the actual affinity mask assigned to the thread */

    s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
    if (s != 0)
        handle_error_en(s, "pthread_getaffinity_np");

    printf("Set returned by pthread_getaffinity_np() contained:\n");
    for (j = 0; j < CPU_SETSIZE; j++)
        if (CPU_ISSET(j, &cpuset))
            printf("    CPU %d\n", j);

    exit(EXIT_SUCCESS);
}

#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值