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

标签 linux linux-kernel pthreads

我的电脑有以下特点——

# 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

这是程序-

#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;   
}

输出如下-

# ./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

调度设置失败。我该怎么做才能修复它?

最佳答案

pthread_setschedparam(3)通过返回 0 成功,通过返回类似 errno(3) 的错误代码失败。适合 strerror(3) .所以代码

 rc = pthread_setschedparam(pthread_self(), policy, &sched_param);
 if( rc != 0) {
  cerr<<"Setting Scheduling paramter failed rc="<<rc 
      << " " << strerror(rc) << endl;   
 }

找出原因。也仔细阅读sched_setscheduler(2) ,特别是它的NOTES部分...

根据 pthread_setschedparam(3)man 页面EINVAL 表示:

EINVAL: policy is not a recognized policy, or param does not make sense for the policy.

关于linux - 为什么计划策略和参数的设置在我的程序 - pthread Linux 中失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20966176/

相关文章:

pthreads - 如何知道 pthread 是否死亡

c - rawsocket sendto() 一些数据包被丢弃并且在网络中看不到

memory-management - 内存调试 : How to get locked pages information in user space/kernel space in linux

c - 将数组传递给 pthread_create

我可以使用 openmp 或 pthreads 通过不同线程调用相同的系统调用吗

linux-kernel - cdev_add() 实际上是做什么的?在向内核注册设备方面

linux - 为什么 sed 会产生两条相同的线?

linux - 解析和替换两个文件中的一些字符串

linux - 用户名中的 pkill 通配符

android - 以 tail -f 方式实时监视/开发文件