I am trying to build an elevator simulator program and I am getting started with the passenger function. I need to create a thread for each passenger and ask them to input their floor number. I have been able to create the no of passengers (threads) needed and I just need to ask them for their input. But I haven't been able to figure that out
code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#define MAX_PASSENGERS 5
#define floor 5
#define NUM_THREAD 6
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER; /*Mutex Initializer*/
int dest_floor;
int elevator[floor];
int current_floor;
int no_passengers;
int t, user_input;
void *passengers(void *threadid)
{
// Getting thread ID
long tid;
tid = (long)threadid;
printf("Passenger %d Kindly select your floor number :\n", tid);
scanf("%d\n", &user_input); // Where I ask them for input
// End of Getting ID
pthread_exit(NULL);
}
int main()
{
pthread_t thread[NUM_THREAD];
int th;
for (t = 1; t < NUM_THREAD; t++)
{
th = pthread_create(&thread[t], NULL, passengers, (void *)t);
if (th)
{
printf("ERROR Creating Thread\n");
exit(-1);
}
}
pthread_exit(NULL);
}
What I tried:
void *passengers(void *threadid)
{
// Getting thread ID
long tid;
tid = (long)threadid;
printf("Passenger %d Kindly select your floor number :\n", tid);
scanf("%d\n", &user_input); // Where I ask them for input
// End of Getting ID
pthread_exit(NULL);
}
What I was expecting :
Passenger 2 Kindly select your floor number:2
Passenger 3 KIndly select your floor number:4
... and so on
What I got :
Passenger 1 Kindly select your floor number:
Passenger 2 KIndly select your floor number :
Passenger 3 Kindly select your floor number:
Passenger 4 KIndly select your floor number :
Passenger 5 Kindly select your floor number:
2
3
4
5
6
It doesn't allow the user to input a floor number on the prompt, but prints out all the threads and then collects the input.