Properly init the semaphore in the readers/writers code.
[TD_SE.git] / lecteursredacteurs / lecteursredacteurs.c
index 85db6457720f82d2108264527b9a6788e1b8fef2..e13f115f023cb1eea67e82bb6d71997941c7fbb5 100644 (file)
@@ -1,4 +1,6 @@
 #include <unistd.h>
+#include <pthread.h>
+#include <stdlib.h>
 #include <stdio.h>
 #include <semaphore.h>
 
@@ -25,6 +27,7 @@ static void *lecteur(void *arg)
                        sem_post(&redact);
                sem_post(&mutex);
        }
+       return NULL;
 }
 
 static void *redacteur(void *arg)
@@ -34,12 +37,21 @@ static void *redacteur(void *arg)
                // modifier les donnĂ©es de la base
                db++;
                printf("redacteur bd=%d\n", db);
-               sleep(2);
+               sleep(1);
                sem_post(&redact);
        }
+       return NULL;
 }
 
 int main()
 {
-
+       pthread_t t1, t2;
+       sem_init(&mutex, 0, 1);
+       sem_init(&redact, 0, 1);
+       pthread_create(&t1, NULL, redacteur, NULL);
+       pthread_create(&t2, NULL, lecteur, NULL);
+       pthread_join(t1, NULL);
+       pthread_join(t2, NULL);
+       printf("exit\n");
+       return EXIT_SUCCESS;
 }