Created
March 27, 2020 13:59
-
-
Save annibuliful/7ace8695691e1141992bc964bf02db51 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> //Header file for sleep(). man 3 sleep for details. | |
#include <pthread.h> | |
#define FOUR_SEAT 1 | |
#define TWO_SEAT 2 | |
#define ONE_SEAT 4 | |
#define ONE_MINUTE 60 | |
int getQueue(); | |
void addQueue(int seatNumber); | |
void addNewQueueAfterSeatComplete(); | |
void sleepThreadBySeat(int seatNumber); | |
void* addToSeat(void* number); | |
void listQueue(); | |
int currentFourSeat = 0; | |
int currentTwoSeat = 0; | |
int currentOneSeat = 0; | |
int queue[100]; | |
int currentQueueIndex = 0; | |
int seatTypes[] = {1,2,4}; | |
pthread_mutex_t lock; | |
int getQueue(){ | |
int seatNumber = queue[currentQueueIndex]; | |
currentQueueIndex--; | |
return seatNumber; | |
} | |
void addQueue(int seatNumber){ | |
queue[currentQueueIndex] = seatNumber; | |
currentQueueIndex++; | |
} | |
void addNewQueueAfterSeatComplete(){ | |
int seatNumber = getQueue(); | |
addToSeat(seatNumber); | |
} | |
void sleepThreadBySeat(int seatNumber){ | |
int minute; | |
if(seatNumber == 4){ | |
minute = 3; // 15; | |
}else if(seatNumber == 2){ | |
minute = 2; //10; | |
}else if(seatNumber == 1){ | |
minute = 1; // 5 | |
} | |
sleep(minute); | |
printf("Finish from %d minute\n", minute); | |
addNewQueueAfterSeatComplete(); | |
} | |
void* addToSeat(void* number){ | |
int seatNumber = (int *) number; | |
printf("%d\n",seatNumber); | |
printf("Add To Queue\n"); | |
if(seatNumber == 4){ | |
if(currentFourSeat < FOUR_SEAT){ | |
currentFourSeat++; | |
sleepThreadBySeat(seatNumber); | |
}else{ | |
addQueue(seatNumber); | |
} | |
}else if(seatNumber == 2){ | |
if(currentTwoSeat < TWO_SEAT){ | |
currentTwoSeat++; | |
sleepThreadBySeat(seatNumber); | |
}else{ | |
addQueue(seatNumber); | |
} | |
}else if(seatNumber == 1){ | |
if(currentOneSeat < ONE_SEAT){ | |
currentOneSeat++; | |
sleepThreadBySeat(seatNumber); | |
}else{ | |
addQueue(seatNumber); | |
} | |
} | |
return NULL; | |
} | |
void listQueue(){ | |
printf("List Queue\n"); | |
int i; | |
for(i = 0;i < currentQueueIndex; i++){ | |
printf("%d, ",queue[i]); | |
} | |
printf("\n\n"); | |
} | |
int randInRange(int min, int max) | |
{ | |
return ( rand() % ( max - min ) ) + min; | |
} | |
int main() | |
{ | |
pthread_t thread_id; | |
printf("Before Thread\n"); | |
pthread_mutex_lock(&lock); | |
int i = 0; | |
srand(time(NULL)); | |
while( i < 120){ | |
int randomGenerateQueue = randInRange(1,9); | |
if(randomGenerateQueue < 4){ | |
int seatIndex = randInRange(0,3); | |
int seatNumber = seatTypes[seatIndex]; | |
pthread_create(&thread_id, NULL, addToSeat, (void *) seatNumber); | |
} | |
i++; | |
} | |
pthread_mutex_unlock(&lock); | |
// int j = 0; | |
// while(j < 5){ | |
// | |
// } | |
pthread_join(thread_id, NULL); | |
printf("After Thread\n"); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment