Im folgenden Code-Snippet erstelle ich 6 Threads. Jeder mit unterschiedlichen Prioritäten. Die Priorität wird im globalen Prioritätsfeld erwähnt. Ich mache eine kontinuierliche Erhöhung der globalen Variablen in jedem Thread basierend auf Thread-Index. Ich habe erwartet, dass die Zählung höher ist, wenn die Thread-Priorität höher ist. aber mein Output entspricht nicht den Prioritätskonzepten pl. Beziehen Sie sich auf die unten gezeigte Reihenfolge. Ich probiere das auf Ubuntu 16.04 und Linux Kernel 4.10 aus.Linux Thread-Priorität, Verhalten ist abnormal
O/P,
Thread=0
Thread=3
Thread=2
Thread=5
Thread=1
Thread=4
pid=32155 count=4522138740
pid=32155 count=4509082289
pid=32155 count=4535088439
pid=32155 count=4517943246
pid=32155 count=4522643905
pid=32155 count=4519640181
Code:
#include <stdio.h>
#include <pthread.h>
#define FAILURE -1
#define MAX_THREADS 15
long int global_count[MAX_THREADS];
/* priority of each thread */
long int priority[]={1,20,40,60,80,99};
void clearGlobalCounts()
{
int i=0;
for(i=0;i<MAX_THREADS;i++)
global_count[i]=0;
}
/**
thread parameter is thread index
**/
void funcDoNothing(void *threadArgument)
{
int count=0;
int index = *((int *)threadArgument);
printf("Thread=%d\n",index);
clearGlobalCounts();
while(1)
{
count++;
if(count==100)
{
global_count[index]++;
count=0;
}
}
}
int main()
{
int i=0;
for(int i=0;i<sizeof(priority)/sizeof(long int);i++)
create_thread(funcDoNothing, i,priority[i]);
sleep(3600);
for(i=0;i<sizeof(priority)/sizeof(long int);i++)
{
printf("pid=%d count=%ld\n",getpid(),
global_count[i]);
}
}
create_thread(void *func,int thread_index,int priority)
{
pthread_attr_t attr;
struct sched_param schedParam;
void *pParm=NULL;
int id;
int * index = malloc(sizeof(int));
*index = thread_index;
void *res;
/* Initialize the thread attributes */
if (pthread_attr_init(&attr))
{
printf("Failed to initialize thread attrs\n");
return FAILURE;
}
if(pthread_attr_setschedpolicy(&attr, SCHED_FIFO))
{
printf("Failed to pthread_attr_setschedpolicy\n");
return FAILURE;
}
if (pthread_attr_setschedpolicy(&attr, SCHED_FIFO))
{
printf("Failed to setschedpolicy\n");
return FAILURE;
}
/* Set the capture thread priority */
pthread_attr_getschedparam(&attr, &schedParam);;
schedParam.sched_priority = sched_get_priority_max(SCHED_FIFO) - 1;
schedParam.sched_priority = priority;
if (pthread_attr_setschedparam(&attr, &schedParam))
{
printf("Failed to setschedparam\n");
return FAILURE;
}
pthread_create(&id, &attr, (void *)func, index);
}
danke für die Eingabe, das Verhalten scheint viel besser zu sein. Ich habe alle Warnungen korrigiert. 3600 war experimentell, um sicherzustellen, dass die anfängliche Planungslatenz die Anzahl nicht beeinflusst. o/p ist nun, pid = 31068 count = 0 pid = 31068 count = 15 pid = 31068 count = 56961864 pid = 31068 count = 56964895 pid = 31068 count = 57002126 pid = 31068 count = 56.688.136 immer noch höchste Priorität Aufgabenverhalten ist nicht so konsistent. Ich habe es mit 30sec Schlaf versucht, ich werde versuchen mit höherer Verzögerung. Jetzt habe ich Programm als root ausgeführt, warum ist diese Einschränkung? –