2012-12-20 25 views
6

सेट करने में असमर्थ मैं pthread_attr_setschedparam() का उपयोग करके पर्थ्रेड प्राथमिकता सेट करने में असमर्थ हूं। मैंने इस मुद्दे को हल करने की कोशिश की है लेकिन यह नहीं कर सका। मैंने अपनी पाठ्य पुस्तक से भी परामर्श लिया जो एक ही समारोह का उपयोग करता है। मैंने इस कोड को पुस्तक से कॉपी किया है। क्या आप मुझे बता सकते हैं कि थ्रेड प्राथमिकता कैसे सेट करें?पर्थ्रेड प्राथमिकता

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

आप क्या परिणाम हो रही है मिल सकती है? आप कैसे जानते हैं कि यह काम नहीं कर रहा है? – Celada

+0

धागा की प्राथमिकता निर्धारित है या नहीं, यह जांचने के लिए मैं 'प्राथमिकता पाने में विफल रहा' प्रिंट करता हूं। और यह हमेशा मुद्रित होता है, जिसका अर्थ है प्राथमिकता सेट नहीं है। – Alfred

+1

क्या आप रूट के रूप में चल रहे हैं? केवल रूट प्राथमिकता बढ़ा सकता है (अच्छा मूल्य कम करें) – anishsane

उत्तर

4

आपका pthread_attr_setschedparam कॉल "अमान्य पैरामीटर" के साथ विफल हो रहा है:

यहाँ कोड है। आपका प्रोग्राम SCHED_OTHER की डिफ़ॉल्ट लिनक्स शेड्यूलिंग नीति से शुरू होगा। आप SCHED_OTHER की प्राथमिकता नहीं बदल सकते हैं।

आदमी से

(2) sched_setscheduler:

SCHED_OTHER केवल स्थिर प्राथमिकता पर इस्तेमाल किया जा सकता 0. SCHED_OTHER मानक लिनक्स समय सहभाजन अनुसूचक है कि सभी प्रक्रियाओं है कि विशेष स्थिर प्राथमिकता की आवश्यकता नहीं है के लिए बनाई गई है, वास्तविक समय तंत्र।

यदि आप प्राथमिकता को बदलने का प्रयास करने से पहले किसी अन्य प्रकार के शेड्यूल में नीति को बदलते हैं तो आपका प्रोग्राम काम करेगा।

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

जब आप उस पृथक/शामिल कोड को अनमोल करते हैं तो उचित थ्रेड्स में 'pthread_exit' को मुख्य और/या' pthread_join' में डाल दें। – 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

इस कार्यान्वयन (इस पोस्ट मदद से) मेरे लिए काम किया जैसे कुछ है, यह सीधा है:

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); 

आप मेरे कार्य कोड here