Advertisement
quuircio

esercizioFacilissimo1

May 18th, 2022
582
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <sys/sem.h>
  5. #include <sys/ipc.h>
  6. #include <sys/types.h>
  7. #include <sys/syscall.h>
  8. #include <pthread.h>
  9. #include <fcntl.h>
  10. #include <string.h>
  11. #include <errno.h>
  12.  
  13.  
  14.  
  15.  
  16. void* funzione(void* args)
  17. {
  18.     //filename=(FILE*)args;
  19.     printf("SONO IL THREAD CON TID: %d\n", pthread_self());
  20.     char* frase="roma2";
  21.    
  22.     FILE* f = fopen("sincro.txt", "w+"); if (f == NULL) {printf("errno val: %d\n", errno); exit (-1);}
  23.     printf("check\n");
  24.     fprintf(f, "%s", frase);
  25.     //fwrite(frase, 1, dim, filename);
  26.     //fflush(stdin);
  27.     fclose(f);
  28.     //printf("%s\n", frase);
  29.     exit(0);
  30. }
  31.  
  32.  
  33. void* funzione_b(void* args)
  34. {
  35.    
  36.     char* frase_b="pongo";
  37.     FILE* f = fopen("sincro.txt", "a"); if (f == NULL) {printf("errno val: %d\n", errno); exit (-1);}
  38.     printf("check\n");
  39.  
  40.     for (int i=0; i<10; i++)
  41.     {
  42.         fprintf(f, "%s", frase_b);
  43.     }
  44.    
  45.    
  46.     //fflush(sfile);
  47.     fclose(f);
  48.     //printf("%s\n", frase_b);
  49.     exit(0);
  50. }
  51.  
  52. int main()
  53. {
  54.     void* res;
  55.    
  56.  
  57.     pthread_t ta;
  58.     pthread_t tb;
  59.     pthread_create(&ta, NULL, funzione, NULL);
  60.     pthread_create(&tb, NULL, funzione_b, NULL);
  61.  
  62.     pthread_join(ta, &res);
  63.     pthread_join(tb, &res);
  64.    
  65.  
  66.     return 0;
  67.    
  68. }
  69.  
Advertisement
RAW Paste Data Copied
Advertisement