concurrencydining-philosopher

Dining philosophers using semaphores (BACI)


I have to solve the dining philosophers problem by using semaphores. In my code every second philosopher is taking a chopstick and the rest are waiting.

I still have some errors in the main function. Can you tell me how to initiate Chopstick[k]? I'm a beginner in BACI.

binarysem ChopStick[5];
binarysem speak;

void philosopher(int index){
int i,k;
int x;
x=0;
for(i=0;i<k;i++){
    cout << "I am philosopher: " << index << " and i am thinking "<< endl;
    signal(speak);
    if(index % 2 == 0){
        wait(ChopStick[index]);
        wait(ChopStick[(index+1) % 5]);
        }
        else{
        wait(ChopStick[index]);
        wait(ChopStick[(index+1) % 5]);
        }
    x++;
    wait(speak);
    cout <<"I am philosopher: "<< index <<" and i eat: "<< x << "times" << endl;
    signal(speak);
    signal(ChopStick[index]);
    signal(ChopStick[(index+1) % 5]);

main(){
int k;
for(k=0;k<5;k++){
    initialsem(ChopStick[k],1);
    initialsem(speak,1);
    }
cobegin
    {
        philosopher(1); philosopher(2); philosopher(3); philosopher(4); philosopher(5);
    }
}

Solution

  • According to this BACI summary, it sounds like arrays should be initialized like in C. So you would want something like:

    binarysem initialsem[5];
    

    (where 5 is the length of the array, and binarysem is the datatype of the array elements)