|
|
@ -10,19 +10,16 @@ |
|
|
|
|
|
|
|
#include "semaph.h" |
|
|
|
|
|
|
|
#define FILE_NB_READERS_NAME "readers.dat" |
|
|
|
|
|
|
|
static void usage(void); |
|
|
|
|
|
|
|
sem_t *lock_for_shared_variables; |
|
|
|
sem_t *lock_for_readers_writers; |
|
|
|
sem_t *lock_against_starvation; |
|
|
|
|
|
|
|
/* System file descriptor containing le number
|
|
|
|
* of readers accessing the shared ressource */ |
|
|
|
FILE *number_of_readers_file; |
|
|
|
|
|
|
|
/* Macro: name of the file containing the number of readers accessint the shared resource */ |
|
|
|
#define FILE_NB_READERS_NAME "readers.dat" |
|
|
|
|
|
|
|
/* Prototypes of functions */ |
|
|
|
void readers_function(int); |
|
|
|
void writers_function(int); |
|
|
|
char is_first(); |
|
|
@ -42,6 +39,7 @@ main(int argc, char *argv[]) |
|
|
|
{ |
|
|
|
int i, max_writers, max_readers, child_state; |
|
|
|
int initial_sem_counter = 1; // initial value for semaphores counter
|
|
|
|
|
|
|
|
if (argc != 3) |
|
|
|
usage(); |
|
|
|
|
|
|
@ -58,6 +56,9 @@ main(int argc, char *argv[]) |
|
|
|
if ((lock_for_readers_writers = sem_open(LOCK_FOR_READERS_WRITERS_NAME, O_CREAT, 0644, initial_sem_counter)) == SEM_FAILED) |
|
|
|
err(EXIT_FAILURE, "sem_open"); |
|
|
|
|
|
|
|
if ((lock_against_starvation = sem_open(LOCK_AGAINST_STARVATION_NAME, O_CREAT, 0644, initial_sem_counter)) == SEM_FAILED) |
|
|
|
err(EXIT_FAILURE, "sem_open"); |
|
|
|
|
|
|
|
/*
|
|
|
|
* Open and initialise the file containing the number of readers |
|
|
|
* accessing the shared resource. |
|
|
@ -118,6 +119,7 @@ writers_function(int writer_id) |
|
|
|
/* Loop of three writings */ |
|
|
|
for (i = 0; i < 3; ++i) { |
|
|
|
/***** Begining of the critical section for the readers/writers mechanism *****/ |
|
|
|
sem_wait(lock_against_starvation); |
|
|
|
sem_wait(lock_for_readers_writers); |
|
|
|
|
|
|
|
/* get the current date */ |
|
|
@ -142,6 +144,7 @@ writers_function(int writer_id) |
|
|
|
printf(" ******************** Writer %d about to exit the critical section -- Date : %02d:%02d (iteration : %d)\n", writer_id, minutes, seconds, i); |
|
|
|
|
|
|
|
/***** End of the critical section for the readers/writers mechanism *****/ |
|
|
|
sem_post(lock_against_starvation); |
|
|
|
sem_post(lock_for_readers_writers); |
|
|
|
sleep(2 + i); |
|
|
|
} |
|
|
@ -158,6 +161,8 @@ readers_function(int reader_id) |
|
|
|
time_t the_clock; |
|
|
|
struct tm *the_date; |
|
|
|
|
|
|
|
sem_wait(lock_against_starvation); |
|
|
|
sem_post(lock_against_starvation); |
|
|
|
sem_wait(lock_for_shared_variables); |
|
|
|
|
|
|
|
if (is_first()) { |
|
|
@ -190,6 +195,7 @@ readers_function(int reader_id) |
|
|
|
printf("========================= Reader %d about to exit the critical section -- Date : %02d:%02d \n", reader_id, minutes, seconds); |
|
|
|
|
|
|
|
sem_wait(lock_for_shared_variables); |
|
|
|
|
|
|
|
if (is_last()) { |
|
|
|
printf("========================= Reader %d is last\n", reader_id); |
|
|
|
/***** End of the critical section for the readers/writers mechanism *****/ |
|
|
|