Hello everybody,
This is parking program .I defined 4 task (keyboard,enter vehicle,exit vehicle,display)
The program is compile but when I run it and press keys (E,S,F), it does not work.
This program is written for linux.
Can you help me?
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#define TRUE 1
#define FALSE 0
int places=10;
sem_t evt_in,evt_out,evt_maj;
int fin=FALSE;
pthread_mutex_t mutex_pl;
pthread_cond_t cond_places ;
void * t_keyboard(void *arg){
char res;
res=getchar();
while (fin!=TRUE){
if(res=='f'){
fin=TRUE;}
else { if( res=='e'){
sem_post(&evt_in);}
else { if (res=='s'){
sem_post(&evt_out);
}
}
}
}
}
void * t_enter (void *arg){
while (fin!=TRUE){
sem_wait(&evt_in);
pthread_mutex_lock(&mutex_pl);
if (places==0){
pthread_cond_wait(&cond_places, &mutex_pl);
}
printf(" barrier opening(IN)");
places = places -1;
sem_post(&evt_maj);
pthread_mutex_unlock(&mutex_pl);
}
}
void * t_exit (void *arg){
while (fin!=TRUE){
sem_wait(&evt_out);
pthread_mutex_lock(&mutex_pl);
if (places==0){
pthread_cond_wait(&cond_places, &mutex_pl);
}
printf("barrier opening IN");
places = places -1;
sem_post(&evt_maj);
pthread_mutex_unlock(&mutex_pl);
}
}
void * t_display (void *arg){
while (fin!= TRUE) {
sem_wait(&evt_maj);
pthread_mutex_lock(&mutex_pl);
printf ("places = %d",places);
pthread_mutex_unlock(&mutex_pl);
}
}
int main() {
/* declarations */
pthread_t thread_keyboard,thread_enter,thread_exit,thread_display;
int ikeyboard,ienter, iexit, idisplay;
pthread_mutex_init(&mutex_pl,NULL);
pthread_cond_init(&cond_places,0);
sem_init(&evt_in,0,0);
sem_init(&evt_out,0,0);
sem_init(&evt_maj,0,0);
iclavier=pthread_create( &thread_keyboard, NULL,&t_keyboard,NULL);
ientree=pthread_create( &thread_enter, NULL,&t_enter,NULL);
isortie=pthread_create( &thread_exit, NULL,&t_exit,NULL);
iaffichage=pthread_create( &thread_display, NULL,&t_display,NULL);
pthread_join( thread_keyboard, NULL);
pthread_join( thread_enter, NULL);
pthread_join( thread_display, NULL);
pthread_join( thread_exit, NULL);
exit(0);
}