下記のコードはスレッドFuncAとFuncBの内部ループを10000000回
実行して、その時間を測定表示するものです。
尚、FuncAとFuncBのプライオリティはそれぞれ、99と1です。
Q1)FuncAとFuncBの実行時間は、共に約1.7秒位で変わりがありません。
つまり、プライオリティの差が出ないようです。
この件に関しまして、コメント頂けますと大変有りがたいです。
...
..#include <pthread.h>
..#include <stdio.h>
..#include <stdlib.h>
..#include <unistd.h>
..#include <errno.h>
..#include <string.h>
..#include <time.h>
..#include <math.h>
..#define REPEAT 10000000
void *FuncA(void *arg);
void *FuncB(void *arg);
int main(){
pthread_attr_t *tattrA;
pthread_attr_t *tattrB;
/*
do{
if( (tattr=(pthread_attr_t *)malloc(sizeof(pthread_attr_t)) )==NULL){
printf("Couldn't allocate memory for attribute object\n");
}
} while(tattr==NULL);
*/
tattrA=(pthread_attr_t *)malloc(sizeof(pthread_attr_t)); tattrB=(pthread_attr_t *)malloc(sizeof(pthread_attr_t)); pthread_t tidA; pthread_t tidB; struct sched_param paramA; struct sched_param paramB; int pr,error,i; int policy=SCHED_FIFO;//1 if(error=pthread_attr_init(tattrA)){ printf("A:Attribute initialization failed with error %s\n",strerror(error)); } if(error=pthread_attr_init(tattrB)){ printf("B:Attribute initialization failed with error %s\n",strerror(error)); } //pthread_attr_setschedpolicy error = pthread_attr_setschedpolicy(tattrA, policy); if (error != 0) printf("A:failed to set policy\n"); //================= error = pthread_attr_setschedpolicy(tattrB, policy); if (error != 0) printf("B:failed to set policy\n"); scanf("%d",&pr); paramA.sched_priority=pr; error=pthread_attr_setschedparam(tattrA,¶mA); if(error!=0) printf("A:failed to set priority\n"); //=========== paramB.sched_priority=1; error=pthread_attr_setschedparam(tattrB,¶mB); if(error!=0) printf("B:failed to set priority\n"); pthread_create(&tidA,tattrA,FuncA,tattrA); pthread_create(&tidB,tattrB,FuncB,tattrB); pthread_join(tidA,NULL); pthread_join(tidB,NULL); printf("pthread_join was done\n"); free(tattrA); free(tattrB); printf("All threads terminated\n"); return 0;
}
void *FuncA(void *arg){
printf("FunA is running\n");
pthread_attr_t *tattr=(pthread_attr_t *)arg;
int state,error;
int policy; struct sched_param param; //display_thread_sched_attr("=====get plicy and priority==="); error=pthread_attr_getdetachstate(tattr,&state); if(error==0 && state==PTHREAD_CREATE_DETACHED){ printf(" My state:A is DETACHED\n"); } else if(error==0 && state==PTHREAD_CREATE_JOINABLE){ printf(" My state:A is JOINABLE\n"); }
//error=pthread_attr_getschedpolicy(tattr,¶m);
error=pthread_attr_getschedpolicy(tattr,&policy);
if(error==0){
printf(" My Policy:A is %d\n",policy);
}
error=pthread_attr_getschedparam(tattr,¶m);
if(error==0){ printf(" My Priority:A is %d\n",param.sched_priority); }
//=============
clock_t c1,c2;
c1 = clock();
//=============
double dwk;
for(int i=0; i<REPEAT; i++){
dwk=sin((double)i);
}
//=================
c2 = clock();
printf("time:A = %f[s]\n", (double)(c2-c1)/CLOCKS_PER_SEC);
//=================
fflush(stdout);
printf("FunA is finished\n"); return NULL;
}
void *FuncB(void *arg){
printf("FunB is running\n");
pthread_attr_t *tattr=(pthread_attr_t *)arg;
int state,error;
int policy; struct sched_param param; //display_thread_sched_attr("=====get plicy and priority==="); error=pthread_attr_getdetachstate(tattr,&state); if(error==0 && state==PTHREAD_CREATE_DETACHED){ printf(" My state:B is DETACHED\n"); } else if(error==0 && state==PTHREAD_CREATE_JOINABLE){ printf(" My state:B is JOINABLE\n"); }
//error=pthread_attr_getschedpolicy(tattr,¶m);
error=pthread_attr_getschedpolicy(tattr,&policy);
if(error==0){
printf(" My Policy:B is %d\n",policy);
}
error=pthread_attr_getschedparam(tattr,¶m);
if(error==0){ printf(" My Priority:B is %d\n",param.sched_priority); }
//=============
clock_t c1,c2;
c1 = clock();
//=============
double dwk;
for(int i=0; i<REPEAT; i++){
dwk=sin((double)i);
}
//=================
c2 = clock();
printf("time:B = %f[s]\n", (double)(c2-c1)/CLOCKS_PER_SEC);
//=================
fflush(stdout);
printf("FunB is finished\n"); return NULL;
}
...
以上宜しくお願いします。
回答2件
あなたの回答
tips
プレビュー