Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <time.h>
- #define REENTRAN
- int status;
- int tab[2][10];
- int sum1=0;
- int sum2=0;
- void * suma1()
- {
- for(int i=0;i<10;i++)
- {
- sum1+=tab[0][i];
- }
- pthread_exit((void *)15);
- }
- void * suma2()
- {
- for(int j=0;j<10;j++)
- {
- sum2+=tab[1][j];
- }
- pthread_exit((void *)51);
- }
- int main()
- {
- int tmp, status;
- pthread_t one, two;
- srand(time(NULL));
- printf("\n");
- for(int i=0; i<2; i++)
- {
- for(int j=0;j<10;j++)
- {
- tab[i][j]=rand()%50;
- printf("%d ",tab[i][j]);
- }
- printf("\n");
- }
- tmp=pthread_create(&one, NULL, suma1, NULL);
- if(tmp==-1)
- {
- printf("blad tworzenia watku one");
- exit(1);
- }
- tmp=pthread_create(&two, NULL, suma2, NULL);
- if(tmp==-1)
- {
- printf("blad tworzenia watku two");
- exit(1);
- }
- tmp=pthread_join(one, (void **)&status);
- if(tmp==-1)
- {
- printf("blad podlaczenia watku one");
- exit(1);
- }
- else if(tmp==0)
- printf("status = %d\n",status);
- tmp=pthread_join(two, (void **)&status);
- if(tmp==-1)
- {
- printf("blad podlaczenia watku two");
- exit(1);
- }
- else if(tmp==0)
- printf("status = %d\n",status);
- printf("\ntid watku 1: %ld\n", one);
- printf("tid watku 2: %ld\n", two);
- printf("\nwatek pierwszy: %d\n", sum1);
- printf("\nwatek drugi: %d\n", sum2);
- printf("suma: %d\n", sum1+sum2);
- tmp=pthread_detach(one);
- if(tmp==-1)
- {
- printf("blad odlaczenia watku one");
- exit(1);
- }
- tmp=pthread_detach(two);
- if(tmp==-1)
- {
- printf("blad odlaczenia watku two");
- exit(1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment