Hi everyone!
I’m trying to change the time-slice (quantum) of the Round-Robin schedule with POSIX patterns.
I have already try to use this:
- sched_rr_get_interval(0, &timeTrigger);
Where: - 0 is the current process;
- &timeTrigger is a reference to a timespec configured with 2 seconds.
Here’s going the code:
#include "RoundRobin.h"
#include <pthread.h>
#include <sched.h>
#include <cstdlib>
#include <iostream>
#include <sys/time.h>
#include <unistd.h>
#define NUM_PTHREADS 2
#define MICRO_PER_SECOND 1000000
struct timeval now, start, stop;
struct timespec timeTrigger, timeCondition;
int fire=1;
pthread_t myPthreads[NUM_PTHREADS];
pthread_mutex_t myMutex = PTHREAD_MUTEX_INITIALIZER;
//pthread_cond_t myCond = PTHREAD_COND_INITIALIZER;
pthread_attr_t myAttr;
using namespace std;
void *Work (void *ptr){
int i = (int) ptr;
pthread_mutex_lock(&myMutex);
// timeCondition.tv_sec = now.tv_sec + fire;
// pthread_cond_timedwait(&myCond, &myMutex, &timeCondition);
cout << "\nPrinting PThread" << i << endl;
for(int j=1;j <= 6; j++){
cout << j << " ";
sleep(1);
}
// fire += 5;
pthread_mutex_unlock(&myMutex);
return NULL;
}
RoundRobin::RoundRobin() {
// TODO Auto-generated constructor stub
}
RoundRobin::~RoundRobin() {
// TODO Auto-generated destructor stub
}
void RoundRobin::CriaPThreads(){
int resultOfCreatePthread=0;
gettimeofday(&now, NULL);
timeTrigger.tv_sec = now.tv_sec + 2;
pthread_cond_init(&myCond, NULL);
pthread_attr_init(&myAttr);
pthread_attr_setschedpolicy(&myAttr, SCHED_RR);
// pthread_attr_setscope(&myAttr, PTHREAD_SCOPE_PROCESS);
pthread_attr_setinheritsched(&myAttr, PTHREAD_EXPLICIT_SCHED);
sched_rr_get_interval(getpid(), &timeTrigger);
for (int i=1; i <= NUM_PTHREADS; i++){
resultOfCreatePthread = pthread_create(&myPthreads[i], &myAttr,
Work, (void *)i);
if (resultOfCreatePthread != 0)
cout << "PThread " << i << ": Impossible to create." << endl;
}
}
void RoundRobin::AguardaFinalizacaoPthread(){
for (int i=1; i <= NUM_PTHREADS; i++)
pthread_join(myPthreads[i], NULL);
pthread_attr_destroy(&myAttr);
pthread_mutex_destroy(&myMutex);
// pthread_cond_destroy(&myCond);
}
I wanna set some pthreads and when the first pthread reach the timeslice (or quantum) the second pthread take the CPU and the first one go to the end of the queue.
I already now that it’s just like the Round-Robin schedule.
But I need some way to do this with POSIX.
Thanks in advance!