Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. #include <ctype.h>
  5. #include <ncurses.h>
  6. #include <sys/time.h>
  7.  
  8.  
  9. #define NMAX 100
  10.  
  11. #define STATESX 4 //number of states
  12. #define EVENTSX 3 //number of distinct events
  13.  
  14. #define STATE1 0x01 //event 0
  15. #define STATE2 0x02 //event 1
  16.  
  17. #define TYPE_S 0x01 //event 0
  18. #define TYPE_N 0x02 //event 1
  19. #define TYPE_OTHER 0x04 //event 2
  20.  
  21. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  22. volatile int count = 0; //global variables used by threads must be volatile
  23. volatile char events2[NMAX];
  24. volatile unsigned int events,state,state_events;
  25. volatile clock_t cook_time, current_tick,next_tick;
  26. volatile int keep_warm, key,fcount,scount ;
  27. volatile unsigned int tps, target_count = 0;
  28.  
  29. volatile int l = 0;
  30. volatile int h = 0;
  31. volatile int m = 0;
  32. volatile int s = 0;
  33.  
  34. void null()
  35. {
  36. // printf("null");
  37. }
  38.  
  39. void increase()
  40. {
  41. l++;
  42. printf("zwiekszono l");
  43. }
  44.  
  45. void increaseH()
  46. {
  47. h++;
  48. if(h>=24)
  49. h = 0;
  50. printf("zwiekszono h, %d", h);
  51. }
  52.  
  53. void increaseM()
  54. {
  55. m++;
  56. if(m>=60){
  57. m = 0;
  58. increaseH();
  59. }
  60. printf("zwiekszono m, %d", m);
  61. }
  62.  
  63. void increaseS()
  64. {
  65. s++;
  66. if(s>=60){
  67. s = 0;
  68. increaseM();
  69. }
  70. printf("zwiekszono s, %d", s);
  71. }
  72.  
  73. void output()
  74. {
  75. printf("h:m:s = %d:%d:%d", h,m,s);
  76. }
  77.  
  78. typedef void (*TPT)();
  79.  
  80. struct state
  81. {
  82. unsigned int active;
  83. unsigned int next_state;
  84. TPT action;
  85. };
  86. struct state state_table[EVENTSX][STATESX] = {
  87. //INPUTH //OUTPUT //INPUTM //INPUTS //Events
  88. { {1,2,null}, {1,0,null}, {1,3,null}, {1,1,null}}, //0 TYPE_S
  89. { {1,0,increaseH}, {1,1,output}, {1,2,increaseM}, {1,3,increaseS}}, //1 TYPE_N
  90. { {1,0,null}, {1,1,output}, {1,2,null}, {1,3,null}} //2 TYPE_OTHER
  91. };
  92.  
  93. unsigned int getmask(unsigned int staten)
  94. {
  95. unsigned int mask = 0;
  96. int i;
  97. for(i = EVENTSX-1;i>=0;i--)
  98. {
  99. mask = mask << 1;
  100. mask |= state_table[i][staten].active;
  101. }
  102. return mask;
  103. }
  104.  
  105. /*
  106. geteventn, using the current atate's event mask to select
  107. only relevant events it then identifies the active event
  108. with the highest priority, returning its number
  109. */
  110. unsigned int getevenyn()
  111. {
  112. unsigned int i,bitmask = 1;
  113. state_events = events & getmask(state);
  114. for(i = 0;i<EVENTSX;i++)
  115. {
  116. if (state_events & bitmask) //mask out irrelevant
  117. { //events
  118. events &= ~bitmask; //cancel selected event flag
  119. return i; //returns event with the highest priority
  120. }
  121. bitmask = bitmask << 1;
  122. }
  123. return ~0;
  124. }
  125.  
  126. void sort(void) //sort events - brute force solution
  127. {
  128. char dum;
  129. for(int i=0;i<count-1;i++)
  130. for(int j=i+1;j<count;j++)
  131. {
  132. if (events2[j]>events2[i])
  133. {
  134. dum = events2[j];
  135. events2[j]=events2[i];
  136. events2[i]=dum;
  137. }
  138. }
  139. }
  140.  
  141. void* function1( void* arg )
  142. {
  143. char ch,ch1;
  144.  
  145. while( 1 ) {
  146. s++;
  147.  
  148. if(s>=60) {
  149. s = 0;
  150. m++;
  151.  
  152. if(m>=60) {
  153. m = 0;
  154. h++;
  155.  
  156. if(h>=24) {
  157. h=0;
  158. }
  159. }
  160. }
  161. sleep(1);
  162. }
  163. return 0;
  164. }
  165.  
  166. void* function2( void* arg )
  167. {
  168. while( 1 ) {
  169. fprintf( stderr,"Count is %d\n", count );
  170. for(int i=0;i<count;i++)
  171. {
  172. fprintf(stderr,"%d %c, ",i,events2[i]);
  173. }
  174. fprintf(stderr,"\n");
  175. if (count>0)
  176. {
  177. pthread_mutex_lock( &mutex );
  178. for(int i=0;i<count-1;i++)
  179. events2[i] = events2[i+1]; //remove data from the head of events
  180. count--;
  181. pthread_mutex_unlock( &mutex );
  182. }
  183. sleep(5);
  184. }
  185.  
  186. return 0;
  187. }
  188.  
  189. int main( void )
  190. {
  191. pthread_create( NULL, NULL, &function1, NULL );
  192. // pthread_create( NULL, NULL, &function2, NULL );
  193.  
  194. unsigned int last_state, event, last_event = 0;
  195.  
  196. scount = 0;
  197. fcount = 0;
  198. state = 1;
  199.  
  200. while(1)
  201. {
  202. scount++;
  203.  
  204. key = 0;
  205. if ((key=getchar()) != -1 && key != '\n') //slow laundry - set event flags from slowly arriving data
  206. {
  207. if (key == 's') events |= TYPE_S;
  208. else if (key == 'n') events |= TYPE_N;
  209. else {
  210. events |= TYPE_OTHER;
  211. }
  212. }
  213.  
  214. if ( (event=getevenyn()) != ~0) //periodic dispatcher
  215. {
  216. last_event = event;
  217. last_state = state;
  218. (*(state_table[event][state].action))(); //transition ACTION
  219. state = state_table[event][state].next_state; //go to next state
  220. if (state != last_state) events = 0; //clear event flags if change of states
  221. }
  222.  
  223. //fprintf(stderr,"last_event: %d ,last_state: %d, ",last_event,last_state);
  224. }
  225.  
  226. return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement