Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. /**
  2. * @file main.c
  3. * @brief race-condition que recorre a 200 threads para incrementar 1000 vezes um contador partilhado.
  4. * @date 2019-10-14
  5. * @srico
  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. #include "args.h"
  17.  
  18. #include "debug.h"
  19. #include "memory.h"
  20.  
  21. #define C_ERRO_PTHREAD_CREATE 1
  22. #define C_ERRO_PTHREAD_JOIN 2
  23. #define C_ERRO_MUTEX_INIT 3
  24. #define C_ERRO_MUTEX_DESTROY 4
  25.  
  26. int G_shared_counter; //variavel global
  27.  
  28. // Estrutura a 'passar' às threads
  29. typedef struct
  30. {
  31. int incremento;
  32. pthread_mutex_t mutex;
  33. }thread_params_t;
  34.  
  35. void *soma(void *arg);
  36.  
  37. int main(int argc, char *argv[]){
  38. struct gengetopt_args_info args;
  39. thread_params_t thread_params;
  40. G_shared_counter=0;
  41.  
  42.  
  43.  
  44. if (cmdline_parser(argc,argv, &args))
  45. {
  46. ERROR(1,"erro no cmdline_parser()\n");
  47.  
  48. }
  49.  
  50. pthread_t tids[args.threads_arg]; //vetor com as threads
  51. thread_params.incremento=args.incremento_arg;
  52.  
  53. //------------iniciar o mutex------------
  54. if ( (errno = pthread_mutex_init(&thread_params.mutex, NULL)) != 0) {
  55. ERROR(C_ERRO_MUTEX_INIT, "pthread_mutex_init() failed");
  56. }
  57.  
  58. //criar as N threads
  59. for (int i = 0; i < args.threads_arg; ++i)
  60. {
  61. if ((errno = pthread_create(&tids[i], NULL, soma,&thread_params)) != 0)
  62. {
  63. ERROR(2, "pthread_create() failed!");
  64. }
  65. }
  66.  
  67. for (int i = 0; i < args.threads_arg; ++i)
  68. {
  69. if ((errno = pthread_join(tids[i], NULL) != 0))
  70. {
  71. ERROR(3, "pthread_join() failed!");
  72. }
  73. }
  74.  
  75. printf("G_shared_counter =%d (expecting %d)\n", G_shared_counter, args.threads_arg * args.incremento_arg);
  76.  
  77. //------------Destrói o mutex------------
  78. if ( (errno = pthread_mutex_destroy(&thread_params.mutex)) != 0) {
  79. ERROR(C_ERRO_MUTEX_DESTROY, "pthread_mutex_destroy() failed");
  80. }
  81.  
  82. cmdline_parser_free(&args);
  83. return 0;
  84. }
  85.  
  86. void *soma(void *arg){
  87. thread_params_t *thread_params = (thread_params_t*)arg;
  88.  
  89. int incremento = thread_params->incremento; //obtem campo da estrutura
  90. int local;
  91.  
  92. //------------lock------------
  93. if ( (errno = pthread_mutex_lock(&thread_params->mutex)) != 0) {
  94. ERROR(C_ERRO_MUTEX_INIT, "pthread_mutex_lock() failed");
  95. }
  96.  
  97. for (int i = 0; i < incremento; ++i)
  98. {
  99. local= G_shared_counter; //pomos na variavel global para aumentar a probabilidade da race condition
  100. sched_yield(); //baixa a prioridade da thread (para q seja a ultima a executar)
  101. local=local+1;
  102. G_shared_counter=local;
  103. }
  104.  
  105. if ( (errno = pthread_mutex_unlock(&thread_params->mutex)) != 0) {
  106. ERROR(C_ERRO_MUTEX_DESTROY, "pthread_mutex_unlock() failed");
  107. }
  108. //------------unlock------------
  109.  
  110. return NULL;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement