Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <pthread.h>
  4. #include <string.h>
  5. //#define _CRT_SECURE_NO_WARNINGS
  6. #define POCETZNAKU 62
  7. #define SOUBOR "random1.dat"
  8. //const char* SOUBOR = "random1.dat";
  9. //FILE *fp;
  10.  
  11.  
  12. int pocetVlaken = 5;            /// TODO argument pri spusteni programu
  13.  
  14. char poleZnaku[POCETZNAKU] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  15.                                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  16.                                '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
  17.  
  18.  
  19. void  vytvorSoubor(int);
  20. int   FileScan();
  21. void  producenti(int, char*);
  22. void* producentFunctions(int, char);
  23.  
  24. typedef struct Bloky{
  25.     char *blok;
  26.     int delkaBlok;
  27. }blck;
  28.  
  29. int main(int argc, char* argv[]) {
  30.  
  31.     //pocetVlaken = argv[1];
  32.  
  33. #pragma region vytvoreni souboru a zjisteni velikosti bloku
  34.     vytvorSoubor(10000000);
  35.     int velikostSouboru = FileScan();
  36.     if (velikostSouboru/pocetVlaken > 1000)
  37.     {
  38.         pocetVlaken = velikostSouboru/1000;     // max velikost jednoho bloku bude 1kB
  39.         printf("!!!Nedostatecny pocet vlaken!!! \nPocet vlaken zmenen na %d\n", pocetVlaken);
  40.     }
  41.     int velikostBloku = velikostSouboru / pocetVlaken;
  42.     int posledniBlok = velikostSouboru % pocetVlaken;       // pricist k poslednimu vlaknu
  43.  
  44. #pragma endregion
  45.  
  46.     FILE *file;
  47.     file = fopen(SOUBOR, "r");
  48.    
  49.     pthread_t *producent_ta = malloc(pocetVlaken,sizeof(int));
  50.     pthread_t consumer_t;
  51.    
  52.     int i,b;
  53.     for (i = 0; i < pocetVlaken; i++){
  54.         if (i == pocetVlaken-1)     //posledni vlakno
  55.         {
  56. #pragma region posledni blok textu
  57.             blck p;
  58.             p.delkaBlok = velikostBloku + posledniBlok;
  59.             p.blok = malloc(p.delkaBlok);
  60.             for (b = 0; b < p.delkaBlok; b++)
  61.             {
  62.                 p.blok[b] = fgetc(file);
  63.             }
  64.             printf("velikost POSLEDNIHO bloku %d B\n", velikostBloku + posledniBlok);
  65.             getchar();
  66.             ///producenti(velikostBloku  + posledniBlok, p);
  67.             // predani pole p vlaknu
  68.             pthread_attr_t attr;
  69.             pthread_attr_init(&attr);
  70.             //pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  71.             pthread_create(&producent_ta[i], &attr, &producentFunctions, (void *)&p);
  72. #pragma endregion
  73.         }
  74.         else
  75.         {
  76. #pragma region bloky od 1 do n-1
  77.             blck p;
  78.             p.delkaBlok = velikostBloku;
  79.             p.blok = malloc(p.delkaBlok);
  80.             for (b = 0; b < p.delkaBlok; b++)
  81.             {
  82.                 p.blok[b] = fgetc(file);
  83.             }
  84.             printf("velikost bloku %d B\n", velikostBloku);
  85.             getchar();
  86.             ///producenti(velikostBloku, p);
  87.             // predani pole p vlaknu
  88.  
  89.             pthread_attr_t attr;
  90.             pthread_attr_init(&attr);
  91.             pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  92.             pthread_create(&producent_ta[i], &attr, &producentFunctions, (void *)&p);
  93.         }
  94. #pragma endregion
  95.  
  96.     }
  97.     fclose(file);
  98.  
  99.     for (int i = 0; i < pocetVlaken; i++)
  100.     {
  101.        
  102.  
  103.  
  104.     }
  105.     //// vlakna
  106.    
  107.    
  108.  
  109.  
  110.     getchar();
  111.     return EXIT_SUCCESS;
  112. }
  113.  
  114. void vytvorSoubor(int velikost)
  115. {
  116.     FILE *file;
  117.     file = fopen(SOUBOR, "w");
  118.  
  119.     int i, j;
  120.    
  121.     for (i = 0; i<velikost; i++)
  122.     {
  123.         j = (int)rand() % POCETZNAKU;
  124.         putc(poleZnaku[j], file);               // pole Znaku = predem definovane znaky
  125.     }
  126.     printf("Vytvoren soubor \"%s\"\n", SOUBOR);
  127.     fclose(file);
  128. }
  129.  
  130. int FileScan()
  131. {
  132.     FILE *file;
  133.     file = fopen(SOUBOR, "r");
  134.     char c;
  135.     int pocetZnaku = 0;
  136.     while (!feof(file))
  137.     {
  138.         c = fgetc(file);
  139.         pocetZnaku = pocetZnaku + 1;
  140.     }
  141.     printf("Pocet znaku v souboru %d\n\n", pocetZnaku-1);
  142.     fclose(file);
  143.     return pocetZnaku-1;
  144. }
  145.  
  146. void producenti(int blok, char *p)
  147. {
  148.     int *cetnosti = calloc(POCETZNAKU, sizeof(int));        // alokace pole velikosti POCETZNAKU * INT a vyplni hodnoty na 0
  149.     int i = 0;
  150.     int j ;
  151.     for (i = 0; i < blok; i++)
  152.     {
  153.         printf("%c %d\n", p[i], i);
  154.         for (j = 0; j < POCETZNAKU; j++)
  155.         {
  156.             if (poleZnaku[j] == p[i])
  157.             {
  158.                 cetnosti[j] += 1;
  159.             }
  160.         }
  161.     }
  162.  
  163.     getchar();
  164.     for (i = 0; i < POCETZNAKU; i++)        // tisk vysledku
  165.     {
  166.         printf("cetnosti %c %d \n", poleZnaku[i], cetnosti[i]);
  167.     }
  168. }
  169.  
  170. void* producentFunctions(void *arg)
  171. {
  172.     blck *t_blok;
  173.     t_blok = (blck *)arg;
  174.    
  175.     FILE *f = fopen("vysledek.txt", "w");
  176.     int *cetnosti = calloc(POCETZNAKU, sizeof(int));        // alokace pole velikosti POCETZNAKU * INT a vyplni hodnoty na 0
  177.     int i = 0;
  178.     int j;
  179.     fprintf(f,"vlakno: %d\n", pthread_self());
  180.     printf( "vlakno: %d\n", pthread_self());
  181.  
  182.     for (i = 0; i < t_blok->delkaBlok; i++)
  183.     {
  184.         printf("%c %d\n", t_blok->blok[i], i);
  185.         for (j = 0; j < POCETZNAKU; j++)
  186.         {
  187.             if (poleZnaku[j] == t_blok->blok[i])
  188.             {
  189.                 cetnosti[j] += 1;
  190.             }
  191.         }
  192.     }
  193.  
  194.     //getchar();
  195.     for (i = 0; i < POCETZNAKU; i++)        // tisk vysledku
  196.     {
  197.         printf("cetnosti %c %d \n", poleZnaku[i], cetnosti[i]);
  198.     }
  199. }
  200. void* konzumer()
  201. {
  202. }
  203.  
  204.  
  205.  
  206. //TODO vlakna - pomoci pipe viz saly, pocet pipe = POCETZNAKU
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement