Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7.  
  8.  
  9. struct task_t {
  10. char* data;
  11. int start;
  12. int end;
  13. };
  14. void* fun(void* arg) {
  15. struct task_t* task = (struct task_t*)arg;
  16. for(int i = task->start; i <= task->end; i++){
  17. if(strncmp((task->data+i),"ABCACD",6) == 0)
  18. printf("%d\n",i);
  19. }
  20. }
  21. int main(int argc, char **argv){
  22.  
  23. srand(time(NULL));
  24. printf("Hello\n");
  25.  
  26. char tab[1000000] = {"0"};
  27. int x = 0;
  28. for(int i = 0; i < 1000000; i ++){
  29. x = rand()%4;
  30. switch(x){
  31. case 0: tab[i] = 'A';
  32. break;
  33. case 1: tab[i] = 'B';
  34. break;
  35. case 2: tab[i] = 'C';
  36. break;
  37. case 3: tab[i] = 'D';
  38. break;
  39. default: return 6;
  40. }
  41. }
  42.  
  43. struct task_t task1 = {tab, 0, 250000};
  44. struct task_t task2 = {tab, 249995, 500000};
  45. struct task_t task3 = {tab, 499995, 750000};
  46. struct task_t task4 = {tab, 749995, 999999};
  47. pthread_t th1, th2, th3, th4;
  48. pthread_create(&th1, NULL, fun, &task1);
  49. pthread_create(&th2, NULL, fun, &task2);
  50. pthread_create(&th3, NULL, fun, &task3);
  51. pthread_create(&th4, NULL, fun, &task4);
  52. pthread_join(th1, NULL);
  53. pthread_join(th2, NULL);
  54. pthread_join(th3, NULL);
  55. pthread_join(th4, NULL);
  56.  
  57.  
  58. printf("bye\n");
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement