Beispiel #1
0
int main(void)
{
    char user[10], pass[10];
    int logres, seats, bookres, bookch, loginch, usercheck;
    int maxseats = 20; //hardcoded max seats available
    system("cls");
    printf("\nWelcome to Railway booking system!\n");
	//Provide option to login or sign up
    printf("1. Press 1 to Login if you have already signed up.\n");
    printf("2. Press 2 to Sign Up.\n\n");
    printf("Your Choice?: ");
    scanf("%d", &loginch);
    usercheck = logincheck(loginch); //Check if user opted for login or sign-up
    if(usercheck == 1) //user opted for login in loginch, logincheck() returns 1
    {
    A:	//label to return to login after sign-up
        printf("\nUser Login:\nPlease enter your username and password:\n");
        printf("Username: "******"%s", user);
        printf("Password: "******"%s", pass);
        logres = login(user, pass); //call to login, checks if user name and pass are correct
        if(logres == 1) //login success if login returns 1 above, ie. user name and pass are correct
        {
            printf("Login successful!\n\nPlease proceed for booking!\n20 Seats available!");
            printf("\nPlease enter number of seats: ");
            scanf("%d", &seats); 
            bookres = booking(seats, maxseats); //allow booking only if login success, 
												//call booking() with no of seats user enters and max seats 
            if(bookres == 1)//booking() above checks if enough seats are available, returns 1 if available
            {
                printf("\nEnough seats available! Proceed for booking.\n\n");
				//Allow class selection if seats available
                printf("Please select your class:\n");
                printf("1.Enter 1 for AC 1 Tier.\n");
                printf("2.Enter 2 for AC 2 Tier.\n");
                printf("3.Enter 3 for AC 3 Tier.\n");
                printf("4.Enter 4 for Sleeper.\n");
                printf("\nYour choice?: ");
                scanf("%d", &bookch);
                maxseats = bookmenu(bookch, seats, maxseats); //Actual Booking, call to bookmenu()
            }
            else //booking returns 0 if user entered seats exceeds available seats
            {
                printf("Not enough seats!");
            }
        }
        else //login returns 0 if username OR pass is incorrect, comparison fails
        {
            printf("Invalid username or password!");
        }
    }
    else if(usercheck == 2)//user opted for sign-up, logincheck() returns 2
    {
        createuser();//Call user creation module
        system("cls");
        goto A; //continue with login, after user creation
    }
    printf("\n\nThank You for using the Railway booking system. Press any key to exit!");
    getche();
    return 0;
}
/* A worker thread receives a number and a 4-letter name via targ */
void * tmain(void * targ)
{
   /* Local variables are not shared with other threads */
   int no, i;
   char name[5];
   pthread_t tid;
   int isBooked[t];//Tells how many times train i has been booked by this thread.
   memset(isBooked, 0, sizeof(isBooked));
   int trainsBooked[t];//Tells which all trains have been booked by this thread
   int numTrainsBooked = 0;//Count of the number of trains booked by this thread
   
   /* Retrieve my number and name from the parameter passed */
   no = ((tinfo *)targ) -> tno;
   strcpy(name,((tinfo *)targ) -> tname);

   /* Retrieve my thread id */
   tid = pthread_self();

   while (1) {
      /* Check for termination condition */

      pthread_mutex_lock(&donemutex);

      /* if the master thread is done */
      if(mdone)
      {
         pthread_mutex_unlock(&donemutex);
         pthread_barrier_wait(&barrier);         
         pthread_exit(NULL);
      }
      /* The master thread is still sleeping, so I continue to work */
      pthread_mutex_unlock(&donemutex);


      //Check if number of active queries is less than MAX
      pthread_mutex_lock(&querymutex);
      if(numActiveQueries == MAX)
      {
         printf("\nThread %d : Blocked since active queries = MAX\n", no);
         pthread_cond_wait(&querycond, &querymutex);
      }      
      numActiveQueries++;
      pthread_mutex_unlock(&querymutex);        

      int a123;
      //Start query
      int queryType = 1 + rand()%3;
      if(queryType == INQUIRE)
      {
         inquiry(no);
      }
      else if(queryType == BOOK)
      {
         booking(no, isBooked, trainsBooked, &numTrainsBooked);
      }
      else
      {
         cancellation(no, isBooked, trainsBooked, &numTrainsBooked);
      }

      pthread_mutex_lock(&querymutex);
      numActiveQueries--;
      if(numActiveQueries == (MAX-1))//wake up a waiting query
         pthread_cond_signal(&querycond);
      pthread_mutex_unlock(&querymutex);
      
      sleep(SLEEPTIME);
   }
}