Reputation: 6526
My PC has the following characteristics -
# 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
Here is the program -
#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;
}
The output is the following -
# ./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
The setting of the scheduling fails. What can I do to fix it?
Upvotes: 4
Views: 1630
Reputation: 1
pthread_setschedparam(3) succeeds by returning 0 and fails by returning an error code like errno(3) and suitable for strerror(3). So code
rc = pthread_setschedparam(pthread_self(), policy, &sched_param);
if( rc != 0) {
cerr<<"Setting Scheduling paramter failed rc="<<rc
<< " " << strerror(rc) << endl;
}
to find out why. Read also carefully sched_setscheduler(2), notably its NOTES section...
According to the man
page of pthread_setschedparam(3) EINVAL
means:
EINVAL
: policy is not a recognized policy, or param does not make sense for the policy.
Upvotes: 2