posix线程的优先级测试

2023-05-16

测试的时候,如果创建的线程不够多,有些问题体现不出来,例如pthread_cond_signal和pthread_cond_broadcast。

奇怪的优化是不会有好结果的。

优先级打印:

测试目的:输出三种调度模式下的最大优先级和最小优先级

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <pthread.h>
#include <sys/select.h>
#include <sys/epoll.h>
#include <errno.h>
#include <sys/wait.h>
#include <netinet/tcp.h>

#define DEBUG_INFO(format, ...) printf("%s:%d -- " format "\n", __func__, __LINE__,##__VA_ARGS__)


int main(){
    int min;
    int max;
    min = sched_get_priority_min(SCHED_OTHER);
    max = sched_get_priority_max(SCHED_OTHER);
    DEBUG_INFO("SCHED_OTHER min = %d,max = %d",min,max);

    min = sched_get_priority_min(SCHED_RR);
    max = sched_get_priority_max(SCHED_RR);
    DEBUG_INFO("SCHED_RR min = %d,max = %d",min,max);

    min = sched_get_priority_min(SCHED_FIFO);
    max = sched_get_priority_max(SCHED_FIFO);
    DEBUG_INFO("SCHED_FIFO min = %d,max = %d",min,max);


    return 0;
}

执行结果:

main:25 -- SCHED_OTHER min = 0,max = 0
main:29 -- SCHED_OTHER min = 1,max = 99
main:33 -- SCHED_OTHER min = 1,max = 99

测试一:先创建10个线程。

使用默认属性。

#include <stdio.h>
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
#include <signal.h>
#include <map>

#define DEBUG_INFO(format, ...) printf("%s:%d -- " format "\n", __func__, __LINE__,##__VA_ARGS__)

std::map<pthread_t ,int> thread_index_map;

struct mystruct{
    int index;
    pthread_t thread;

};

void clean_up_resource(void *arg){
    struct mystruct *pms = (struct mystruct *)arg;
    DEBUG_INFO("delete thread index = %d\n", pms->index);
    free(arg);
}

void *func(void *arg){
    struct mystruct *pms = (struct mystruct *)arg;
    static pthread_t thread_id = pthread_self();
    pthread_cleanup_push(clean_up_resource,arg);
    
    while(1){
        for(int i = 0;i < 10000000;i++){

        }
        break;
    }
    DEBUG_INFO("index = %d",pms->index);
    return 0;
    pthread_cleanup_pop(0);
}

int main(int argc, char**){
    for(int i=0; i < 10; i++){
        pthread_t t;
        struct mystruct *pms = (struct mystruct*)malloc(sizeof(struct mystruct));
        pms->index = i;
        int ret = pthread_create(&t, NULL, func,pms);
        if(ret != 0){
            perror("pthread_create");
        }
    }
    while(1){
        sleep(1);
    }
}

 执行结果:

main:44 -- pid = 39495
func:37 -- index = 3
func:37 -- index = 8
func:37 -- index = 0
func:37 -- index = 4
func:37 -- index = 6
func:37 -- index = 9
func:37 -- index = 2
func:37 -- index = 7
func:37 -- index = 5
func:37 -- index = 1

pstree -p命令的执行截图

 上面我们看到输出的index的值,不是倒叙也不是正序,而是乱序的,现在,我们需要设置线程的优先级,让他们有序的运行,如何操作呢。

测试二:线程的清理

捕获信号SIGINT,在SIGINT信号处理函数中,结束10个线程。然后

#include <stdio.h>
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
#include <signal.h>
#include <map>
#include <list>

#define DEBUG_INFO(format, ...) printf("%s:%d -- " format "\n", __func__, __LINE__,##__VA_ARGS__)

std::map<pthread_t ,int> thread_index_map;
typedef void (*sighandler)(int signo);
struct mystruct{
    int index;
    pthread_t thread;

};

void clean_up_resource(void *arg){
    struct mystruct *pms = (struct mystruct *)arg;
    DEBUG_INFO("delete thread index = %d\n", pms->index);
    free(arg);
}
std::list<pthread_t> pthread_list;
void *func(void *arg){
    pthread_list.push_back(pthread_self());
    struct mystruct *pms = (struct mystruct *)arg;
    static pthread_t thread_id = pthread_self();
    pthread_cleanup_push(clean_up_resource,arg);
    
    while(1){
        for(int i = 0;i < 10000000;i++){

        }
        break;
    }
    DEBUG_INFO("index = %d",pms->index);
    while(1)sleep(1);
    return 0;
    pthread_cleanup_pop(0);
}



void sig_func(int signo){
    auto it = pthread_list.begin();
    DEBUG_INFO("%lu",pthread_list.size());
    for(;it != pthread_list.end();it++){
        pthread_cancel(*it);
    }
    DEBUG_INFO("good bye");
    sleep(2);
    exit(0);
}

int main(int argc, char**){
    sighandler sig_ret;
    sig_ret = signal(SIGINT, sig_func);
    if(sig_ret == SIG_ERR){
        perror("signal");
        exit(-1);
    }
    DEBUG_INFO("pid = %u",getpid());
    for(int i=0; i < 10; i++){
        pthread_t t;
        struct mystruct *pms = (struct mystruct*)malloc(sizeof(struct mystruct));
        pms->index = i;
        int ret = pthread_create(&t, NULL, func,pms);
        if(ret != 0){
            perror("pthread_create");
        }
    }
    while(1){
        sleep(1);
    }
    return 0;
}

执行结果:

main:65 -- pid = 50827
func:39 -- index = 1
func:39 -- index = 4
func:39 -- index = 2
func:39 -- index = 3
func:39 -- index = 0
func:39 -- index = 5
func:39 -- index = 6
func:39 -- index = 8
func:39 -- index = 7
func:39 -- index = 9
^Csig_func:49 -- 10
sig_func:53 -- good bye
clean_up_resource:23 -- delete thread index = 0

clean_up_resource:23 -- delete thread index = 4

clean_up_resource:23 -- delete thread index = 1

clean_up_resource:23 -- delete thread index = 3

clean_up_resource:23 -- delete thread index = 5

clean_up_resource:23 -- delete thread index = 8

clean_up_resource:23 -- delete thread index = 9

clean_up_resource:23 -- delete thread index = 2

clean_up_resource:23 -- delete thread index = 7

clean_up_resource:23 -- delete thread index = 6

按下CTRL+C后,sig_func被执行,开始遍历删除链表中的线程。最后退出进程。为了让删除动作成功完成,执行exit(0)之前先休眠2秒。

 线程的属性

pthread_attr_init函数

NAME
       pthread_attr_init, pthread_attr_destroy - initialize and destroy thread attributes object

SYNOPSIS
       #include <pthread.h>

       int pthread_attr_init(pthread_attr_t *attr);
       int pthread_attr_destroy(pthread_attr_t *attr);

       Compile and link with -pthread.

pthread_getattr_np函数 

NAME
       pthread_getattr_np - get attributes of created thread

SYNOPSIS
       #define _GNU_SOURCE             /* See feature_test_macros(7) */
       #include <pthread.h>

       int pthread_getattr_np(pthread_t thread, pthread_attr_t *attr);

       Compile and link with -pthread.

 pthread_attr_t结构体

 pthread_attr_init函数

NAME
       pthread_attr_init, pthread_attr_destroy - initialize and destroy thread attributes object

SYNOPSIS
       #include <pthread.h>

       int pthread_attr_init(pthread_attr_t *attr);
       int pthread_attr_destroy(pthread_attr_t *attr);

       Compile and link with -pthread.

 线程的优先级是经常设置的属性,由两个函数进行控制:pthread_attr_getschedparam()函数获得线程的优先级设置;函数pthread_attr_setschedparam()设置线程的优先级。

pthread_attr_setschedparam函数 

NAME
       pthread_attr_setschedparam, pthread_attr_getschedparam - set/get scheduling parameter attributes in thread attributes object

SYNOPSIS
       #include <pthread.h>

       int pthread_attr_setschedparam(pthread_attr_t *attr,
                                      const struct sched_param *param);
       int pthread_attr_getschedparam(const pthread_attr_t *attr,
                                      struct sched_param *param);

       Compile and link with -pthread.

结构体struct sched_param

struct sched_param {
               int sched_priority;     /* Scheduling priority */
           };

pthread_testcancel函数 

NAME
       pthread_testcancel - request delivery of any pending cancellation request

SYNOPSIS
       #include <pthread.h>

       void pthread_testcancel(void);

       Compile and link with -pthread.

schedpolicy:这个属性控制线程的调度方式。它的取值可以是SCHED_OTHER、SCHED_RP和SCHED_FIFO。这个属性的默认值为SCHED_OTHER。另外两种调度方式只能用于以超级用户权限运行的进程,因为它们都具备实时调度的功能,但在行为上略有区别。SCHED_RP使用循环(round-robin)调度机制,而SCHED_FIFO使用“先进先出”策略。

测试三:优先级测试

#ifndef _GNU_SOURCE
#define _GNU_SOURCE     /* To get pthread_getattr_np() declaration */
#endif
#define _REENTRANT
#include <stdio.h>
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
#include <signal.h>
#include <map>
#include <list>
#include <sched.h>
#include <atomic>
using namespace std;

#define DEBUG_INFO(format, ...) printf("%s:%d -- " format "\n", __func__, __LINE__,##__VA_ARGS__)

std::map<pthread_t ,int> thread_index_map;
typedef void (*sighandler)(int signo);
struct mystruct{
    int index;
    pthread_t thread;
    pthread_attr_t attr;
    struct sched_param sched;


};

void clean_up_resource(void *arg){
    struct mystruct *pms = (struct mystruct *)arg;
    DEBUG_INFO("delete thread index = %d\n", pms->index);
    pthread_attr_destroy(&pms->attr);
    free(arg);
}
std::list<pthread_t> pthread_list;
// std::atomic_bool start = false;
atomic<long> start(0);
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *func(void *arg){
    pthread_list.push_back(pthread_self());
    struct mystruct *pms = (struct mystruct *)arg;
    static pthread_t thread_id = pthread_self();
    pthread_cleanup_push(clean_up_resource,arg);

    // pthread_mutex_lock(&mtx);
    // while(start == 0){
    //     pthread_cond_wait(&cond,&mtx);
    // }
    // pthread_mutex_unlock(&mtx);
    int ret = pthread_setschedparam(pthread_self(), SCHED_FIFO, &pms->sched);
    if (ret != 0) {
        perror("pthread_setschedparam");
        exit(-1);
    }
    while(start == 0){}
    DEBUG_INFO("start thread = %d",pms->index);
    
    while(1){
        for(int i = 0;i < 100;i++){
            pthread_testcancel();
            for(int j = 0;j < 100;j++){usleep(1);}
        }
        break;
    }
    DEBUG_INFO("index = %d",pms->index);
    while(1)sleep(1);
    return 0;
    pthread_cleanup_pop(0);
}

void sig_func(int signo){
    auto it = pthread_list.begin();
    DEBUG_INFO("%lu",pthread_list.size());
    for(;it != pthread_list.end();it++){
        
        pthread_cancel(*it);
    }
    DEBUG_INFO("good bye");
    sleep(2);
    exit(0);
}

int main(int argc, char**){
    sighandler sig_ret;
    sig_ret = signal(SIGINT, sig_func);
    if(sig_ret == SIG_ERR){
        perror("signal");
        exit(-1);
    }
    DEBUG_INFO("pid = %u",getpid());
    for(int i=0; i < 10; i++){
        pthread_t t;
        struct mystruct *pms = (struct mystruct*)malloc(sizeof(struct mystruct));
        if(!pms){
            perror("malloc");
            raise(SIGINT);
            while(1)sleep(1);
        }
        pms->index = i;       

        pthread_attr_init(&pms->attr);
        pthread_attr_getschedparam(&pms->attr,&pms->sched);
        DEBUG_INFO("pms->sched.sched_priority = %d",pms->sched.sched_priority);
        pthread_attr_setschedpolicy(&pms->attr,SCHED_FIFO);//SCHED_OTHER);//SCHED_RP);//SCHED_FIFO);
        pms->sched.sched_priority = (i + 1)*5;
        pthread_attr_setschedparam(&pms->attr,&pms->sched);

        pthread_attr_getschedparam(&pms->attr,&pms->sched);
        DEBUG_INFO("pms->sched.sched_priority = %d",pms->sched.sched_priority);

        
        int ret = pthread_create(&t, NULL, func,pms);
        if(ret != 0){
            perror("malloc");
            raise(SIGINT);
            free(pms);            
            
        }
    }
    start = 1;
    //pthread_cond_signal(&cond);
    pthread_cond_broadcast(&cond);
    while(1){
        sleep(1);
    }
    return 0;
}

main:93 -- pid = 87681
main:106 -- pms->sched.sched_priority = 0
main:112 -- pms->sched.sched_priority = 5
main:106 -- pms->sched.sched_priority = 0
main:112 -- pms->sched.sched_priority = 10
main:106 -- pms->sched.sched_priority = 0
main:112 -- pms->sched.sched_priority = 15
main:106 -- pms->sched.sched_priority = 0
main:112 -- pms->sched.sched_priority = 20
main:106 -- pms->sched.sched_priority = 0
main:112 -- pms->sched.sched_priority = 25
main:106 -- pms->sched.sched_priority = 0
main:112 -- pms->sched.sched_priority = 30
main:106 -- pms->sched.sched_priority = 0
main:112 -- pms->sched.sched_priority = 35
main:106 -- pms->sched.sched_priority = 0
main:112 -- pms->sched.sched_priority = 40
main:106 -- pms->sched.sched_priority = 0
main:112 -- pms->sched.sched_priority = 45
main:106 -- pms->sched.sched_priority = 0
main:112 -- pms->sched.sched_priority = 50
func:59 -- start thread = 4
func:59 -- start thread = 0
func:59 -- start thread = 1
func:59 -- start thread = 3
func:59 -- start thread = 6
func:59 -- start thread = 5
func:59 -- start thread = 7
func:59 -- start thread = 9
func:59 -- start thread = 2
func:59 -- start thread = 8
func:68 -- index = 9
func:68 -- index = 6
func:68 -- index = 7
func:68 -- index = 8
func:68 -- index = 4
func:68 -- index = 2
func:68 -- index = 3
func:68 -- index = 5
func:68 -- index = 0
func:68 -- index = 1

 线程9优先级最高,所以先执行完成,线程0优先级最低,倒数第二完成。只能说整体上能看出来是优先级的趋势是对的了。因为是多核的关系吗。所以不是纯正的降序。

在单核的ARM板上运行,如下所示,没有达到我心中的降序的效果啊。4都最先完成了。但是每次执行完成的数序确实固定的,这...

func:68 -- index = 4
func:68 -- index = 5
func:68 -- index = 6
func:68 -- index = 7
func:68 -- index = 3
func:68 -- index = 2
func:68 -- index = 8
func:68 -- index = 9
func:68 -- index = 1
func:68 -- index = 0

 测试五:优化后的代码

#ifndef _GNU_SOURCE
#define _GNU_SOURCE     /* To get pthread_getattr_np() declaration */
#endif
#ifndef _REENTRANT
#define _REENTRANT
#endif
#include <stdio.h>
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
#include <signal.h>
#include <map>
#include <list>
#include <sched.h>
#include <atomic>
using namespace std;

#define DEBUG_INFO(format, ...) printf("%s:%d -- " format "\n", __func__, __LINE__,##__VA_ARGS__)

std::map<pthread_t ,int> thread_index_map;
typedef void (*sighandler)(int signo);
struct mystruct{
    int index;
    pthread_t thread;
    pthread_attr_t attr;
    struct sched_param sched;


};

void clean_up_resource(void *arg){
    struct mystruct *pms = (struct mystruct *)arg;
    DEBUG_INFO("delete thread index = %d\n", pms->index);
    pthread_attr_destroy(&pms->attr);
    free(arg);
}
std::list<pthread_t> pthread_list;
// std::atomic_bool start = false;
atomic<long> start(0);
atomic<long> counter(0);
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *func(void *arg){
    pthread_list.push_back(pthread_self());
    struct mystruct *pms = (struct mystruct *)arg;
    static pthread_t thread_id = pthread_self();
    pthread_cleanup_push(clean_up_resource,arg);

    
    // int ret = pthread_setschedparam(pthread_self(), SCHED_FIFO, &pms->sched);
    // if (ret != 0) {
    //     perror("pthread_setschedparam");
    //     exit(-1);
    // }
    sched_setscheduler(pthread_self(), SCHED_FIFO,&pms->sched);//SCHED_FIFO//SCHED_RR(round-robin)
    pthread_mutex_lock(&mtx);
    while(start == 0){
        pthread_cond_wait(&cond,&mtx);
    }
    pthread_mutex_unlock(&mtx);
    DEBUG_INFO("start thread = %d",pms->index);
    pthread_attr_getschedparam(&pms->attr,&pms->sched);
    DEBUG_INFO("pms->sched.sched_priority = %d",pms->sched.sched_priority);
    
    while(1){
        for(int i = 0;i < 10000;i++){
            //pthread_testcancel();
            for(int j = 0;j < 10000;j++){counter++;}
        }
        break;
    }
    DEBUG_INFO("index = %d",pms->index);
    while(1)sleep(1);
    return 0;
    pthread_cleanup_pop(0);
}

void sig_func(int signo){
    auto it = pthread_list.begin();
    DEBUG_INFO("%lu",pthread_list.size());
    for(;it != pthread_list.end();it++){
        
        pthread_cancel(*it);
    }
    DEBUG_INFO("good bye");
    sleep(2);
    std::cout << "counter = " << counter << std::endl;
    exit(0);
}

int main(int argc, char**){
    sighandler sig_ret;
    sig_ret = signal(SIGINT, sig_func);
    if(sig_ret == SIG_ERR){
        perror("signal");
        exit(-1);
    }
    DEBUG_INFO("pid = %u",getpid());
    for(int i=0; i < 10; i++){
        pthread_t t;
        struct mystruct *pms = (struct mystruct*)malloc(sizeof(struct mystruct));
        if(!pms){
            perror("malloc");
            raise(SIGINT);
            while(1)sleep(1);
        }
        pms->index = i;       

        pthread_attr_init(&pms->attr);
        pthread_attr_getschedparam(&pms->attr,&pms->sched);
        DEBUG_INFO("pms->sched.sched_priority = %d",pms->sched.sched_priority);
        pthread_attr_setschedpolicy(&pms->attr,SCHED_FIFO);//SCHED_OTHER);//SCHED_RP);//SCHED_FIFO);
        pms->sched.sched_priority = 100 - (i + 1)*5;
        pthread_attr_setschedparam(&pms->attr,&pms->sched);

        pthread_attr_getschedparam(&pms->attr,&pms->sched);
        DEBUG_INFO("pms->sched.sched_priority = %d",pms->sched.sched_priority);

        
        int ret = pthread_create(&t, NULL, func,pms);
        if(ret != 0){
            perror("malloc");
            raise(SIGINT);
            free(pms);            
            
        }
    }
    start = 1;
    //pthread_cond_signal(&cond);
    pthread_cond_broadcast(&cond);
    while(1){
        sleep(1);
    }
    return 0;
}

测试结果略

奇怪的优化代表着腐朽

#ifndef _GNU_SOURCE
#define _GNU_SOURCE     /* To get pthread_getattr_np() declaration */
#endif
#ifndef _REENTRANT
#define _REENTRANT
#endif
#include <stdio.h>
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <semaphore.h>
#include <signal.h>
#include <map>
#include <list>
#include <sched.h>
#include <atomic>

using namespace std;

#define DEBUG_INFO(format, ...) printf("%s:%d -- " format "\n", __func__, __LINE__,##__VA_ARGS__)

std::map<pthread_t ,int> thread_index_map;
typedef void (*sighandler)(int signo);
struct mystruct{
    int index;
    pthread_t thread;
    pthread_attr_t attr;
    struct sched_param sched;


};

void clean_up_resource(void *arg){
    struct mystruct *pms = (struct mystruct *)arg;
    DEBUG_INFO("delete thread index = %d", pms->index);
    pthread_attr_destroy(&pms->attr);
    free(arg);
}
std::list<pthread_t> pthread_list;
// std::atomic_bool start = false;
atomic<long> start(0);
atomic<long> start_sync(0);
atomic<long> counter(0);
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

sem_t gsem;
void *func(void *arg){
    pthread_list.push_back(pthread_self());
    struct mystruct *pms = (struct mystruct *)arg;
    static pthread_t thread_id = pthread_self();
    pthread_cleanup_push(clean_up_resource,arg);

    
    // int ret = pthread_setschedparam(pthread_self(), SCHED_FIFO, &pms->sched);
    // if (ret != 0) {
    //     perror("pthread_setschedparam");
    //     exit(-1);
    // }
    sched_setscheduler(pthread_self(), SCHED_FIFO,&pms->sched);//SCHED_FIFO//SCHED_RR(round-robin)
    pthread_mutex_lock(&mtx);
    while(start == 0){
        pthread_cond_wait(&cond,&mtx);
    }
    pthread_mutex_unlock(&mtx);
    sleep(1);//等待1秒,是为了最大限度的让每个线程同步
    while(start_sync == 0){}
    //到这里,理想情况下,能够最先得到信号量的线程就是优先级最高的线程
    sem_wait(&gsem);
    int sem_value = 0;
    sem_getvalue(&gsem,&sem_value);
    pthread_attr_getschedparam(&pms->attr,&pms->sched);
    //DEBUG_INFO("start thread = %d,pms->sched.sched_priority = %d,sem_value = %d",pms->index,pms->sched.sched_priority,sem_value);
    
    while(1){
        for(int i = 0;i < 10000;i++){
            //pthread_testcancel();
            for(int j = 0;j < 10000;j++){
                // counter++;
            }
            //usleep(1);
        }
        break;
    }
    pthread_attr_getschedparam(&pms->attr,&pms->sched);
    printf("%d,",pms->index);
    //DEBUG_INFO("index = %d,pms->sched.sched_priority = %d",pms->index,pms->sched.sched_priority);
    while(1)sleep(1);
    return 0;
    pthread_cleanup_pop(0);
}

void sig_func(int signo){
    auto it = pthread_list.begin();
    DEBUG_INFO("%lu",pthread_list.size());
    for(;it != pthread_list.end();it++){
        
        pthread_cancel(*it);
    }
    DEBUG_INFO("%s","good bye");
    sleep(2);
    std::cout << "counter = " << counter << std::endl;
    exit(0);
}

int main(int argc, char**){
    sighandler sig_ret;
    sig_ret = signal(SIGINT, sig_func);
    if(sig_ret == SIG_ERR){
        perror("signal");
        exit(-1);
    }
    sem_init(&gsem,0,0);
    DEBUG_INFO("pid = %u",getpid());
    for(int i=0; i < 10; i++){
        pthread_t t;
        struct mystruct *pms = (struct mystruct*)malloc(sizeof(struct mystruct));
        if(!pms){
            perror("malloc");
            raise(SIGINT);
            while(1)sleep(1);
        }
        pms->index = i;       

        pthread_attr_init(&pms->attr);
        pthread_attr_setinheritsched(&pms->attr,PTHREAD_EXPLICIT_SCHED);
        pthread_attr_getschedparam(&pms->attr,&pms->sched);
        //DEBUG_INFO("pms->sched.sched_priority = %d",pms->sched.sched_priority);
        pthread_attr_setschedpolicy(&pms->attr,SCHED_FIFO);//SCHED_OTHER);//SCHED_RP);//SCHED_FIFO);
        pms->sched.sched_priority = (i + 1);//*5;//100 - (i + 1)*5;
        pthread_attr_setschedparam(&pms->attr,&pms->sched);

        pthread_attr_getschedparam(&pms->attr,&pms->sched);
        //DEBUG_INFO("pms->sched.sched_priority = %d",pms->sched.sched_priority);

        
        int ret = pthread_create(&t, NULL, func,pms);
        if(ret != 0){
            perror("malloc");
            raise(SIGINT);
            free(pms);            
            
        }
    }
    start = 1;
    //pthread_cond_signal(&cond);
    pthread_cond_broadcast(&cond);
    for(int i = 0;i < 10;i++){ 
        sem_post(&gsem);
    }
    sleep(1);
    start_sync = 1;
    sleep(5);
    printf("\n");
    
    while(1){
        sleep(1);
    }
    return 0;
}

执行结果:

main:115 -- pid = 60349
0,2,5,4,6,1,8,3,9,7,

 小结

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

posix线程的优先级测试 的相关文章

随机推荐

  • adb导出手机应用到电脑

    简单说一下相关步骤 xff0c 以备不时之需 1 手机开启usb调试 2 Windows系统 Win 43 R打开命令行窗口 xff0c 输入adb devices xff0c 如果连接成功会出现机子的序列号 3 adb shell pm
  • Js作用域与作用域链详解

    一直对Js的作用域有点迷糊 xff0c 今天偶然读到Javascript权威指南 xff0c 立马被吸引住了 xff0c 写的真不错 我看的是第六版本 xff0c 相当的厚 xff0c 大概1000多页 xff0c Js博大精深 xff0c
  • windows10环境下tensorflow安装教程

    楼主最近一直忙着找工作 最近几个月一直all in java 好久没学机器学习 深度学习 前几天突然通知要提交论文中期了 于是赶紧打开电脑 结果发现之前安装的tensorflow居然登陆不上了 折腾了半天 搜过各种csdn博客 一直安装失败
  • 'gbk' codec can't encode character '\xa0'

    从网上抓了一些字节流 xff0c 想打印出来结果发生了一下错误 xff1a UnicodeEncodeError 39 gbk 39 codec can 39 t encode character 39 xbb 39 in position
  • 【Git记录学习】github创建项目以及本地使用(vscode)

    一 github创建空仓库 从github中创建空仓库 在执行完上一步操作后会返回这样的界面 xff0c 包括了一些基本的git操作以及HttpS SSH地址 生成一个readme md文档 xff08 步骤2 Set up下面有蓝色的超链
  • 关于DFT变换含义、公式和具体形式

    原文地址 xff1a http blog sina com cn s blog 7853c3910102v9wd html 这篇文章从实际工程应用的角度 xff0c 记录一下如何计算 xff0c 关于公式 变形和应用 维基百科上的 DFT公
  • 1602显示数字不稳定一直跳动(AD转换)

    程序如下所示 首先说明下 xff0c 此程序为AD转换芯片PCF8591采集电压数据 xff0c 然后送到1602显示 现象 xff1a 1602显示的数字一直频繁的跳动 xff0c 乱花眼 此现象不是一直出现的 xff0c 有时候会出现
  • C++11中的线程类

    前面介绍的线程是利用了POSIX线程库 xff0c 这是传统C C 43 43 程序员使用线程的方式 xff0c 而C 43 43 11提供了语言层面使用线程的方式 C 43 43 11新标准中引入了5个头文件来支持多线程编程 xff0c
  • 4.4.1内核编译

    内核源码下载地址 xff1a https mirrors edge kernel org pub linux kernel v4 x linux 4 4 1 tar gz 安装依赖包 xff1a 报错就装 cp boot config xx
  • fatal error: hugetlbfs.h: No such file or directory

    fatal error hugetlbfs h No such file or directory 解决办法 xff1a sudo apt get update sudo apt get install libhugetlbfs dev
  • WSL下 配置NFS-失败

    配置一个IP地址 xff1a sudo ip addr add 192 168 250 2 24 broadcast 192 168 250 255 dev eth2 sudo apt get install nfs kernel serv
  • OMT 对象模型、动态模型和功能模型

    对象模型描述系统中对象的静态结构 对象之间的关系 对象的属性 对象的操作 对象模型表示静态的 结构上的 系统的 数据 34 特征 对象模型为动态模型和功能模型提供了基本的框架 xff0c 对象模型用包含对象和类的对象图来表示 OMT的对象模
  • 关于epoll的调试的几个问题

    将今天调试的几个小问题点总结下 xff0c 后续遇到再添加 一 将总结的问题点放在最前面 1 epoll wait的maxevents参数 epoll wait的maxevents参数 xff0c 经过测试 xff0c maxevents的
  • poll函数测试

    一 基础知识 include lt poll h gt int poll struct pollfd fds nfds t nfds int timeout 其中参数fds指向一个结构体数组的第0个元素的指针 xff0c 每个数组元素都是一
  • IPC:匿名管道和命名管道

    一 管道初级测试 写两个小程序 xff0c 一个负责向管道发数据 xff0c 一个从管道接收数据 xff1b pipe cpp include lt iostream gt using namespace std int main cout
  • IPC:system V消息队列

    ftok函数 ftok convert a pathname and a project identifier to a System V IPC key SYNOPSIS include lt sys types h gt include
  • IPC:system V 信号量和共享内存

    信号量相关知识 结构体 union semun int val Value for SETVAL struct semid ds buf Buffer for IPC STAT IPC SET unsigned short array Ar
  • 信号signal编程测试

    信号会打断系统调用 xff0c 慎用 xff0c 就是用的时候测一测 下面是信号的基础测试 信号 信号 xff08 signal xff09 机制是UNIX系统中最为古老的进程之间的通信机制 它用于在一个或多个进程之间传递异步信号 信号可以
  • 线程间通讯的信号量semaphore.h

    sem init SEM INIT 3 Linux Programmer 39 s Manual SEM INIT 3 NAME sem init initialize an unnamed semaphore SYNOPSIS inclu
  • posix线程的优先级测试

    测试的时候 xff0c 如果创建的线程不够多 xff0c 有些问题体现不出来 xff0c 例如pthread cond signal和pthread cond broadcast 奇怪的优化是不会有好结果的 优先级打印 xff1a 测试目的