You are on page 1of 4

#include <stdio.

h>
#include <pthread.h>
#include <semaphore.h>
#include <string.h>

sem_t eco_wait;
sem_t busi_wait;
sem_t eco_canc;
sem_t busi_canc;
struct plane
{
int avail;
int seat;
char pname[50];
} economic[10],business[6];

void* book_eco(void *pass)


{
char *p=(char*)pass;

int flag=0;
int i;
sem_wait(&eco_wait);
printf("Welcome %s\n",p);
for(i=0; i<10; i++)
{
if(economic[i].avail==0)
{
flag=1;
break;
}
}
if(flag==0)
{ sleep(4);
printf("Sorry Sir, we are fully occupied for
now\n_____________________________________________\n");
sem_post(&eco_wait);
return ;
}
economic[i].avail=1;
strcpy(economic[i].pname,p);
economic[i].seat=i;
printf("Thank You for booking your flight with INDIAN Airlines\n");
printf("Following are your Booking Details\n");
printf("Name: %s",economic[i].pname);
printf("\nSeat No. %d\n",i);
printf("Class:
Economic\n_________________________________________________________________________
_\n");
sleep(4);

sem_post(&eco_wait);
}

void* book_busi(void *pass)


{
char *p=(char*)pass;
int flag=0;
int i;
sem_wait(&busi_wait);
printf("Welcome %s\n",p);
for(i=0; i<6; i++)
{
if(business[i].avail==0)
{
flag=1;
break;
}
}
if(flag==0)
{ sleep(4);
printf("Sorry Sir, we are fully occupied for
now\n_______________________________________________\n");
sem_post(&busi_wait);
return ;
}
business[i].avail=1;
strcpy(business[i].pname,p);
business[i].seat=i;
printf("Thank You for booking your flight with INDIAN Airlines\n");
printf("Following are your Booking Details\n");
printf("Name: %s",business[i].pname);
printf("\nSeat No. %d\n",i);
printf("Class:
Business\n_________________________________________________________________________
______\n");
sleep(4);
sem_post(&busi_wait);
}

void *cancel_booking_eco(void *pass)


{ //printf("IN finction 1\n");
int j=0,i;
char *p=(char*)pass;
sem_wait(&eco_canc);
//printf("Welcome %s\n",p);

for(i=0; i<10; i++)


{
if(strcmp(p,economic[i].pname)==0)
{
economic[i].avail=0;
printf("%s your Flight has been
Cancelled\n_______________________________________________________\n",p);
strcpy(economic[i].pname,"");
sleep(1);
j=1;
}
}
if(j==0)
{
printf("Invalid Credentials\n");
return ;
}
sem_post(&eco_canc);
}
void *cancel_booking_busi(void *pass)
{
printf("IN finction 2\n");
int j=0,i;
char *p=(char*)pass;
sem_wait(&busi_canc);
printf("Welcome %s\n",p);

//printf("Please provide your seat no.\n");


//scanf("%d",&j);
for(i=0; i<6; i++)
{
if(strcmp(p,business[i].pname)==0)
{
business[i].avail=0;
printf("%s your Flight has been
Cancelled\n__________________________________________\n",p);
strcpy(business[i].pname,"");
sleep(1);
j=1;
}
}
if(j==0)
{
printf("Invalid Credintials\n");
return ;
}
sem_post(&busi_canc);
}

int main()
{
pthread_t tid[20],tcan[5];
sem_init(&eco_wait,0,1);
sem_init(&busi_wait,0,1);
sem_init(&eco_canc,0,1);
sem_init(&busi_canc,0,1);
//printf("Hello WOlrd\n");
char pnames[20][50]= {

"Elenore Paredes",
"Dionne Pleasants",
"Erna Woerner",
"Karri Sloat",
"Luann Dipasquale",
"Jenette Toler",
"Louis Rossetti",
"Inell Keehn",
"Amberly Samayoa",
"Dudley Naumann",
"Branden Rust",
"Leonel Holsey",
"Trinity Hodes",
"Deb Fossum",
"Dovie Starkey",
"Hilaria Cano",
"Regan Burnham",
"Kenny Maria",
"Jane Skeens",
"Roxy Stephan"
};
int i;
for(i=0; i<12; i++)
{
pthread_create(&tid[i],NULL,book_eco,(void *)pnames[i]);
//pthread_join(tid[i],NULL);
}
for(i=0; i<12; i++)
{
pthread_join(tid[i],NULL);
}
for(i=12; i<=19; i++)
{
pthread_create(&tid[i],NULL,book_busi,(void *)pnames[i]);
//pthread_join(tid[i],NULL);
}
for(i=12; i<=19; i++)
{
pthread_join(tid[i],NULL);
}
//pthread_create(&tcan[0],NULL,cancel_booking_busi,(void *)pnames[12]);
pthread_create(&tcan[1],NULL,cancel_booking_eco,(void *)pnames[5]);
pthread_create(&tcan[2],NULL,cancel_booking_eco,(void *)pnames[4]);
//pthread_join(tcan[0],NULL);
pthread_join(tcan[1],NULL);
pthread_join(tcan[2],NULL);
}