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);


}

Dani AI

Generated

For : the lack of response to E/S/F comes from multiple, small mistakes that together stop input handling and thread coordination. 's point about letter case is valid, but the keyboard thread and the enter/exit logic also need fixes (reading inside the loop, correct increments/decrements, signalling, and visible output).

Key problems and fixes to apply:

  • Read input inside the keyboard loop and normalize case (or accept both cases). Calling getchar() just once outside the loop will never see subsequent keypresses.
  • printf without a newline (or without fflush(stdout)) can look like "nothing happens" because output is buffered. Add \n or call fflush.
  • The exit task must increment available places; the enter task decrements. Use while(condition) pthread_cond_wait(...) (not an if) to handle spurious wakeups. After changing places in the exit task, call pthread_cond_signal or pthread_cond_broadcast to wake threads waiting to enter.
  • Protect shared state (places, the shutdown flag) with the mutex or use an atomic type; set the shutdown flag under the mutex and wake any blocked threads (post semaphores and broadcast the condvar) to allow clean shutdown.
  • Check pthread_create return values and use consistent variable names. Thread functions should return NULL at end.

Example patterns (do not drop these straight into the original unchanged; adapt names and error handling):

/* keyboard thread: read repeatedly, skip newline, normalize case */
int c;
while (!fin && (c = getchar()) != EOF) {
  char ch = (char)c;
  if (ch == '\n') continue;
  ch = (char)tolower((unsigned char)ch);
  if (ch == 'e') sem_post(&evt_in);
  else if (ch == 's') sem_post(&evt_out);
  else if (ch == 'f') {
    pthread_mutex_lock(&mutex_pl);
    fin = TRUE;
    pthread_mutex_unlock(&mutex_pl);
    /* wake others so they can exit cleanly */
    sem_post(&evt_in); sem_post(&evt_out);
    pthread_cond_broadcast(&cond_places);
  }
}
/* exit thread: wait until there is at least one car inside, then increment places */
while (!fin) {
  sem_wait(&evt_out);
  pthread_mutex_lock(&mutex_pl);
  while (places == MAX_PLACES) pthread_cond_wait(&cond_places, &mutex_pl);
  places++;                       /* car leaves */
  printf("barrier opening (OUT)\n");
  fflush(stdout);
  sem_post(&evt_maj);
  pthread_cond_signal(&cond_places); /* wake enterers */
  pthread_mutex_unlock(&mutex_pl);
}

Making these changes (read loop, case normalization, correct increment/decrement, condvar signalling, flushing output, and proper synchronization) will make the program respond to keystrokes and terminate cleanly.

You're checking for lowercase letters, but your question involved uppercase. Really simple suggestion, but that's what sticks out in my mind from scanning your code quickly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.