Guest User

Untitled

a guest
Nov 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. [3853,140124152944384] as
  2. [3853,140124152944392] gi
  3. [3853,140124152944388] yo
  4. [3853,140124152944384] as
  5. [3853,140124152944392] gi
  6. [3853,140124152944388] yo
  7.  
  8. #include <pthread.h>
  9. #include <stdio.h>
  10. #include "util.h"
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <sys/wait.h>
  14. #include <sys/types.h>
  15. #include <sys/ipc.h>
  16.  
  17. struct arg_holder {
  18. int reps;
  19. char * argv;
  20. int argcc;
  21. };
  22.  
  23.  
  24. pthread_mutex_t lock;
  25. pthread_cond_t cond;
  26. int q=2;
  27. int counter=0;
  28. void *disp(void *param) {
  29.  
  30.  
  31. struct arg_holder *my_arg;
  32. my_arg = (struct arg_holder *) param;
  33.  
  34. int myreps = my_arg->reps;
  35. char *myargv = my_arg->argv;
  36. int argccc = my_arg->argcc;
  37.  
  38. pthread_mutex_lock(&lock);
  39. init();
  40. counter++;
  41. pthread_mutex_unlock(&lock);
  42.  
  43.  
  44. while(counter==argccc-2){
  45.  
  46. pthread_cond_signal(&cond);
  47.  
  48. }
  49.  
  50.  
  51. for(int i=0;i<myreps;i++){
  52. pthread_mutex_lock(&lock);
  53. pthread_cond_wait(&cond,&lock);
  54. display(myargv);
  55. pthread_mutex_unlock(&lock);
  56. }
  57.  
  58. return NULL;
  59. }
  60.  
  61.  
  62. int main(int argc, char* argv[])
  63. {
  64. if(argc<3) {printf("Nopen"); exit(1); } // Segmentation Fault
  65. int rep = atoi(argv[1]); // Number of repetitions
  66.  
  67. if (pthread_mutex_init(&lock, NULL) != 0)
  68. {
  69. perror("n mutex init failedn");
  70. exit(1);
  71. }
  72.  
  73. if (pthread_cond_init(&cond, NULL) != 0) {
  74. perror("pthread_cond_init() error");
  75. exit(1);
  76. }
  77.  
  78. struct arg_holder thread_data_array[argc-2];
  79.  
  80.  
  81.  
  82. pthread_t threads[argc-2];
  83. for(int i = 0; i < argc-2; i++) {
  84.  
  85. thread_data_array[i].reps=rep;
  86. thread_data_array[i].argv=argv[q];
  87. thread_data_array[i].argcc=argc;
  88.  
  89.  
  90. int err = pthread_create(&threads[i], NULL, disp,(void *) &thread_data_array[i]);
  91. if (err != 0)
  92. printf("ncan't create thread :");
  93.  
  94. q++;
  95. }
  96.  
  97.  
  98. for(int i = 0; i < argc-2; i++) {
  99. pthread_join(threads[i], NULL);
  100. }
  101. pthread_mutex_destroy(&lock); // Destroy mutex
  102. pthread_cond_destroy(&cond); // Destroy cond variable
  103. return 0;
  104. }
Add Comment
Please, Sign In to add comment