线程在一次迭代后挂起

时间:2016-04-28 01:21:49

标签: c pthreads hang

我有一个应该模拟酒店的程序。 这包括应该与Check-In线程和Check-Out线程交互的Guest线程。

我使用“gcc -pthread HotelProject_v4.c”进行编译 并以“./a.out”执行

我的输出看起来非常适合“Guest 0”,但是一旦它到达它的Guest()线程的末尾,最后一行“从Guest 0收到$ x ......”,它就会挂起,好像它在等待下一个客人说话。

我意识到我的代码中的问题很可能在Main()方法中,但为了节省时间,如果我错了,我已经附上了整个程序。

Main()方法位于代码块的底部。

代码:

//Hotel Project 
//This project creates a system to simulate a hotel.

#include <semaphore.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_GUESTS  10 //10 guests

/*GLOBAL VARIABLE*/ int globalVar0; // Check-in guestID
/*GLOBAL VARIABLE*/ int globalVar1; // Check-out guestID
/*GLOBAL VARIABLE*/ int globalVar2; // Check-out room

void *CheckIn(void *); // the two threads
void *CheckOut(void *);

pthread_t guests[NUM_GUESTS];
pthread_t desk[2];

sem_t roomSem;              //total number of rooms in the hotel
sem_t check_in, check_out;  //reception desks
sem_t s1, s2, s3, s4, s5;   //global semaphores

int rooms[5] = {1,1,1,1,1}; //keep track of availability of each room

int total_guests;       //incremented with each guest check-out
int pool;               //incremented with each guest use pool
int resturant;          //incremented with each guest use resturant
int fitness_center;     //incremented with each guest use fitness center
int business_center;    //incremented with each guest use business center

struct thread_data
{
    int guest_id;   // id number of guest
    int room;       // room guest utilizes
    int event;      // 0=swim, 1=resturaunt, 2=fitness, 3=business
    int cost;       // final cost of stay (random value)
};

struct thread_data thread_data_array[NUM_GUESTS]; // array initialization

void *Guest(void *guestData)
{
int guestid, guestroom, gevent, gcost, sleepVal;
struct thread_data *guest_data;

guest_data = (struct thread_data *) guestData;

/*output*/ printf("Guest %d waits for room to be available\n", guestid);
sem_wait(&roomSem); // guest wants a room; check to see if one is available

/*output*/ printf("Guest %d waits for check-in\n", guestid);
sem_wait(&check_in); // if there is a room available, guest approaches check_in clerk; making him busy
/*output*/ printf("Guest %d goes to the check-in reservationist\n", guestid);
globalVar0 = guestid;
sem_post(&s1);  // event ordering
sem_wait(&s2);  // event ordering
guestroom = guest_data->room;   // gets room value from struct
/*output*/ printf("Guest %d receives Room %d and completes check-in\n", guestid, guestroom);

sem_post(&check_in); // event ordering. Check-In now available

gevent = getRandomLessThan(4);  // activity connected to 0, 1, 2, 3
guest_data[guestid].event = gevent; // assign to Guest's struct

switch(gevent)
{
    case(0): /*output*/ printf("Guest %d does event: Pool\n", guestid);
            pool++;

    case(1): /*output*/ printf("Guest %d does event: Resturant\n", guestid);
            resturant++;

    case(2): /*output*/ printf("Guest %d does event: Fitness Center\n", guestid);
            fitness_center++;

    case(3): /*output*/ printf("Guest %d does event: Business Center\n", guestid);
            business_center++;
}
/*output*/ printf("Guest %d does event: %d\n", guestid, gevent);

sleepVal = getRandomLessThan(3) + 1;
printf("Guest %d sleeps for %d seconds\n", guestid, sleepVal);
sleep(sleepVal); // 0-2 + 1 = 1-3

/*output*/ printf("Guest %d waits for check-out\n", guestid);
sem_wait(&check_out); // guest waits for check-out desk to be available
/*output*/ printf("Guest %d goes to the check-out reservationist and returns room %d\n", guestid, guestroom);
globalVar1 = guestid;
globalVar2 = guestroom;
sem_post(&s3); // event ordering

sem_wait(&s4); // event ordering
gcost = guest_data[guestid].cost;
/*output*/ printf("Guest %d receives the total balance of $%d\n", guestid, gcost);
/*output*/ printf("Guest %d makes a payment\n", guestid);
sem_post(&s5); // event ordering

total_guests++;

pthread_exit(NULL);
}

int getRandomLessThan(int x)
{
srand(time(NULL));
int r = rand() % x;
printf("Random number: %d\n", r);

return r;
}

int GetOpenRoom()
{
int i;
for (i=0; i<5; i++) // rooms.size and rooms.length throw errors
{
    if (rooms[i] = 1) // room available
    {
        rooms[i] = 0; // set to unavailable
        return i;
    }
}
}

void *CheckIn(void *guestData)
{
int i, guestid, guestroom;

for(i=0; i<NUM_GUESTS; i++)
{
    sem_wait(&s1); // event ordering

    guestid = globalVar0; // get the global variable set to the guestID
    /*output*/ printf("The check-in reservationist greets Guest %d\n", guestid);

    sem_wait(&roomSem); // use a room
    guestroom = GetOpenRoom(); // method returns index of open room
    thread_data_array[guestid].room = guestroom; // assign room to guest

    /*output*/ printf("Assign room %d to Guest %d\n", guestroom, guestid);

    sem_post(&s2); // event ordering
}

pthread_exit(NULL);
}


void *CheckOut(void *guestData)
{
int i, guestid, guestroom, gcost;

for(i=0; i<NUM_GUESTS; i++)
{
    sem_wait(&s3); // event ordering

    guestid = globalVar1;
    //guestroom = globalVar2;
    guestroom = thread_data_array[guestid].room;

    /*output*/ printf("The check-out reservationist greets Guest %d and receives the key from room %d\n", guestid, guestroom);
    rooms[guestroom] = 1; // room now available, set back to 1
    sem_post(&roomSem); // release a room

    /*output*/ printf("Calculate the balance for Guest %d\n", guestid);
    gcost = getRandomLessThan(300);     // 0 < cost < 300
    thread_data_array[guestid].cost = gcost;

    sem_post(&s4); // event ordering
    sem_wait(&s5); // event ordering
    /*output*/ printf("Receive $%d from Guest %d and complete the check-out\n", gcost, guestid);
}

pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
int rc;
int guest;
//struct thread_data *guest_data; //delete?

sem_init(&roomSem, 1, 5); //not 0 means shared between multiple processes, 5 is initial value
sem_init(&check_in, 1, 1);
sem_init(&check_out, 1, 1);
sem_init(&s1, 1, 0);
sem_init(&s2, 1, 0);
sem_init(&s3, 1, 0);
sem_init(&s4, 1, 0);
sem_init(&s5, 1, 0);

//rooms[5] = {1,1,1,1,1};

pthread_create(&desk[0], NULL, CheckIn, NULL);  // Check In runs on it's own now
pthread_create(&desk[1], NULL, CheckOut, NULL); // Check Out runs on it's own now

for(guest = 0; guest < NUM_GUESTS; guest++)
{
    thread_data_array[guest].guest_id = guest; // guest ID for each guest thread
    /*Output*/printf("Main(): Creating Thread: %d\n", guest);
    rc = pthread_create(&guests[guest], NULL, Guest, (void *)&thread_data_array[guest]); //Guest method, &thread_data_array[guest] argument
    if (rc) {
        printf("ERROR; return code from pthread_create(Guest) is %d\n", rc);
        exit(-1);
    }
}

pthread_join(desk[0], NULL);
pthread_join(desk[1], NULL);
for(guest = 0; guest < NUM_GUESTS; guest++)
{
    pthread_join(guests[guest], NULL); // waits for each guest to complete and cleans memory space
}

printf("\t\tNumber of Customers\n");
printf("Total Guests:\t\t%d\n", total_guests);
printf("Pool:\t\t\t%d\n", pool);
printf("Resturant:\t\t%d\n", resturant);
printf("Fitness Center:\t\t%d\n", fitness_center);
printf("Business Center:\t%d\n", business_center);

sem_close(&roomSem);
sem_close(&check_in);
sem_close(&check_out);
sem_close(&s1);
sem_close(&s2);
sem_close(&s3);
sem_close(&s4);
sem_close(&s5);

}

0 个答案:

没有答案