Advertisement
Guest User

Untitled

a guest
May 27th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <pthread.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <sys/shm.h>
  8. #include <sys/ipc.h>
  9. #include <unistd.h>
  10. #include <semaphore.h>
  11. #include <sys/time.h>
  12.  
  13. char tab[10];
  14. sem_t mtx;
  15.  
  16. void randomize()
  17. {
  18. int i;
  19. for(i=0;i<10;i++)
  20. {
  21. tab[i] = (char) (rand() % 26) + 97;
  22. }
  23. }
  24.  
  25.  
  26. void printab(char* tab)
  27. {
  28. int i;
  29. for(i = 0; i < 10; i++)
  30. {
  31. printf("%c ", tab[i]);
  32. }
  33. }
  34.  
  35.  
  36. void Sorted()
  37. {
  38. int i;
  39. for (i=0; i<9; i++)
  40. {
  41. if (tab[i] > tab[i+1])
  42. {
  43. break ;
  44. }
  45. }
  46. if (i < 9)
  47. {
  48. printf("[X]");
  49. }
  50. else
  51. {
  52. printf("[O]");
  53. }
  54. }
  55.  
  56.  
  57.  
  58. int compare(const void *x, const void *y)
  59. {
  60. return *(const char *)x - *(const char *)y;
  61. }
  62.  
  63.  
  64. void *ThreadFunc(void *pContext)
  65. {
  66. int tid = (intptr_t)pContext; //Thread ID
  67. printf("##### Thread #%d started...\n", tid + 1);
  68. struct timeval time1, time2;
  69. gettimeofday(&time1, NULL);
  70. sem_wait(&mtx );//check and decrement mtx value
  71. printf("[%d] --> ", tid + 1);
  72. randomize();
  73. qsort(tab, 10, 1, compare); //sort the tab
  74. printab(tab);
  75. Sorted(); //check is sorted
  76. sem_post(&mtx); //increment mtx
  77. gettimeofday(&time2, NULL);
  78. printf (" %d -> ",(int) (time1.tv_usec ));
  79. printf ("%d\n",(int) (time2.tv_usec ));
  80. printf ("Time spent %d\n",(int) (time2.tv_usec - time1.tv_usec));
  81. return NULL;
  82. }
  83.  
  84.  
  85. int main()
  86. {
  87. int i;
  88. struct timeval time1, time2;
  89. printf ("##### Thread: Start Time -> %d\n",(int) (time1.tv_usec));
  90. gettimeofday(&time1, NULL);
  91. pthread_t tab_thread[20];
  92. sem_init(&mtx, 0, 1 );
  93. for (i = 0; i < 20; ++i)
  94. {
  95. pthread_create(&(tab_thread[i]), NULL, ThreadFunc, (void *)(intptr_t)i);
  96. }
  97. for (i = 0; i < 20; ++i)
  98. {
  99. pthread_join(tab_thread[i], NULL);//wait and check
  100. }
  101. sem_destroy(&mtx); //libérer la mémoire
  102. gettimeofday(&time2, NULL);
  103. printf ("##### Thread: End Time -> %d\n",(int) (time2.tv_usec));
  104. return 0;
  105. }
  106.  
  107. 2. Output of the
  108. program:
  109. This program was made a simple programming environment (Ubuntu 14.04 on VirtualMachine) with
  110. VIM and clang. I chose to use Ubuntu on Virtual Machine, although I also have it by Dual Boot for practical
  111. reasons. I chose VIM and gcc as text editor and compiler because I am used to use them at my home
  112. university in France. The program is then compiled with a basic makefile with clang.
  113. The fuction « randomize » fills an array with random characters.
  114. The fuction « printab » print the array.
  115. The fuction « sorted » checks if the characters in the array are sorted (alphabetic order).
  116. The function « compar » compar two characters and returns the difference between the ASCII code
  117. value of the two characters. If the value is postive the characters are well sorted else the second character
  118. have to be place before the fist one in the array.
  119. The fuction « ThreadFunc » is the fucntion that each thread have to excute.
  120. « tid » is the thread ID. We have to use the varible « mtx » to manage the order of execution of each thread.
  121. Indeed we can fill the table with only one thread at a time. For that we use the semaphore « mtx » which is
  122. set at 1. « sem_wait » is used to to manage it. It will decrement it. If mtx = 1 the thread is executed and mtx is
  123. decremented, else the thread have to wait his turn. Then we use the function randomize to fill the table.
  124. « qsort » is a function which sort the array. It take as argument an array, the number of elements of that
  125. array, the size of each elements of that array and a function which compare the two characters. Then it prints
  126. the table and check if it is well sorted. The semaphore « mtx » is then decremented thanks to the function
  127. « sem_post ».
  128. In the main function, we inialize the semaphore with « sem_init ». Then we creat and initialize an
  129. array of thread. We fill the array with threads thanks to the function « pthred_creat » which creats threads.
  130. Then we go through the table with « pthread_join » function which wait a specific thread to terminate and
  131. check if it is terminate. We release the memory of « mtx ».
  132. For the time we I used function of a library ( time.h).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement