Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.43 KB | None | 0 0
  1. // Lab4_pp.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #define HAVE_STRUCT_TIMESPEC
  6.  
  7. #include <pthread.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <windows.h>
  11. #include <semaphore.h>
  12. pthread_rwlock_t rwlock;
  13.  
  14.  
  15.  
  16. FILE *file;
  17.  
  18. void *entrance(void*);
  19.  
  20. void *full_modify(void *param)
  21. {
  22.     while (true) {
  23.         pthread_rwlock_wrlock(&rwlock);
  24.         file = fopen("file.txt", "w");
  25.         for (int i = 0; i < 5; i++) {
  26.             char *string = "PADDING\n";
  27.             fputs(string, file);
  28.         }
  29.         fclose(file);
  30.         pthread_rwlock_unlock(&rwlock);
  31.         Sleep(3000);
  32.     }
  33. }
  34.  
  35. void *screen(void *param)
  36. {
  37.     setbuf(stdout, NULL);
  38.     char n;
  39.     while (true) {
  40.         pthread_rwlock_rdlock(&rwlock);
  41.         file = fopen("file.txt", "r");
  42.        
  43.         while (!feof(file))
  44.         {
  45.             int res = fscanf(file, "%c", &n);
  46.             if (res == EOF)
  47.                 break;
  48.             fprintf(stdout, "%c", n);
  49.            
  50.         }
  51.         pthread_t thread1[1];
  52.         pthread_create(&thread1[0], NULL, entrance, NULL);
  53.         printf("\n\n");
  54.         fflush(stdout);
  55.         Sleep(10);
  56.        
  57.         fclose(file);
  58.         pthread_rwlock_unlock(&rwlock);
  59.         Sleep(2000);
  60.     }
  61. }
  62.  
  63. void *entrance(void *param)
  64. {
  65.     char n;
  66.     setbuf(stdout, NULL);
  67.     //while (true) {
  68.         char count[91] = { 0 };
  69.         pthread_rwlock_rdlock(&rwlock);
  70.         file = fopen("file.txt", "r");
  71.         while (!feof(file))
  72.         {
  73.             int res = fscanf(file, "%c", &n);
  74.             if (res == EOF)
  75.                 break;
  76.             count[n]++;
  77.         }
  78.        
  79.         for (int i = 65; i < 91; i++) {
  80.             fprintf(stdout, "%c : %d  ", (char)i, count[i]);
  81.            
  82.         }
  83.         fprintf(stdout,"\n");
  84.         fflush(stdout);
  85.         Sleep(10);
  86.    
  87.        
  88.         fclose(file);
  89.         pthread_rwlock_unlock(&rwlock);
  90.         //Sleep(2000);
  91.     //}
  92.         return 0;
  93. }
  94.  
  95. void *add(void *param)
  96. {
  97.    
  98.     while (true) {
  99.         pthread_rwlock_wrlock(&rwlock);
  100.         file = fopen("file.txt", "a");
  101.    
  102.         char string[10];
  103.         string[8] = '\n';
  104.         string[9] = '\0';
  105.         for (size_t i = 0; i < 8; i++)
  106.         {
  107.             string[i] = 'A' + (rand() % 26);
  108.            
  109.         }
  110.             fputs(string, file);
  111.        
  112.         fclose(file);
  113.         pthread_rwlock_unlock(&rwlock);
  114.         Sleep(1000);
  115.     }
  116. }
  117.  
  118. int main()
  119. {
  120.     setbuf(stdout, NULL);
  121.  
  122.     pthread_rwlock_init(&rwlock, NULL);
  123.    
  124.    
  125.     pthread_t thread[4] ;
  126.     //pthread_create(&thread[0], NULL, full_modify, NULL) ;
  127.    
  128.     pthread_create(&thread[1], NULL, screen, NULL) ;
  129.  
  130.     pthread_t adders[3];
  131.     for (int i=0; i<3; i++) pthread_create(&adders[i], NULL, add, NULL);
  132.  
  133.     //pthread_create(&thread[2], NULL, entrance, NULL);
  134.  
  135.     full_modify(0);
  136.    
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement