Advertisement
alexarcan

thread-uri_din_lab

Dec 9th, 2015
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include <time.h>
  4. int main()
  5. {
  6. FILE *f= fopen("info.txt","w");
  7. srand (time(NULL));
  8. int i;
  9. fprintf(f,"%d\n",100);
  10. for(i=0; i<100; i++){
  11. fprintf(f,"%d\n", rand()%100);
  12. }
  13. return 0;
  14. }
  15.  
  16.  
  17.  
  18. ----------------------
  19.  
  20. #include<stdlib.h>
  21. #include<stdio.h>
  22. #include<pthread.h>
  23.  
  24. int nr_total = 0, med = 0;
  25. int v[101],i = 0;
  26.  
  27. void *functie (void *threadid){
  28.  
  29. int k=0,k1=0,sum=0;
  30. long media=0;
  31. //long id = *((long *)threadid);
  32. long id = (long)threadid;
  33. //printf("id= %ld\n",id);
  34. if (id == 1)
  35. {
  36. k=0;
  37. k1=med;
  38. }
  39. else if(id == 2)
  40. {
  41. k=med+1;
  42. k1=2*med+1;
  43. }
  44. else if (id == 3)
  45. { k = 2*med+1;
  46. k1=nr_total;
  47. }
  48. for(i=k; i<=k1; i++){
  49. //printf("i=%d ; k=%d ; k1=%d ; v[i]=%d \n",i,k,k1,v[i]);
  50. sum+=v[i];}
  51. media=sum/(k1+1);
  52. //printf("Media este:%d\n",media);
  53. pthread_exit((void*)media);
  54. }
  55. int main(int arg, char *argv[])
  56. {
  57.  
  58. long suma=0,m=0;
  59. long id;
  60. pthread_t thread[4];
  61.  
  62. FILE *f = fopen(argv[1],"r");
  63. fscanf(f,"%d",&nr_total);
  64.  
  65. med=(int)nr_total/3;
  66.  
  67. for(i=0; i<nr_total; i++)
  68. fscanf(f,"%d",&v[i]);
  69. for(id=1; id<=3; id++){
  70. int rc = pthread_create(&thread[id], NULL, functie, (void*)id);
  71. if(rc){
  72. printf("Error!\n");
  73. exit(1);}
  74. }
  75. for(i=1;i<=3;i++){
  76. void *status;
  77. pthread_join(thread[i],&status);
  78. suma+=(long)status;
  79. }
  80. m=suma/3;
  81. printf("MEDIA = %ld\n",m);
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement