-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.c
80 lines (65 loc) · 1.8 KB
/
scheduler.c
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
76
77
78
79
#include "scheduler.h"
//variables
unsigned timeInst; // global to synchronize time accross tasks
unsigned dataCollectionCtr; // global used to count robots processed
volatile pth_t pt[MAX_THREADS]; // this is volatile to not let the compiler assume anything about the value
/**************************************************
*
* Scheduler
*
***************************************************/
int scheduler(void)
{
long numthreads;
int pth_ret;
int i;
timeInst = 0; // beggining at time 0
dataCollectionCtr = 0;
pth_ret = pth_init();
if (pth_ret != TRUE)
{
fprintf(stderr, "Could not initialize GNU Pth, exiting.\n");
return(EXIT_FAILURE);
}
pt[0] = pth_self();
while (1)
{
printf("yielding\n");
pt[1] = pth_spawn(PTH_ATTR_DEFAULT, mcastSubscribe, NULL);
pt[2] = pth_spawn(PTH_ATTR_DEFAULT, mcastUnSubscribe, NULL);
pt[3] = pth_spawn(PTH_ATTR_DEFAULT, receiveData, NULL);
pt[4] = pth_spawn(PTH_ATTR_DEFAULT, dataSelection, NULL);
pt[5] = pth_spawn(PTH_ATTR_DEFAULT, dataProcessing, NULL);
pt[6] = pth_spawn(PTH_ATTR_DEFAULT, decisionMaking, NULL);
pt[7] = pth_spawn(PTH_ATTR_DEFAULT, sendData, NULL);
for(i =1; i< MAX_THREADS; i++)
{
pth_yield(pt[i]);
}
if(timeInst >=61) {return;}
timeInst++;// inc time to proceed to next batch of data
}
return(EXIT_SUCCESS);
}
// thread examples
// threads always yield to pt[0] as it is our scheduler
void *thread_1(void *ptr)
{
int i;
for (i = 0; i < 10; i++) {
printf("thread1\n ");
pth_yield(pt[0]);
}
pth_exit(NULL);
return(NULL);
}
void *thread_2(void *ptr)
{
int i;
for (i = 0; i < 10; i++) {
printf("thread2\n ");
pth_yield(pt[0]);
}
pth_exit(NULL);
return(NULL);
}