Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Załączenie bibliotek
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <err.h>
- #include <fcntl.h>
- #include <time.h>
- #include <pthread.h>
- //Zmienna do pamięci współdzielonej
- #define SHM_SIZE 10
- //Tablice mutexów do zapisu i odczytu
- pthread_mutex_t lock_write[2];
- pthread_mutex_t lock_read[2];
- //Funkcja wczytywania pamięci współdzielonej
- char* szer() {
- int shmid;
- key_t key;
- char *shm;
- if ((key = ftok(".", 'A')) == -1) {
- errx(1, "Blad tworzenia klucza!");
- }
- if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0) {
- errx(2, "Blad tworzenia segmentu pamieci dzielonej!");
- }
- if ((shm = shmat(shmid, NULL, 0)) == (char *)-1) {
- errx(3, "Blad przylaczania pamieci dzielonej!");
- }
- return shm;
- }
- //Funkcja procesu producenta
- void *proces_producenta(void *arg) {
- char *s;
- s = szer();
- int i = 0;
- for (i = 0; i <= 100; i++) {
- pthread_mutex_lock(&lock_write[0]);
- pthread_mutex_lock(&lock_write[1]);
- printf("Proces producenta, dodany element: %i\n", i);
- *s++ = i;
- pthread_mutex_unlock(&lock_read[0]);
- pthread_mutex_unlock(&lock_read[1]);
- }
- pthread_exit(0);
- }
- //Funkcja procesu klienta
- void *proces_klienta(void *arg) {
- char *s;
- s = szer();
- int numer_klienta = (int)arg;
- int i = 0;
- for (i = 0; i <= 100; i++) {
- pthread_mutex_lock(&lock_read[numer_klienta - 1]);
- printf("Klienta %i, pobrany element: %i\n", numer_klienta, *s);
- s++;
- pthread_mutex_unlock(&lock_write[numer_klienta - 1]);
- }
- pthread_exit(0);
- }
- int main() {
- char *s;
- s = szer();
- int err;
- pthread_t p_thread[3];
- //Zainicjalizowanie mutexów
- if (pthread_mutex_init(&lock_read[0], NULL) != 0)
- {
- printf("\n fail - mutex init \n");
- return 1;
- }
- if (pthread_mutex_init(&lock_write[0], NULL) != 0)
- {
- printf("\n fail - mutex init \n");
- return 1;
- }
- if (pthread_mutex_init(&lock_read[1], NULL) != 0)
- {
- printf("\n fail - mutex init \n");
- return 1;
- }
- if (pthread_mutex_init(&lock_write[1], NULL) != 0)
- {
- printf("\n fail - mutex init \n");
- return 1;
- }
- //Ustawienie początkowych wartości mutexów
- pthread_mutex_unlock(&lock_write[0]);
- pthread_mutex_unlock(&lock_write[1]);
- pthread_mutex_lock(&lock_read[0]);
- pthread_mutex_lock(&lock_read[1]);
- int i = 0;
- //Utworzenie wątku producenta
- err = pthread_create(&p_thread[0], NULL, proces_producenta, (void *)i);
- if (err != 0)
- fprintf(stderr, "Error creating the thread");
- sleep(1);
- int kli = 1;
- //Utworzenie wątku klienta 1
- err = pthread_create(&p_thread[1], NULL, proces_klienta, (void *)kli);
- if (err != 0)
- fprintf(stderr, "Error creating the thread");
- sleep(1);
- kli = 2;
- //Utworzenie wątki klienta 2
- err = pthread_create(&p_thread[2], NULL, proces_klienta, (void *)kli);
- if (err != 0)
- fprintf(stderr, "Error creating the thread");
- int cnt = 0;
- //Zamknięcie wątków
- for (cnt = 0; cnt < 3; cnt++) {
- pthread_join(p_thread[cnt], NULL);
- }
- fprintf(stdout, "All threads completed.\n");
- //Usunięcie mutexów
- pthread_mutex_destroy(&lock_write[0]);
- pthread_mutex_destroy(&lock_read[1]);
- pthread_mutex_destroy(&lock_write[1]);
- pthread_mutex_destroy(&lock_read[0]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement