Advertisement
Guest User

tostas

a guest
Jul 19th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. /**
  2. * @file main.c
  3. * @brief Description
  4. * @date 2018-1-1
  5. * @author name of author
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <errno.h>
  12. #include <string.h>
  13. #include <sys/types.h>
  14. #include <sys/wait.h>
  15. #include <pthread.h>
  16.  
  17. #include "debug.h"
  18. #include "memory.h"
  19. #include "args.h"
  20.  
  21. #define MODE_SLICE 0
  22. #define MODE_FIAMBRE 1
  23. #define MODE_QUEIJO_1 2
  24. #define MODE_FINALIZADOR 3
  25. #define MODE_TOSTAR 6
  26. #define MODE_QUEIJO_2 4
  27. #define MODE_MESA 10
  28.  
  29. #define ERR_THREADS 5
  30.  
  31. void *task(void *arg);
  32. struct gengetopt_args_info args;
  33. typedef struct
  34. {
  35. int mode; // slice, fiambre, etc..
  36. pthread_mutex_t mutex;
  37. pthread_cond_t cond;
  38. }shared_t;
  39.  
  40. void *slice(void *);
  41. void *fiambre(void *);
  42. void *queijo(void *);
  43. void *finalizador(void *);
  44.  
  45.  
  46.  
  47. int main(int argc, char *argv[]){
  48. /* Disable warnings */
  49. (void)argc; (void)argv;
  50.  
  51. if(cmdline_parser(argc, argv, &args))
  52. ERROR(1, "Erro: execução de cmdline_parser\n");
  53.  
  54. shared_t shared;
  55. shared.mode = MODE_SLICE;
  56.  
  57. if ((errno = pthread_mutex_init(&shared.mutex, NULL)) != 0) {
  58. ERROR(ERR_THREADS, "pthread_mutex_init() failed");
  59. }
  60. if ((errno = pthread_cond_init(&shared.cond, NULL)) != 0) {
  61. ERROR(ERR_THREADS, "pthread_cond_init() failed");
  62. }
  63.  
  64. pthread_t tid1,tid2,tid3,tid4;
  65.  
  66. if ((errno = pthread_create(&tid1, NULL, slice, &shared)) != 0) {
  67. ERROR(ERR_THREADS, "pthread_create() failed");
  68. }
  69. if ((errno = pthread_create(&tid2, NULL, fiambre, &shared)) != 0) {
  70. ERROR(ERR_THREADS, "pthread_create() failed");
  71. }
  72. if ((errno = pthread_create(&tid3, NULL, queijo, &shared)) != 0) {
  73. ERROR(ERR_THREADS, "pthread_create() failed");
  74. }
  75. if ((errno = pthread_create(&tid4, NULL, finalizador, &shared)) != 0) {
  76. ERROR(ERR_THREADS, "pthread_create() failed");
  77. }
  78.  
  79. if ((errno = pthread_join(tid1, NULL)) != 0) {
  80. ERROR(ERR_THREADS, "pthread_join() failed");
  81. }
  82. if ((errno = pthread_join(tid2, NULL)) != 0) {
  83. ERROR(ERR_THREADS, "pthread_join() failed");
  84. }
  85. if ((errno = pthread_join(tid3, NULL)) != 0) {
  86. ERROR(ERR_THREADS, "pthread_join() failed");
  87. }
  88. if ((errno = pthread_join(tid4, NULL)) != 0) {
  89. ERROR(ERR_THREADS, "pthread_join() failed");
  90. }
  91.  
  92. if ((errno = pthread_mutex_destroy(&shared.mutex)) != 0) {
  93. ERROR(ERR_THREADS, "pthread_mutex_destroy() failed");
  94. }
  95. if ((errno = pthread_cond_destroy(&shared.cond)) != 0) {
  96. ERROR(ERR_THREADS, "pthread_cond_destroy() failed");
  97. }
  98. cmdline_parser_free(&args);
  99.  
  100. return 0;
  101. }
  102. void *slice(void *arg)
  103. {
  104. shared_t * param = arg;
  105.  
  106. if ((errno = pthread_mutex_lock(&(param->mutex))) != 0)
  107. {
  108. WARNING("pthread_mutex_lock() failed\n");
  109. return NULL;
  110. }
  111.  
  112. if(param->mode!=MODE_SLICE)
  113. {
  114.  
  115. if ((errno = pthread_cond_wait(&(param->cond), &(param->mutex))) != 0)
  116. {
  117. WARNING("pthread_cond_wait() failed");
  118. return NULL;
  119. }
  120. }
  121. printf("Bread Sliced!!\n");
  122. param->mode = MODE_QUEIJO_1;
  123.  
  124.  
  125.  
  126. if ((errno = pthread_cond_signal(&(param->cond))) != 0)
  127. {
  128. WARNING("pthread_cond_signal() failed");
  129. return NULL;
  130. }
  131.  
  132. if ((errno = pthread_mutex_unlock(&(param->mutex))) != 0)
  133. {
  134. WARNING("pthread_mutex_unlock() failed");
  135. return NULL;
  136. }
  137. return NULL;
  138.  
  139. }
  140. void *fiambre(void *arg)
  141. {
  142. shared_t * param = arg;
  143.  
  144. if ((errno = pthread_mutex_lock(&(param->mutex))) != 0)
  145. {
  146. WARNING("pthread_mutex_lock() failed\n");
  147. return NULL;
  148. }
  149.  
  150. while(param->mode!=MODE_FIAMBRE )
  151. {
  152. if ((errno = pthread_cond_wait(&(param->cond), &(param->mutex))) != 0)
  153. {
  154. WARNING("pthread_cond_wait() failed");
  155. return NULL;
  156. }
  157.  
  158. printf("Putting ham slice!!\n");
  159. param->mode = MODE_QUEIJO_2;
  160.  
  161.  
  162. if ((errno = pthread_cond_broadcast(&(param->cond))) != 0)
  163. {
  164. WARNING("pthread_cond_signal() failed");
  165. return NULL;
  166. }
  167.  
  168. if ((errno = pthread_mutex_unlock(&(param->mutex))) != 0)
  169. {
  170. WARNING("pthread_mutex_unlock() failed");
  171. return NULL;
  172. }
  173.  
  174. }
  175. return NULL;
  176. }
  177.  
  178. void *queijo(void *arg)
  179. {
  180. shared_t * param = arg;
  181.  
  182. if ((errno = pthread_mutex_lock(&(param->mutex))) != 0)
  183. {
  184. WARNING("pthread_mutex_lock() failed\n");
  185. return NULL;
  186. }
  187. if (param->mode==MODE_QUEIJO_1)
  188. {
  189. /* code */
  190. while(param->mode!=MODE_QUEIJO_1){
  191. if((errno = pthread_cond_wait(&(param->cond), &(param->mutex))) != 0)
  192. {
  193. WARNING("pthread_cond_wait() failed");
  194. return NULL;
  195. }
  196.  
  197.  
  198. printf("Putting cheese slice #1\n");
  199. param->mode=MODE_FIAMBRE;
  200.  
  201. pthread_cond_broadcast(&(param->cond));
  202.  
  203.  
  204. if ((errno = pthread_mutex_unlock(&(param->mutex))) != 0)
  205. {
  206. WARNING("pthread_mutex_unlock() failed");
  207. return NULL;
  208. }
  209. }
  210.  
  211. }else if(param->mode==MODE_QUEIJO_2){
  212.  
  213. while(param->mode!=MODE_QUEIJO_2){
  214. if((errno = pthread_cond_wait(&(param->cond), &(param->mutex))) != 0)
  215. {
  216. WARNING("pthread_cond_wait() failed");
  217. return NULL;
  218. }
  219.  
  220.  
  221. printf("Putting cheese slice #2\n");
  222. param->mode=MODE_FINALIZADOR;
  223.  
  224. pthread_cond_broadcast(&(param->cond));
  225.  
  226.  
  227. if ((errno = pthread_mutex_unlock(&(param->mutex))) != 0)
  228. {
  229. WARNING("pthread_mutex_unlock() failed");
  230. return NULL;
  231. }
  232. }
  233. }
  234.  
  235. return NULL;
  236. }
  237.  
  238. void *finalizador(void *arg)
  239. {
  240. shared_t * param = arg;
  241.  
  242.  
  243. if ((errno = pthread_mutex_lock(&(param->mutex))) != 0)
  244. {
  245. WARNING("pthread_mutex_lock() failed\n");
  246. return NULL;
  247. }
  248.  
  249. if(param->mode!=MODE_FINALIZADOR || param->mode!=MODE_TOSTAR )
  250. {
  251. if ((errno = pthread_cond_wait(&(param->cond), &(param->mutex))) != 0)
  252. {
  253. WARNING("pthread_cond_wait() failed");
  254. return NULL;
  255. }
  256. }
  257. else if (param->mode==MODE_TOSTAR)
  258. {
  259. printf("Toasting!!\n");
  260. param->mode=MODE_FIAMBRE;
  261. }
  262. else if (param->mode==MODE_FINALIZADOR)
  263. {
  264. printf("A tosta está pronta\n");
  265. param->mode=MODE_SLICE;
  266. sleep(3);
  267. }
  268.  
  269. if ((errno = pthread_cond_signal(&(param->cond))) != 0)
  270. {
  271. WARNING("pthread_cond_signal() failed");
  272. return NULL;
  273. }
  274.  
  275. if ((errno = pthread_mutex_unlock(&(param->mutex))) != 0)
  276. {
  277. WARNING("pthread_mutex_unlock() failed");
  278. return NULL;
  279. }
  280.  
  281. return NULL;
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement