Page 1 of 1

experiment: generate producer consumer problem deadlock

Posted: Mon Oct 30, 2017 8:32 pm
by ggodw000
I was thinking to compile the following code (or something similar) and run perhaps 100s of thousands of times on high-powered server to see if I can generate the deadlock. Definitely, the goal is not to challenge, but be able to see it in my own eye.
Then make improvement and also generate the shortcoming.
At home, I have an ancient HP server with only one physical CPU with just 1 one core.
At work, I have an access to latest Skylake with 200 cores, perhaps I can manage to run million times.
Will it work?

https://en.wikipedia.org/wiki/Producer–consumer_problem

Inadequate implementation (pasted from URL above):

Code: Select all

int itemCount = 0;

procedure producer() 
{
    while (true) 
    {
        item = produceItem();

        if (itemCount == BUFFER_SIZE) 
        {
            sleep();
        }

        putItemIntoBuffer(item);
        itemCount = itemCount + 1;

        if (itemCount == 1) 
        {
            wakeup(consumer);
        }
    }
}

procedure consumer() 
{
    while (true) 
    {

        if (itemCount == 0) 
        {
            sleep();
        }

        item = removeItemFromBuffer();
        itemCount = itemCount - 1;

        if (itemCount == BUFFER_SIZE - 1) 
        {
            wakeup(producer);
        }

        consumeItem(item);
    }
}

Re: experiment: generate producer consumer problem deadlock

Posted: Tue Oct 31, 2017 12:00 pm
by iansjack
ggodw000 wrote: Will it work?
Isn't the point of an experiment to test that sort of question?

Re: experiment: generate producer consumer problem deadlock

Posted: Tue Oct 31, 2017 5:30 pm
by ggodw000
YUP, i guess i will jump right in.