int main() { int key = 123 ; int memsize = sizeof(int) ; int shmemId = open_shmem(key, memsize) ; if (shmemId == -1) { perror("shared segment opening error") ; exit(EXIT_FAILURE) ; } int* buffer = (int*) attach_shmem(shmemId) ; *buffer = 0 ; int emptySemKey = 100 ; int emptySemId = open_semaphore(emptySemKey) ; int fullSemKey = 101 ; int fullSemId = open_semaphore(fullSemKey) ; if ((emptySemId == -1) || (fullSemId == -1)) { perror("semaphore opening error") ; exit(EXIT_FAILURE) ; } while (*buffer >= 0) { down(fullSemId) ; if (*buffer >= 0) printf("%d\n", *buffer) ; up(emptySemId) ; } detach_shmem(buffer) ; }
int main(){ signal(SIGQUIT, quit); signal(SIGINT, quit); signal(SIGUSR2, priority); signal(SIGUSR1, feux); key_t cle_shmem = KEY_SHMEM; key_t key_mutex = KEY_MUTEX; if((int)(id_mutex = open_semaphore(key_mutex)) == -1) { printf("Impossible d'ouvrir le mutex.\n"); quit(); } if((int)(id_shmem = open_shmem(cle_shmem, shmem_size)) == -1) { printf("Feux : Impossible d'ouvrir la mémoire partagée.\n"); quit(); } if((int)(pshmem = attach_shmem(id_shmem)) == -1) { printf("Feux : Impossible de s'attacher à la mémoire partagée.\n"); quit(); } printf("PID Feux : %d\n", getpid()); down(id_mutex); pshmem[PID_FEUX]=getpid(); up(id_mutex); feux(); }
void generateurTrafficPrioritaire(){ key_t key_shmem = KEY_SHMEM; key_t key_mutex = KEY_MUTEX; if((id_mutex = open_semaphore(key_mutex)) == -1) { printf("Impossible d'ouvrir le mutex.\n"); quit(); } if((int)(id_shmem = open_shmem(key_shmem, shmem_size)) == -1) { printf("generateurTrafficPrioritaire : Impossible d'ouvrir la mémoire partagée.\n"); quit(); } if((int)(pshmem = attach_shmem(id_shmem)) == -1) { printf("generateurTrafficPrioritaire : Impossible de s'attacher à la mémoire partagée.\n"); quit(); } down(id_mutex); pshmem[SRC_PRIO] = 0; pshmem[DEST_PRIO] = 0; pshmem[ID_PRIO] = 0; pshmem[PID_PRIO]=getpid(); up(id_mutex); int pid_coord = pshmem[PID_COORD]; printf("PID generPrio : %d\n", getpid()); printf("PID Coord : %d\n", pid_coord); int timer, source, dest, id=0; srand(time(NULL)); for(;;){ timer=rand()%26; while(timer<15){ timer=rand()%26; } printf("Timer is UP: %d s\n", timer); sleep(timer); printf("Timer is DOWN: %d s\n", timer); source=(rand()%4)+1; dest=(rand()%4)+1; while(dest==source){ dest=(rand()%4)+1; } down(id_mutex); pshmem[SRC_PRIO] = source; pshmem[DEST_PRIO] = dest; pshmem[ID_PRIO] = id; up(id_mutex); kill(pid_coord, SIGUSR1); printf("! --- PRIORITAIRE --- ! Source: %d, Dest: %d, ID :%d\n", source, dest, id); printf("Coordinateur is notified by signal !\n"); id++; } }