Advertisement
quuircio

eseFacilissimo2

May 18th, 2022
654
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.  
  12. static FILE* sfile;
  13.  
  14. pthread_mutex_t mutex;
  15.  
  16. void* funzione(void* args)
  17. {
  18.  
  19.     char* frase="frase1";
  20.     fprintf(sfile, "%s", frase);
  21.     pthread_mutex_unlock(&mutex);
  22.     printf("%s by funzione1\n", frase);
  23.     sleep(1);
  24.     exit(0);
  25. }
  26.  
  27. void* funzione_b(void* args)
  28. {
  29.    
  30.     char* frase_b= "frase2";
  31.     pthread_mutex_lock(&mutex);
  32.     fprintf(sfile, "%s", frase_b);
  33.     //fflush(sfile);
  34.     pthread_mutex_unlock(&mutex);
  35.    
  36.     printf("%s by funzione_b\n", frase_b);
  37.     exit(0);
  38. }
  39.  
  40. int main()
  41. {
  42.     void* res;
  43.     pthread_mutex_init(&mutex, NULL);
  44.     pthread_mutex_lock(&mutex);
  45.     sfile=fopen("sincro2.txt", "w+");
  46.  
  47.     pthread_t ta;
  48.     pthread_t tb;
  49.     pthread_create(&ta, NULL, funzione, NULL);
  50.     pthread_create(&tb, NULL, funzione_b, NULL);
  51.  
  52.     pthread_join(ta, &res);
  53.     pthread_join(tb, &res);
  54.    
  55.     fclose(sfile);
  56.     return 0;
  57.    
  58. }
  59.  
Advertisement
RAW Paste Data Copied
Advertisement