Advertisement
Anonymus_Soul

Untitled

Apr 15th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <sys/wait.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7.  
  8. //Struct Létrehozása adattárolás miatt
  9. typedef struct {
  10.     int ThreadIndex;
  11.     int* Resoults;
  12.     int* Input;
  13. } inputdata;
  14.  
  15. //Függvény prototípusok
  16. void* Forktask(void* argument);
  17. void GenerateRand(int upper, int lower, int* tomb );
  18. int Final(int* tomb);
  19.  
  20. int main() {
  21.     srand(time(NULL));
  22.     //pipe változók dekralációja
  23.     int pipes[2];
  24.     pid_t cpid;
  25. //pipe error kezelés
  26.     if(pipe(pipes) == -1){
  27.         perror("pipe");
  28.         exit(-1);
  29.     }
  30. //pidek kiiratása
  31.     printf("%d: fd1: %d, fd2: %d\n",getpid(),pipes[0],pipes[1]);
  32. //gyermek process léthehozása
  33.     cpid = fork();
  34.     if(cpid == -1){
  35.                 perror("fork");
  36.                 exit(0);
  37.     }  
  38. //gyermek process szálkezelési mechanizmus pipe-ok segítségével
  39.     if(cpid == 0){
  40.         int i;
  41.         int input[1000];
  42.         int Resoults[10] = { 0,0,0,0,0,0,0,0,0 };
  43.         pthread_t Thread[10];
  44.         read(pipes[0], input, sizeof(int) * 1000);
  45.         close(pipes[0]);
  46.             for (i = 0; i < 10; i++){
  47.                 inputdata* Active = malloc(sizeof(inputdata));
  48.                 Active->ThreadIndex = i;
  49.                 Active->Input = input;
  50.                 Active->Resoults = Resoults;
  51.                 pthread_create(&Thread[i], NULL, Forktask, Active);
  52.             }
  53.             for (i = 0; i < 10; i++){
  54.                 pthread_join(Thread[i], NULL);
  55.             }
  56.             write(pipes[1], Resoults, 10 * sizeof(int));
  57.             close(pipes[0]);
  58.             exit(0);
  59.     }
  60. //szülőprocess szálkezelési mechanizmus pipe-ok segítségével és eredmény meghatározás és kiiratás
  61.     else
  62.     {
  63.         int i;
  64.         int input[1000];
  65.         int Resoults[10];
  66.         GenerateRand(10000, 1000, input);
  67.         write(pipes[1], input, sizeof(int) * 1000);
  68.         read(pipes[0], Resoults, 10 * sizeof(int));
  69.         close(pipes[0]);
  70.         wait(NULL);
  71.         for (i = 0; i < 10; i++) {
  72.             printf("%d.szál eredménye: %d\n", i + 1, Resoults[i]);
  73.         }
  74.         printf("Eredmény :%d\n", Final(Resoults));
  75.     }
  76.     return 0;
  77. }
  78. //szálankénti maximum kiválasztás
  79. void* Forktask(void* arg) {
  80.     inputdata* argument = arg;
  81.     int BeginingIndex = argument->ThreadIndex * 100;
  82.     int EndIndex = BeginingIndex + 100;
  83.     int i;
  84.     for (i = BeginingIndex; i < EndIndex; i++)
  85.     {
  86.         if (argument->Input[i] > argument->Resoults[argument->ThreadIndex]) {
  87.             argument->Resoults[argument->ThreadIndex] = argument->Input[i];
  88.         }
  89.     }
  90.     free(argument);
  91.     pthread_exit(0);
  92. }
  93. //random szám generálás a tömbfeltöltés miatt
  94. void GenerateRand(int upper, int lower, int* tomb) {
  95.     int i;
  96.     for ( i = 0; i < 1000; i++){
  97.          tomb [i] = (rand() % (upper - lower + 1)) + lower;
  98.  
  99.     }
  100. }
  101. //végső legnagyobb elem meghatározása a szálak által pipeon keresztül begyűjtött adatokból
  102. int Final(int* tomb) {
  103.     int FinalRseaults = 0,i;
  104.     for (i = 0; i < 10; i++){
  105.         if (tomb[i] > FinalRseaults){
  106.             FinalRseaults = tomb[i];
  107.         }
  108.     }
  109.     return FinalRseaults;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement