I decided to simplify my other post, simplifying the code so that it will be easier for you to help me.
So, the objective of the code is to run 3 threads using only 1 core, so that i can use it to schedule tasks, and to measure computation and response times. Each one have different priorities so that there's always only 1 thread running.
Instead, the 3 threads run at the same time, so that I can conclude that the there are at least 3 cores being used by the program.
There's the code: #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include
#define SCHED SCHED_FIFO
#define NUM_THREADS 3
#include "func.h"
struct timespec start1;
int result_code;
void *ff1(void* arg){
u_int64_t diff;
struct timespec start, end;
struct sched_param param;
// Periority
param.sched_priority=sched_get_priority_max(SCHED_FIFO);
result_code = sched_setscheduler(0,SCHED,¶m);
assert(!result_code);
printf("Running Task 1\n");
clock_gettime(CLOCK_MONOTONIC, &start);
f1(1,5);
clock_gettime(CLOCK_MONOTONIC, &end);
diff = pow(10,9) * (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec);
printf("1st Task elapsed time = %ld ns => %f s\n", diff, diff / (pow(10,9)));
diff = pow(10,9) * (end.tv_sec - start1.tv_sec) + (end.tv_nsec - start1.tv_nsec);
printf("1st Task response time = %ld ns => %f s\n", diff, diff / (pow(10,9)));
printf("Task 1 finished\n");
}
void *ff2 (void* arg){
u_int64_t diff;
struct timespec start, end;
struct sched_param param;
// sched
param.sched_priority=sched_get_priority_max(SCHED_FIFO)-1;
result_code = sched_setscheduler(0,SCHED,¶m);
assert(!result_code);
printf("Running Task 2\n");
clock_gettime(CLOCK_MONOTONIC, &start);
f2(1,5);
clock_gettime(CLOCK_MONOTONIC, &end);
diff = pow(10,9) * (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec);
printf("2nd Task elapsed time = %ld ns => %f s\n", diff, diff / (pow(10,9)));
diff = pow(10,9) * (end.tv_sec - start1.tv_sec) + (end.tv_nsec - start1.tv_nsec);
printf("2nd Task response time = %ld ns => %f s\n", diff, diff / (pow(10,9)));
printf("Task 2 finished\n");
}
void *ff3 (void* arg){
u_int64_t diff;
struct timespec start, end;
struct sched_param param;
// sched
param.sched_priority=sched_get_priority_max(SCHED_FIFO)-2;
result_code = sched_setscheduler(0,SCHED,¶m);
assert(!result_code);
printf("Running Task 3\n");
clock_gettime(CLOCK_MONOTONIC, &start);
f3(1,5);
clock_gettime(CLOCK_MONOTONIC, &end);
diff = pow(10,9) * (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec);
printf("3rd Task elapsed time = %ld ns => %f s\n", diff, diff / (pow(10,9)));
// Cálculo do tempo de resposta da tarefa 2
diff = pow(10,9) * (end.tv_sec - start1.tv_sec) + (end.tv_nsec - start1.tv_nsec);
printf("3rd Task response time = %ld ns => %f s\n", diff, diff / (pow(10,9)));
printf("Task 3 finished\n");
}
int main(){
pthread_t threads[3];
short i;
// Affinity
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(0,&mask);
result_code = sched_setaffinity(0, sizeof(cpu_set_t), &mask);
assert(!result_code);
pthread_attr_t attr[3];
// Start time
clock_gettime(CLOCK_MONOTONIC, &start1);
for (i = 0; i < 3; i++){
result_code = pthread_attr_init(&attr[i]);
assert(!result_code);
result_code = pthread_attr_setaffinity_np(&attr[i], sizeof(cpu_set_t), &mask);
assert(!result_code);
}
result_code=pthread_create(&threads[0],&attr[0],&ff1,NULL);
assert(!result_code);
result_code=pthread_create(&threads[1],&attr[1],&ff2,NULL);
assert(!result_code);
result_code=pthread_create(&threads[2],&attr[2],&ff3,NULL);
assert(!result_code);
//wait for each thread to complete
for(i=0;i<NUM_THREADS;i++){
result_code=pthread_join(threads[i],NULL);
assert(!result_code);
}
printf("Main -> Finished\n");
return(EXIT_SUCCESS);
}
The file func.h got the followed functions:
f1(int, int);
f2(int, int);
f3(int, int);
The object file was given by the teacher so all i can do is to upload it for you guys to download. The functions only do some tasks for some miliseconds.
func.o: https://ufile.io/uzhwf Note: I had an previous post that was too confusing (sorry, first time posting on Stackoverflow). Thanks everyone that spent some time trying to help me.
UPDATE: Updated the code and uploaded func.o. UPDATE1: Added some error checks and changed the sched_priority using sched_get_priority_max. Now it i get a "core dumped" in ff3 (function)'s sched_setscheduler.