Round-Robin time-slice with POSIX

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#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; 

pthread_t myPthreads[NUM_PTHREADS]; 
pthread_mutex_t myMutex = PTHREAD_MUTEX_INITIALIZER; 
pthread_attr_t myAttr; 

using namespace std; 

void *Work (void *ptr){ 
   int i = (int) ptr; 

   pthread_mutex_lock(&myMutex); 

   cout << "\nPrinting PThread" << i << endl; 
   for(int j=1;j <= 6; j++){ 
      cout << j << " "; 
      sleep(1); 
   } 

   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_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); 
} 


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!
Topic archived. No new replies allowed.