Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.23 KB | None | 0 0
  1. //Załączenie bibliotek
  2. #include <sys/types.h>
  3. #include <sys/ipc.h>
  4. #include <sys/shm.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <err.h>
  9. #include <fcntl.h>
  10. #include <time.h>
  11. #include <pthread.h>
  12.  
  13. //Zmienna do pamięci współdzielonej
  14. #define SHM_SIZE 10
  15.  
  16. //Tablice mutexów do zapisu i odczytu
  17. pthread_mutex_t lock_write[2];
  18. pthread_mutex_t lock_read[2];
  19.  
  20.  
  21. //Funkcja wczytywania pamięci współdzielonej
  22. char* szer() {
  23.     int shmid;
  24.     key_t key;
  25.     char *shm;
  26.     if ((key = ftok(".", 'A')) == -1) {
  27.         errx(1, "Blad tworzenia klucza!");
  28.     }
  29.     if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0) {
  30.         errx(2, "Blad tworzenia segmentu pamieci dzielonej!");
  31.     }
  32.     if ((shm = shmat(shmid, NULL, 0)) == (char *)-1) {
  33.         errx(3, "Blad przylaczania pamieci dzielonej!");
  34.     }
  35.     return shm;
  36. }
  37.  
  38. //Funkcja procesu producenta
  39. void *proces_producenta(void *arg) {
  40.     char *s;
  41.     s = szer();
  42.     int i = 0;
  43.     for (i = 0; i <= 100; i++) {
  44.         pthread_mutex_lock(&lock_write[0]);
  45.         pthread_mutex_lock(&lock_write[1]);
  46.         printf("Proces producenta, dodany element: %i\n", i);
  47.         *s++ = i;
  48.         pthread_mutex_unlock(&lock_read[0]);
  49.         pthread_mutex_unlock(&lock_read[1]);
  50.     }
  51.     pthread_exit(0);
  52. }
  53.  
  54. //Funkcja procesu klienta
  55. void *proces_klienta(void *arg) {
  56.     char *s;
  57.     s = szer();
  58.     int numer_klienta = (int)arg;
  59.     int i = 0;
  60.     for (i = 0; i <= 100; i++) {
  61.         pthread_mutex_lock(&lock_read[numer_klienta - 1]);
  62.         printf("Klienta %i, pobrany element: %i\n", numer_klienta, *s);
  63.         s++;
  64.         pthread_mutex_unlock(&lock_write[numer_klienta - 1]);
  65.     }
  66.     pthread_exit(0);
  67. }
  68.  
  69. int main() {
  70.     char *s;
  71.     s = szer();
  72.     int err;
  73.     pthread_t p_thread[3];
  74.     //Zainicjalizowanie mutexów
  75.     if (pthread_mutex_init(&lock_read[0], NULL) != 0)
  76.     {
  77.         printf("\n fail - mutex init \n");
  78.         return 1;
  79.     }
  80.     if (pthread_mutex_init(&lock_write[0], NULL) != 0)
  81.     {
  82.         printf("\n fail - mutex init \n");
  83.         return 1;
  84.     }
  85.     if (pthread_mutex_init(&lock_read[1], NULL) != 0)
  86.     {
  87.         printf("\n fail - mutex init \n");
  88.         return 1;
  89.     }
  90.     if (pthread_mutex_init(&lock_write[1], NULL) != 0)
  91.     {
  92.         printf("\n fail - mutex init \n");
  93.         return 1;
  94.     }
  95.     //Ustawienie początkowych wartości mutexów
  96.     pthread_mutex_unlock(&lock_write[0]);
  97.     pthread_mutex_unlock(&lock_write[1]);
  98.     pthread_mutex_lock(&lock_read[0]);
  99.     pthread_mutex_lock(&lock_read[1]);
  100.     int i = 0;
  101.     //Utworzenie wątku producenta
  102.     err = pthread_create(&p_thread[0], NULL, proces_producenta, (void *)i);
  103.     if (err != 0)
  104.         fprintf(stderr, "Error creating the thread");
  105.     sleep(1);
  106.     int kli = 1;
  107.     //Utworzenie wątku klienta 1
  108.     err = pthread_create(&p_thread[1], NULL, proces_klienta, (void *)kli);
  109.     if (err != 0)
  110.         fprintf(stderr, "Error creating the thread");
  111.     sleep(1);
  112.     kli = 2;
  113.     //Utworzenie wątki klienta 2
  114.     err = pthread_create(&p_thread[2], NULL, proces_klienta, (void *)kli);
  115.     if (err != 0)
  116.         fprintf(stderr, "Error creating the thread");
  117.     int cnt = 0;
  118.     //Zamknięcie wątków
  119.     for (cnt = 0; cnt < 3; cnt++) {
  120.         pthread_join(p_thread[cnt], NULL);
  121.     }
  122.     fprintf(stdout, "All threads completed.\n");
  123.     //Usunięcie mutexów
  124.     pthread_mutex_destroy(&lock_write[0]);
  125.     pthread_mutex_destroy(&lock_read[1]);
  126.     pthread_mutex_destroy(&lock_write[1]);
  127.     pthread_mutex_destroy(&lock_read[0]);
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement