Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <semaphore.h>
- #include <stdio.h>
- #include <unistd.h>
- #define READERS 5 // liczba czytelnikĂłw
- /*
- * - mutex: zapewnia wyĹÄ czny dostÄp do biblioteki albo czytelnikom albo
- * pisarzowi
- * - mutex_readers: w rozwiÄ zaniu trzeba wiedzieÄ czy dany czytelnik jest
- * pierwszym wchodzÄ cym lub ostatnim wychodzÄ cym by wykonaÄ wtedy dodatkowe
- * operacje. Ten mutex zapewnia, Ĺźe sprawdzenie to wykona siÄ poprawnie (nie
- * bÄdzie sytuacji, Ĺźe dwĂłch lub wiÄcej czytelnikĂłw uzna siebie jednoczeĹnie
- * za "pierwszego" lub "ostatniego" czytelnika.
- * - mutex_order: zapewnia cechÄ kolejkowania tak aby nie doszĹo do zagĹodzenia
- * czytelnikĂłw lub pisarza
- */
- pthread_mutex_t mutex, mutex_readers, mutex_order;
- sem_t sem_readers; // licznik czytelnikĂłw
- /*
- * Kod symulujÄ cy czytanie (krĂłtka operacja)
- */
- void read_content(int id) {
- printf("Reading (id=%d)\n", id);
- }
- /*
- * Kod symulujÄ cy pisanie (wymuszona, dĹuga operacja)
- */
- void write_content() {
- int i;
- printf("Writing");
- fflush(NULL);
- for (i = 0; i < 3; ++i) {
- sleep(1);
- printf(".");
- fflush(NULL);
- }
- printf("\n");
- }
- /*
- * Kod kaĹźdego czytelnika.
- */
- void *reader(void *id) {
- while (1) {
- int readers;
- pthread_mutex_lock(&mutex_order);
- pthread_mutex_lock(&mutex_readers);
- sem_getvalue(&sem_readers, &readers);
- if (readers == 0) // jeĹli pierwszy czytelnik
- pthread_mutex_lock(&mutex); // to zablokuj bibliotekÄ
- sem_post(&sem_readers); // zwiÄksz licznik czytelnikĂłw
- pthread_mutex_unlock(&mutex_order);
- pthread_mutex_unlock(&mutex_readers);
- read_content((int) id); // akcja czytania
- pthread_mutex_lock(&mutex_readers);
- sem_wait(&sem_readers);
- sem_getvalue(&sem_readers, &readers);
- if (readers == 0) // jeĹli ostatni czytelnik
- pthread_mutex_unlock(&mutex); // to zwolnij bibliotekÄ
- pthread_mutex_unlock(&mutex_readers);
- }
- }
- /*
- * Kod pisarza bardzo przypomina kod producenta. Dodano jedynie "mutex_order"
- * celem zapewnienia cechy kolejkowania ĹźÄ daĹ dostÄpu do biblioteki.
- */
- void *writer(void *dummy) {
- while (1) {
- pthread_mutex_lock(&mutex_order);
- pthread_mutex_lock(&mutex);
- pthread_mutex_unlock(&mutex_order);
- write_content();
- pthread_mutex_unlock(&mutex);
- }
- }
- int main() {
- int i;
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
- pthread_mutex_init(&mutex, NULL);
- pthread_mutex_init(&mutex_readers, NULL);
- pthread_mutex_init(&mutex_order, NULL);
- sem_init(&sem_readers, 0, 0);
- pthread_t threads[2];
- pthread_create(&threads[0], &attr, writer, NULL);
- for (i = 1; i <= READERS; ++i)
- pthread_create(&threads[i], &attr, reader, (void*) i);
- for (i = 0; i <= READERS + 1; ++i)
- pthread_join(threads[i], NULL);
- pthread_attr_destroy(&attr);
- pthread_mutex_destroy(&mutex);
- pthread_mutex_destroy(&mutex_readers);
- pthread_mutex_destroy(&mutex_order);
- sem_destroy(&sem_readers);
- pthread_exit(NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement