2012-12-20 18 views
6

Nie mogę ustawić priorytetu Pthread przy użyciu pthread_attr_setschedparam(). Próbowałem rozwiązać ten problem, ale nie mogłem tego zrobić. Sprawdziłem także mój podręcznik tekstowy, który również wykorzystuje tę samą funkcję. Skopiowałem ten kod z książki. Czy możesz mi powiedzieć, jak ustawić priorytet wątku?Nie można ustawić priorytetu Pthread

Oto kod:

void *Func(void *arg); 
int main() 
{ 
pthread_t tid[5]; 

pthread_attr_t *tattr; 
struct sched_param param; 
int pr,error,i; 

do 
{ 
if((tattr=(pthread_attr_t *)malloc(sizeof(pthread_attr_t)))==NULL) 
{ 
    printf("Couldn't allocate memory for attribute object\n"); 
} 
}while(tattr==NULL); 

if(error=pthread_attr_init(tattr)) 
{ 
    fprintf(stderr,"Attribute initialization failed with error %s\n",strerror(error)); 
} 

for(i=0;i<5;i++) 
{ 
    //scanf("%d",&pr); 
     error = pthread_attr_getschedparam(tattr,&param); 

     if(error!=0) 
     { 
      printf("failed to get priority\n"); 
     } 

     param.sched_priority=10; 
     error=pthread_attr_setschedparam(tattr,&param); 

     if(error!=0) 
     { 
      printf("failed to set priority\n"); 
     } 
/* 
    if(i%2==0) 
    { 
     if(error=pthread_attr_setdetachstate(tattr,PTHREAD_CREATE_DETACHED)) 

     { 
      fprintf(stderr,"Failed to set thread attributes with error %s\n",strerror(error)); 
     } 
    } 
    else 
     if(error=pthread_attr_setdetachstate(tattr,PTHREAD_CREATE_JOINABLE)) 
     { 
      fprintf(stderr,"Failed to set thread attributes with error %s\n",strerror(error)); 
     } 
*/  
     pthread_create(&tid[i],tattr,Func,NULL); 


     printf("waiting for thread %d\n",i); 
} 

free(tattr);// release dynamically allocated memory 

printf("All threads terminated\n"); 
return 0; 
} 

void *Func(void *arg) 
{ 
printf("inside\n"); 

pthread_attr_t *tattr=(pthread_attr_t *)arg; 
int state,error; 

struct sched_param param; 

error=pthread_attr_getdetachstate(tattr,&state); 

if(error==0 && state==PTHREAD_CREATE_DETACHED) 
{ 
    printf(" My state is DETACHED\n"); 
} 
else 
    if(error==0 && state==PTHREAD_CREATE_JOINABLE) 
    { 
     printf(" My state is JOINABLE\n"); 
    } 

error=pthread_attr_getschedpolicy(tattr,&param); 

if(error==0) 
{ 
    printf(" My Priority is %d\n",param.sched_priority); 
} 

return NULL; 
} 
+0

Jaki wynik jest wyświetlany? Skąd wiesz, że to nie działa? – Celada

+0

Drukuję 'nie udało się uzyskać priorytetu' dla sprawdzenia, czy priorytet wątku jest ustawiony, czy nie. i Zawsze jest drukowane, co oznacza, że ​​priorytet nie jest ustawiony. – Alfred

+1

Czy działasz jako root? Tylko root może zwiększać priorytet (zmniejszać ładną wartość). – anishsane

Odpowiedz

4

Twój pthread_attr_setschedparam wywołanie się niepowodzeniem z "Nieprawidłowy parametr". Twój program rozpocznie się od domyślnej polityki szeregowania linux z SCHED_OTHER. Nie można zmienić priorytetu SCHED_OTHER.

od człowieka (2) sched_setscheduler:

SCHED_OTHER mogą być używane tylko ze statycznym priorytetem 0. SCHED_OTHER jest standardowy Linux czas dzielenia harmonogram, który jest przeznaczony dla wszystkich procesów, które nie wymagają specjalnego priorytetu statycznego mechanizmy czasu rzeczywistego.

Jeśli zmienisz politykę w atrybucie pthread na inny rodzaj harmonogramu przed próbą zmiany priorytetu, twój program będzie działał. Coś jak

for (i = 0;i < 5;i++) 
{ 
    policy = SCHED_RR; 

    error = pthread_attr_setschedpolicy(tattr, policy); 

    // insert error handling 

    error = pthread_attr_getschedparam(tattr, &param); 

    // insert error handling 

    // yada yada yada ... 
} 
+0

Umieszczaj także w 'pthread_exit' w głównym i/lub' pthread_join' odpowiednim wątku, gdy odkomentujesz ten kod odłączenia/dołączenia. – Duck

0
I created thread in main and set attributes using  
pthread_attr_setechedparam(...) .Once the child thread is up and running  i want to check this thread priority and policy set before using 
pthread_getschedparam(, ,). I executed code sudo still it is printing  zero for both pri and policy. Please find the code below. 
#include "stdio.h" 

#include "stdlib.h" 

#include "pthread.h" 

void *Func(void *arg); 
int main() 
{ 

pthread_t tid; 

pthread_attr_t *tattr; 

struct sched_param param; 

int pr,error,i; 

int pol; 
do 

    { 

if((tattr=(pthread_attr_t *)malloc(sizeof(pthread_attr_t)))==NULL) 

{ 

    printf("Couldn't allocate memory for attribute object\n"); 
    } 


}while(tattr==NULL); 

    if(error=pthread_attr_init(tattr)) 
{ 
    fprintf(stderr,"Attribute initialization failed with error %s\n",strerror(error)); 
    } 

//for(i=0;i<5;i++) 
    //{ 
    error = pthread_attr_getschedparam(tattr,&param); 

    if(error!=0) 
    { 
     printf("failed to get priority\n"); 
    } 

    int policy=1; 
    error=pthread_attr_setschedpolicy(tattr,policy); 
    if(error!=0) 
    { 
     printf("failed to set priority\n"); 
    } 

    param.sched_priority=10; 
    error=pthread_attr_setschedparam(tattr,&param); 

    if(error!=0) 
    { 
     printf("failed to set priority\n"); 
    } 

    pthread_create(&tid,tattr,Func,NULL); 

    pthread_getschedparam(tid,&pol,&param); 
    printf("policy:: %d pri :: %d\n",pol,param.sched_priority); 

    printf("waiting for thread %d\n",i); 

    pthread_join(tid,NULL); 
    free(tattr);// release dynamically allocated memory 

    printf("All threads terminated\n"); 
    return 0; 
    } 

    void *Func(void *arg) 
    { 
    printf("inside\n"); 
    int policy,err=0; 
    struct sched_param p; 
    err=pthread_getschedparam(pthread_self(),&policy,&p); 
    if(err!=0) 
    { 
    printf("error\n"); 
    } 
    printf("the priorty= %d policy =%d\n",p.sched_priority,policy); 

    } 
0

Ta implementacja pracował dla mnie (z tego postu pomocy), to proste:

pthread_t myThread; 
pthread_attr_t *tattr; 
struct sched_param param; 
int error,policy, prio; 

prio = 10; 

tattr = (pthread_attr_t *)malloc(sizeof(pthread_attr_t)); 
error = pthread_attr_init(tattr); 
cout << "init: " << error << endl; 

error = pthread_attr_getschedparam(tattr,&param); 
cout << "getschedparam: " << error << endl; 

error = pthread_attr_setinheritsched(tattr, PTHREAD_EXPLICIT_SCHED); 
cout << "setinheritsched: " << error << endl; 

policy = SCHED_RR; 
error = pthread_attr_setschedpolicy(tattr, policy); 
cout << "setschedpolicy = " << policy << ": " << error << endl; 

param.sched_priority = prio; 
error = pthread_attr_setschedparam(tattr ,&param); 
cout << "setschedparam: " << error << endl; 

error = pthread_create(&myThread,tattr,threadFunc,&numOfExecutions); 
cout << "create: " << error << endl; 

pthread_getschedparam(myThread,&policy,&param); 
printf("policy:: %d pri :: %d\n",policy,param.sched_priority); 

można znaleźć mój kod roboczych here

Powiązane problemy