Advertisement
Guest User

Untitled

a guest
May 30th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. //
  2. // main.c
  3. // TestVlad
  4. //
  5. // Created by Efraim Budusan on 30/05/16.
  6. // Copyright © 2016 Efraim Budusan. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <pthread.h>
  13. #include <string.h>
  14. #include <time.h>
  15. #include <unistd.h>
  16.  
  17. char text[500];
  18. int charToRemove = 0;
  19. int n;
  20.  
  21.  
  22.  
  23. pthread_mutex_t eliminate= PTHREAD_MUTEX_INITIALIZER;
  24. pthread_mutex_t mutexes[100];
  25.  
  26. void * popThread( void * arg)
  27. {
  28. int i = (int)(size_t)arg;
  29. while (1) {
  30.  
  31. pthread_mutex_lock(&eliminate);
  32. memmove(text, text+1, strlen(text));
  33. pthread_mutex_unlock(&eliminate);
  34. printf("%s \n",text);
  35. pthread_mutex_lock(&mutexes[i]);
  36.  
  37. }
  38. return NULL;
  39. }
  40.  
  41.  
  42. void * generatorThread()
  43. {
  44. while(1)
  45. {
  46. charToRemove = rand() % strlen(text);
  47. for (int i=0;i<n;i++) {
  48. pthread_mutex_unlock(&mutexes[i]);
  49. }
  50. sleep(2);
  51. }
  52. return NULL;
  53. }
  54.  
  55. int main(int argc, const char * argv[]) {
  56. // insert code here...
  57.  
  58. srand(time(0));
  59. printf("Enter the text:");
  60. fgets(text, sizeof(text), stdin);
  61.  
  62. printf("Enter the no of threads:");
  63. scanf("%d",&n);
  64.  
  65. pthread_t th[n];
  66. pthread_t gThread;
  67. pthread_create(&gThread, NULL, &generatorThread , NULL);
  68. for (int i=1;i<=n;i++)
  69. {
  70. pthread_mutex_init(&mutexes[i],NULL);
  71. pthread_mutex_unlock(&mutexes[i]);
  72. pthread_create(&th[i], NULL, &popThread, (void*)(size_t)i );
  73. }
  74.  
  75. pthread_join(gThread , NULL);
  76. for (int i=1;i<=n;i++)
  77. {
  78. pthread_join(th[i] , NULL);
  79. }
  80.  
  81. return 0;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement