首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么设置计划的策略和参数在我的程序- policy中失败?

为什么设置计划的策略和参数在我的程序- policy中失败?
EN

Stack Overflow用户
提问于 2014-01-07 07:27:07
回答 1查看 1.5K关注 0票数 4

我的个人电脑有以下特点-

代码语言:javascript
复制
# chrt -m
SCHED_OTHER min/max priority    : 0/0
SCHED_FIFO min/max priority : 1/99
SCHED_RR min/max priority   : 1/99
SCHED_BATCH min/max priority    : 0/0
SCHED_IDLE min/max priority : 0/0

这是节目-

代码语言:javascript
复制
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
/** get pid **/
#include <sys/types.h>
#include <unistd.h>
/** kill signal **/
#include <signal.h>

using namespace std;

int shared_variable = 0;


class helium_thread
{
  private:
  pthread_t *thread_id;
  pid_t process_pid;

  public:
  static pthread_mutex_t mutex_thread;
  static pthread_cond_t cond_var;
  void set_thread_id(pthread_t tid);
  pthread_t *get_thread_id();
  int create_thread(pthread_t *thread_ptr, const pthread_attr_t *attr, void * (*start_routine)(void *), void *arg );
  helium_thread();  
  ~helium_thread();

};

helium_thread thread_1, thread_2;
/** The definition of the static member can't be inside a function, You need to put it outside **/
/** When I tried using inside a function, I got the error - error: invalid use of qualified-name ‘helium_thread::mutex_thread **/

pthread_mutex_t helium_thread::mutex_thread = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  helium_thread::cond_var = PTHREAD_COND_INITIALIZER;


void helium_thread::set_thread_id( pthread_t tid)
{
   *(this->thread_id) = tid;    
}

pthread_t * helium_thread::get_thread_id( )
{
   return (this->thread_id);
}

int helium_thread::create_thread(pthread_t *thread_ptr, const pthread_attr_t *attr, void * (*start_routine)(void *), void *arg )
{
   int ret;
   ret = pthread_create(thread_ptr,attr,start_routine,(void *)arg)  ;
   return ret;

}

helium_thread::helium_thread()
{

    thread_id = new pthread_t;
}

helium_thread::~helium_thread()
{
    delete thread_id;
}


/** While defining the methods of the class, Keywords static and virtual should not be repeated in the definition. **/
/** They should only be used in the class declaration. **/

/** remember this **/

#if 0
enum schedparam_policy {
    SCHED_RR,
    SCHED_IO,
    SCHED_FIFO,
    SCHED_OTHER
};

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

#endif

/*** End here ***/

void print_sched_priority()
{

    int rc;
   struct sched_param sched_param;
   int  policy;

   rc = pthread_getschedparam(pthread_self(), &policy, &sched_param);

   if( rc != 0)
     {
      cout<<"Getting Scheduling paramter failed"<<endl;   
     }
   else
   {
     cout<<"The priority of Thread_Func_A is "<< sched_param.sched_priority<<endl;  
     const char *policy_name = (policy == SCHED_FIFO) ? "SCHED_FIFO" : (policy == SCHED_RR) ? "SCHED_RR" : (policy == SCHED_OTHER) ? "SCHED_OTHER":"??";
     cout<<"The policy of Thread_Func_A is "<< policy_name<<endl;
     cout<<"The max priority is "<<sched_get_priority_max(policy)<<endl;
     cout<<"The min priority is "<<sched_get_priority_min(policy)<<endl;

   }
}


void set_sched_priority()
{

   int rc;
   struct sched_param sched_param;
   int  policy;

   policy = SCHED_RR;

   /** highest priority **/
   sched_param.sched_priority = 0; 

   /** with chrt -m or sched_get_priority_max, I get to know the priority range **/

   rc = pthread_setschedparam(pthread_self(), policy, &sched_param);

   if( rc != 0)
     {
      cout<<"Setting Scheduling paramter failed"<<endl;   
     }
   else
   {
     cout<<"The priority of Thread_Func_A is "<< sched_param.sched_priority<<endl;  
     const char *policy_name = (policy == SCHED_FIFO) ? "SCHED_FIFO" : (policy == SCHED_RR) ? "SCHED_RR" : (policy == SCHED_OTHER) ? "SCHED_OTHER":"??";
     cout<<"The policy of Thread_Func_A is "<< policy_name<<endl;

   }
}

void *Thread_Function_A(void *thread_arg)
{
  print_sched_priority();
  set_sched_priority();
  print_sched_priority();

  #if 0

  while(shared_variable < 10)
  {
     /** print only odd numbers **/
    pthread_mutex_lock(&(helium_thread::mutex_thread));


    if ( (shared_variable % 2 ) == 0 )
    {
     /** It means the even number; this thread should not print even numbers **/    
     pthread_cond_wait(&helium_thread::cond_var, &helium_thread::mutex_thread);

    }

   cout<<"Thread A shared_variable ..odd = "<< shared_variable<<endl;  
   shared_variable++;
   pthread_mutex_unlock(&(helium_thread::mutex_thread));

 }
 #endif
    pthread_exit(NULL);
}

void thread_b_cleanup (void* buffer)
{
  cout<<"Clean up for thread_b"<<endl;
  delete[] (int *)buffer;
}

void *Thread_Function_B(void *thread_arg)
{

   void *thread_b_resource = new int[5];    
   pthread_cleanup_push (thread_b_cleanup, thread_b_resource);

  while(shared_variable < 10)
  { 
     pthread_mutex_lock(&(helium_thread::mutex_thread));

     if ( (shared_variable % 2 ) != 0 )
     {
        /** This means odd **/ 
        pthread_cond_signal (&helium_thread::cond_var);
     }

     else
     {

              cout<<"Thread B prints shared_variable--even = "<< shared_variable<<endl; 
              shared_variable++;

     }


  pthread_mutex_unlock(&(helium_thread::mutex_thread));


 }
  pthread_cleanup_pop (1);
  pthread_exit(NULL);

}


int main(int argc, char *argv[])
{
   pid_t thread_pid_val = getpid();
   thread_1.create_thread((thread_1.get_thread_id()), NULL,Thread_Function_A,&thread_pid_val);
   //thread_2.create_thread((thread_2.get_thread_id()), NULL,Thread_Function_B,&thread_pid_val);
   pthread_join( *(thread_1.get_thread_id()), NULL);
   //pthread_join( *(thread_2.get_thread_id()), NULL);

   return  0;   
}

输出如下-

代码语言:javascript
复制
# ./thread_basic.out 
The priority of Thread_Func_A is 0
The policy of Thread_Func_A is SCHED_OTHER
The max priority is 0
The min priority is 0
Setting Scheduling paramter failed
The priority of Thread_Func_A is 0
The policy of Thread_Func_A is SCHED_OTHER
The max priority is 0
The min priority is 0

日程设置失败。我能做些什么来解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-07 08:45:22

赛特里奇帕拉姆(3)通过返回0获得成功,通过返回类似于errno(3)的错误代码并适合于错误(3)而失败。所以代码

代码语言:javascript
复制
 rc = pthread_setschedparam(pthread_self(), policy, &sched_param);
 if( rc != 0) {
  cerr<<"Setting Scheduling paramter failed rc="<<rc 
      << " " << strerror(rc) << endl;   
 }

找出原因。也请仔细阅读setscheduler调度器(2),特别是其注释部分..。

根据man页面赛特里奇帕拉姆(3) EINVAL的意思是:

EINVAL:策略不是公认的策略,或者param对策略没有意义。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20966176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档