Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t返回C中线程的调度优先级_C_Linux_Multithreading - Fatal编程技术网

Can';t返回C中线程的调度优先级

Can';t返回C中线程的调度优先级,c,linux,multithreading,C,Linux,Multithreading,我一直在做一项艰巨的任务,这项任务需要大量的研究,我已经走到了死胡同。我一直在阅读linux手册页,但我很难吸收其中的任何信息。这是我用C语言编程的第三周 我已经创建了一个新线程,并将其调度优先级设置为SCHED_FIFO。在我的程序中,我创建它,让它等待两秒钟,然后一旦完成,我就尝试打印出我给它的优先级(所以基本上,我希望它打印出“policy=SCHED_FIFO”)。我也将不得不为SCHED_OTHER做这件事,但一旦我明白了这一点,我自己就应该很容易做到 #include <std

我一直在做一项艰巨的任务,这项任务需要大量的研究,我已经走到了死胡同。我一直在阅读linux手册页,但我很难吸收其中的任何信息。这是我用C语言编程的第三周

我已经创建了一个新线程,并将其调度优先级设置为SCHED_FIFO。在我的程序中,我创建它,让它等待两秒钟,然后一旦完成,我就尝试打印出我给它的优先级(所以基本上,我希望它打印出“policy=SCHED_FIFO”)。我也将不得不为SCHED_OTHER做这件事,但一旦我明白了这一点,我自己就应该很容易做到

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

void *FIFORunner(void *vargp, pthread_t thread);


int main(){
    pthread_t myThread;
    printf("Before Thread\n\n");

    pthread_create(&myThread, NULL, FIFORunner, NULL);

    pthread_join(myThread, NULL);

    printf("After Thread\n");

    return 0;
} 



void *FIFORunner(void *vargp, pthread_t myThread){

   int s, policy;

   struct sched_param param;

   pthread_setschedparam(&myThread, SCHED_FIFO, &param);

   sleep(2);

   s = pthread_getschedparam(myThread, &policy, &param);

   printf("policy = %d\n", policy);
   return NULL;
}
#包括
#包括
#包括
#包括
void*FIFORunner(void*vargp,pthread\u t thread);
int main(){
pthread_t myThread;
printf(“线程前\n\n”);
pthread_create(&myThread,NULL,FIFORunner,NULL);
pthread_join(myThread,NULL);
printf(“线程后\n”);
返回0;
} 
void*FIFORunner(void*vargp,pthread\u t myThread){
INTS,政策;
结构sched_param param;
pthread_setschedparam(&myThread、SCHED_FIFO和¶m);
睡眠(2);
s=pthread_getschedparam(myThread、策略和参数);
printf(“策略=%d\n”,策略);
返回NULL;
}
这是我的密码,没什么特别的。每当我运行它时,它都会输出一个看似随机的整数,例如:policy=1464624896、policy=-1630095616和policy=1487660800


如何使pthread_getschedparam()工作?我传错东西了吗?

以下建议代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

void *FIFORunner( void *vargp );


int main( void )
{
    pthread_t myThread;
    printf("Before Thread\n\n");

    // any priority and/or scheduling changes should be
    // applied via the second parameter to 'pthread_create()'
    pthread_create( &myThread, NULL, FIFORunner, NULL);

    printf( "in main: myThread = %d\n", (int)myThread );

    pthread_join( myThread, NULL );

    printf("After Thread\n");

    return 0;
}


void *FIFORunner( void *vargp )
{
    (void)vargp;

   pthread_t myThread = pthread_self();

    int policy = SCHED_OTHER;   // any higher policy will cause 'pthread_setschedparam()` to fail.


    struct sched_param param;
    param.sched_priority = 0;   // any higher priority will cause 'pthread_setschedparam()` to fail.


    printf( "in Thread: myThread = %d\n", (int)myThread );
    if( pthread_setschedparam( myThread, policy, &param ) )
    {
        perror( "pthread_setschedparm failed" );
        pthread_exit( NULL );
    }

    sleep(2);

    if( pthread_getschedparam( myThread, &policy, &param ) )
    {
        perror( "pthread_getschedparam failed" );
        pthread_exit( NULL );
    }

    printf( "policy = %d\n", policy );
    pthread_exit( NULL );
}
  • 干净地编译
  • 执行所需的功能
  • 如果尝试提高优先级或计划级别,则将失败
  • 正确检查错误
  • 一旦你有了这个基本代码,你就可以尝试提高
    优先级和/或
    调度级别
  • 建议在创建线程时在“attributes”参数中设置优先级和/或调度级别,因为在线程内提高这些值不起作用
  • 这是线程函数的语法:
    void*(*start\u例程)(void*)
    请注意,只有一个参数,而不是两个。因此,OPs发布的代码不会编译。(这不是唯一的原因)
  • 关于:pthread_setschedparam(&myThread、SCHED_FIFO和¶m);变量
    myThread
    是函数
    main()
    中的局部变量,因此在main()的作用域之外不可见。结果是发布的代码无法编译
  • 关于:void*FIFORunner(void*vargp)未使用参数vargp。这将导致编译器输出警告消息。要消除警告消息,函数中的第一行应该是:
    (void)vargp
  • 对于两者:
    pthread_setschedparam(&myThread、SCHED_FIFO和¶m)
    s=pthread_getschedparam(myThread,&policy,¶m)应检查返回值(=0),以确保操作成功。如果未成功,请调用
    peror()
    ,输出所附文本以及系统认为操作失败的原因
  • 关于:
    返回NULL
    这将“起作用”,但是,留在
    pthread
    函数族中的等效语句是:
    pthread_exit(NULL)
  • 现在,拟议的守则:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <pthread.h>
    
    void *FIFORunner( void *vargp );
    
    
    int main( void )
    {
        pthread_t myThread;
        printf("Before Thread\n\n");
    
        // any priority and/or scheduling changes should be
        // applied via the second parameter to 'pthread_create()'
        pthread_create( &myThread, NULL, FIFORunner, NULL);
    
        printf( "in main: myThread = %d\n", (int)myThread );
    
        pthread_join( myThread, NULL );
    
        printf("After Thread\n");
    
        return 0;
    }
    
    
    void *FIFORunner( void *vargp )
    {
        (void)vargp;
    
       pthread_t myThread = pthread_self();
    
        int policy = SCHED_OTHER;   // any higher policy will cause 'pthread_setschedparam()` to fail.
    
    
        struct sched_param param;
        param.sched_priority = 0;   // any higher priority will cause 'pthread_setschedparam()` to fail.
    
    
        printf( "in Thread: myThread = %d\n", (int)myThread );
        if( pthread_setschedparam( myThread, policy, &param ) )
        {
            perror( "pthread_setschedparm failed" );
            pthread_exit( NULL );
        }
    
        sleep(2);
    
        if( pthread_getschedparam( myThread, &policy, &param ) )
        {
            perror( "pthread_getschedparam failed" );
            pthread_exit( NULL );
        }
    
        printf( "policy = %d\n", policy );
        pthread_exit( NULL );
    }
    
    #包括
    #包括
    #包括
    #包括
    void*FIFORunner(void*vargp);
    内部主(空)
    {
    pthread_t myThread;
    printf(“线程前\n\n”);
    //任何优先级和/或计划更改都应
    //通过第二个参数应用于“pthread_create()”
    pthread_create(&myThread,NULL,FIFORunner,NULL);
    printf(“主:myThread=%d\n”,(int)myThread);
    pthread_join(myThread,NULL);
    printf(“线程后\n”);
    返回0;
    }
    void*FIFORunner(void*vargp)
    {
    (无效)vargp;
    pthread_t myThread=pthread_self();
    int policy=SCHED_OTHER;//任何更高的策略都会导致“pthread_setschedparam()”失败。
    结构sched_param param;
    param.sched_priority=0;//任何更高的优先级都会导致“pthread_setschedparam()”失败。
    printf(“线程中:myThread=%d\n”,(int)myThread);
    if(pthread_setschedparam(myThread、policy和param))
    {
    perror(“pthread_setschedparm失败”);
    pthread_exit(NULL);
    }
    睡眠(2);
    if(pthread_getschedparam(myThread、策略和参数))
    {
    perror(“pthread_getschedparam失败”);
    pthread_exit(NULL);
    }
    printf(“策略=%d\n”,策略);
    pthread_exit(NULL);
    }
    
    @user3629249第一点有效。第二个是有效的。第三个(未使用的参数)是不同组织的主观代码风格。第四个是正确的,并且很可能解释问题。第五个是无效的,请检查手册页中的
    pthread\u create
    @immibis,任何接受未干净编译的代码的组织都在乞求失败。因此,编译器发出的
    未使用参数的警告
    不是“主观代码样式”,但是,使用
    pthread_exit()
    return
    可以认为是编码样式。@immibis,我把我的评论移到了答案上。我否决了这一点,因为这是一个只使用代码的答案,没有解释问题是什么,也没有解释问题是如何解决的。@immibis,也许你错过了这个答案中的前5条语句。再加上我发表的评论,我清楚地指出了老年退休金问题的根源。我把我所有的评论都转移到了我的博客上answer@immibis,代码未编译,调用:
    pthread_setschedparam()
    失败,threead函数中对
    myThread
    的引用不起作用。OP还需要什么解释?对
    pthread_setschedparam
    的调用在代码中是否失败?它在OP的代码中失败了吗?为了不失败,你做了什么改变?