Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<stdio.h>
- #include <time.h>
- int main()
- {
- FILE *f= fopen("info.txt","w");
- srand (time(NULL));
- int i;
- fprintf(f,"%d\n",100);
- for(i=0; i<100; i++){
- fprintf(f,"%d\n", rand()%100);
- }
- return 0;
- }
- ----------------------
- #include<stdlib.h>
- #include<stdio.h>
- #include<pthread.h>
- int nr_total = 0, med = 0;
- int v[101],i = 0;
- void *functie (void *threadid){
- int k=0,k1=0,sum=0;
- long media=0;
- //long id = *((long *)threadid);
- long id = (long)threadid;
- //printf("id= %ld\n",id);
- if (id == 1)
- {
- k=0;
- k1=med;
- }
- else if(id == 2)
- {
- k=med+1;
- k1=2*med+1;
- }
- else if (id == 3)
- { k = 2*med+1;
- k1=nr_total;
- }
- for(i=k; i<=k1; i++){
- //printf("i=%d ; k=%d ; k1=%d ; v[i]=%d \n",i,k,k1,v[i]);
- sum+=v[i];}
- media=sum/(k1+1);
- //printf("Media este:%d\n",media);
- pthread_exit((void*)media);
- }
- int main(int arg, char *argv[])
- {
- long suma=0,m=0;
- long id;
- pthread_t thread[4];
- FILE *f = fopen(argv[1],"r");
- fscanf(f,"%d",&nr_total);
- med=(int)nr_total/3;
- for(i=0; i<nr_total; i++)
- fscanf(f,"%d",&v[i]);
- for(id=1; id<=3; id++){
- int rc = pthread_create(&thread[id], NULL, functie, (void*)id);
- if(rc){
- printf("Error!\n");
- exit(1);}
- }
- for(i=1;i<=3;i++){
- void *status;
- pthread_join(thread[i],&status);
- suma+=(long)status;
- }
- m=suma/3;
- printf("MEDIA = %ld\n",m);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement