2012-04-23 16 views
5

में छूट दी गई है, मैंने SCHED_FIFO का परीक्षण करने के लिए परीक्षण कार्यक्रम लिखा है। मैंने सीखा है कि SCHED_FIFO को SCHED_OTHER धागे से पूर्ववत नहीं किया जा सकता है। लेकिन जब मैं एक ही प्रोग्राम कई बार चलाता हूं तो मैं प्राप्त परिणामों की व्याख्या नहीं कर सका।SCHED_FIFO थ्रेड को SCHED_OTHER थ्रेड द्वारा लिनक्स

/* Includes */ 
#include <unistd.h>  /* Symbolic Constants */ 
#include <sys/types.h> /* Primitive System Data Types */ 
#include <errno.h>  /* Errors */ 
#include <stdio.h>  /* Input/Output */ 
#include <stdlib.h>  /* General Utilities */ 
#include <pthread.h> /* POSIX Threads */ 
#include <string.h>  /* String handling */ 
#include <sched.h> 
/* prototype for thread routine */ 
void print_message_function (void *ptr); 
void print_message_function1 (void *ptr); 

/* struct to hold data to be passed to a thread 
this shows how multiple data items can be passed to a thread */ 
typedef struct str_thdata 
{ 
int thread_no; 
int thread_value; 
char message[100]; 
    } thdata; 

int main() 
    { 
pthread_t thread1, thread2; /* thread variables */ 
thdata data1, data2;   /* structs to be passed to threads */ 

/* initialize data to pass to thread 1 */ 
data1.thread_no = 1; 
data1.thread_value = 0; 
strcpy(data1.message, "Hello!"); 

/* initialize data to pass to thread 2 */ 
data2.thread_no = 2; 
data2.thread_value = 10000; 
strcpy(data2.message, "Hi!"); 

/* create threads 1 and 2 */  
pthread_create (&thread1, NULL, (void *) &print_message_function, (void *) &data1); 
pthread_create (&thread2, NULL, (void *) &print_message_function1, (void *) &data2); 

/* Main block now waits for both threads to terminate, before it exits 
    If main block exits, both threads exit, even if the threads have not 
    finished their work */ 
pthread_join(thread1, NULL); 
pthread_join(thread2, NULL); 

/* exit */ 
exit(0); 
} /* main() */ 

/** 
* print_message_function is used as the start routine for the threads used 
* it accepts a void pointer 
**/ 
void print_message_function (void *ptr) 
{ 

thdata *data;    
data = (thdata *) ptr; /* type cast to a pointer to thdata */ 

struct sched_param param; 
//int priority=10; 
/* sched_priority will be the priority of the thread */ 
//param.sched_priority = priority; 
/* only supported policy, others will result in ENOTSUP */ 

int policy = SCHED_OTHER; 
/* scheduling parameters of target thread */ 
pthread_setschedparam(pthread_self(), policy, &param); 
printf("Thread %d says sched policy %d \n", data->thread_no, SCHED_OTHER); 
pthread_getschedparam(pthread_self(),&policy,&param); 

printf("Thread %d says %s %d \n", data->thread_no, data->message,policy); 

int i=0; 
/* do the work */ 
printf("Thread %d says %s %d \n", data->thread_no, data->message,(int)pthread_self()); 
for(i=0;i<100;i++) 

printf("Thread %d says %d \n", data->thread_no,data->thread_value++); 
pthread_exit(0); /* exit */ 
} /* print_message_function (void *ptr) */ 



void print_message_function1 (void *ptr) 
{ 

thdata *data;    
data = (thdata *) ptr; /* type cast to a pointer to thdata */ 

struct sched_param param; 
int priority=10; 
/* sched_priority will be the priority of the thread */ 
param.sched_priority = priority; 
/* only supported policy, others will result in ENOTSUP */

int policy = SCHED_FIFO; 
/* scheduling parameters of target thread */ 
pthread_setschedparam(pthread_self(), policy, &param); 
printf("Thread %d says sched policy %d \n", data->thread_no, SCHED_FIFO); 

pthread_getschedparam(pthread_self(),&policy,&param); 

printf("Thread %d says %s %d \n", data->thread_no, data->message,policy); 

int i=0; 
/* do the work */ 
printf("Thread %d says %s %d \n", data->thread_no, data->message,(int)pthread_self()); 
for(i=0;i<100;i++) 

printf("Thread %d says %d \n", data->thread_no,data->thread_value++); 
pthread_exit(0); /* exit */ 
} /* print_message_function (void *ptr) */ 

मैं मिल गया है कई रन में अप्रत्याशित परिणाम जहां मैं SCHED_FIFO देखा है SCHED_OTHER धागा द्वारा रोका जाता है, अर्थात प्रति कार्यक्रम के रूप में, धागा 2 FIFO मोड में है, जबकि धागा 1 SCHED_OTHER मोड है। मैंने कई बार देखा है जहां थ्रेड 2 को थ्रेड 1 द्वारा छूट दी गई है।

कोई समस्या को हल करने में मेरी मदद कर सकता है?

+0

'sysctl -a के आउटपुट। grep _rt'? – ninjalj

+0

@martinjames kernel.sched_rt_period_us = 1000000 kernel.sched_rt_runtime_us = 950000 – GoT

+0

मैं g ++ 4.6.1 – GoT

उत्तर

6

आप शायद जो मूलभूत मूल्यों हैं प्रभाव में इन sysctl सेटिंग रखते हैं:

kernel.sched_rt_period_us = 1000000 
kernel.sched_rt_runtime_us = 950000 

इसका मतलब यह है कि वास्तविक समय धागे हर 1 दूसरी अवधि के केवल 95% घेर की अनुमति है।

यह भी देखें: Can't provoke Priority Inversion in C++

+0

हाँ के साथ उबंटू 11.10 का उपयोग कर रहा हूं, ये मेरे डिफ़ॉल्ट मान हैं। लेकिन, मैंने रीयलटाइम को अक्षम करने के लिए -1 को kernel.sched_rt_runtime_us डालने का प्रयास किया। इसने – GoT

+4

पर काम नहीं किया है आपके पास कितने कोर हैं? आप कैसे सुनिश्चित कर रहे हैं कि धागे अलग-अलग कोर पर निष्पादित नहीं हो सकते हैं? यह भी ध्यान रखें कि आपको प्राथमिकता निर्धारित करनी है। POSIX कार्यों की एक जोड़ी 'sched_get_priority_max' और 'sched_get_priority_min' परिभाषित करता है जो नीति को तर्क के रूप में लेते हैं और आपको उस नीति के लिए वैध प्राथमिकताओं की सीमा देते हैं। – Kaz

+1

मैंने कमांड sudo sh -c "echo 0>/sys/devices/system/cpu/cpu3/online" का उपयोग करके सभी कोरों का उपयोग करके अक्षम कर दिया है .. चीजें ठीक हैं। धन्यवाद :-) – GoT

4

इसके अलावा जब भी आईओ पर अपने धागा ब्लॉक, printf बयानों से शायद, एक और धागा निर्धारित किया जा सकता है।

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^