Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "unistd.h"
  3. #include "pthread.h"
  4. #include "stdlib.h"
  5. #include "errno.h"
  6. #include "limits.h"
  7. #include "math.h"
  8. #include "semaphore.h"
  9. #include "string.h"
  10.  
  11. #define BUFFER_SIZE 100
  12.  
  13. #define PREFIX "MARCIN"
  14.  
  15. sem_t doZapisu, doOdczytu;
  16.  
  17. void emptyBuffer( char* buff);
  18. void emptyNBuffer( char* buff, int n);
  19.  
  20. void *threadCzytelnik(void *arg);
  21.  
  22.  
  23.  
  24.  
  25.  
  26. struct targs{
  27.     int numWatku;
  28. };
  29. char common[BUFFER_SIZE];
  30. int koniecPliku = 0;
  31. int main()
  32. {
  33.     FILE *fp;
  34.    
  35.     emptyBuffer(common);
  36.     sem_init(&doZapisu, 0, 1);
  37.     sem_init(&doOdczytu, 0, 0);
  38.     pthread_attr_t attrs;
  39.     pthread_attr_init(&attrs);
  40.     pthread_attr_setdetachstate(&attrs,
  41.     PTHREAD_CREATE_DETACHED );
  42.    
  43.     pthread_t threadId[3];
  44.     fp = fopen("/etc/passwd","r");
  45.     struct targs numery[3];
  46.     {
  47.         int i = 0;
  48.         for(;i < 3; i++)
  49.         {
  50.             numery[i].numWatku = i+1;
  51.             pthread_create(&threadId[i], &attrs, threadCzytelnik, &numery[i]);
  52.         }
  53.     }
  54.     while( 1 ) {
  55.         sem_wait(&doZapisu);
  56.             emptyBuffer(common);
  57.             char *ile = fgets(common, BUFFER_SIZE, fp);
  58.             if( ile==NULL ) break;
  59.         sem_post(&doOdczytu);
  60.         //printf("%s\n", common);
  61.         //emptyBuffer(common);
  62.     }
  63.     fclose(fp);
  64.     koniecPliku = 1;
  65.     sem_post(&doOdczytu);
  66.     sem_post(&doOdczytu);
  67.     sem_post(&doOdczytu);
  68. }
  69.  
  70. void emptyBuffer( char* buff)
  71. {
  72.     {
  73.         int i = 0;
  74.         for(;i<BUFFER_SIZE;i++)
  75.         {
  76.             buff[i] = 0;
  77.         }
  78.     }
  79. }
  80.  
  81. void emptyNBuffer( char* buff, int n)
  82. {
  83.     {
  84.         int i = 0;
  85.         for(;i<n;i++)
  86.         {
  87.             buff[i] = 0;
  88.         }
  89.     }
  90. }
  91.  
  92. void *threadCzytelnik(void *arg)
  93. {
  94.     struct targs *argptr = (struct targs*) arg;
  95.     char nazwaPliku[80];
  96.     snprintf(nazwaPliku, 80, "./MARCIN%d", argptr->numWatku);
  97.     FILE *fp = fopen(nazwaPliku, "w");
  98.     while(1)
  99.     {
  100.         sem_wait(&doOdczytu);
  101.             if(koniecPliku)
  102.             {
  103.                 fflush(fp);
  104.                 fclose(fp);
  105.                 pthread_exit(0);
  106.             }
  107.             fwrite(common, sizeof(char), strlen(common), fp);
  108.             printf("%d-%s\n", argptr->numWatku, common);
  109.             fflush(stdout);
  110.         sem_post(&doZapisu);
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement