experiment: generate producer consumer problem deadlock

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
ggodw000
Member
Member
Posts: 396
Joined: Wed Nov 18, 2015 3:04 pm
Location: San Jose San Francisco Bay Area
Contact:

experiment: generate producer consumer problem deadlock

Post 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);
    }
}
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: experiment: generate producer consumer problem deadlock

Post by iansjack »

ggodw000 wrote: Will it work?
Isn't the point of an experiment to test that sort of question?
ggodw000
Member
Member
Posts: 396
Joined: Wed Nov 18, 2015 3:04 pm
Location: San Jose San Francisco Bay Area
Contact:

Re: experiment: generate producer consumer problem deadlock

Post by ggodw000 »

YUP, i guess i will jump right in.
key takeaway after spending yrs on sw industry: big issue small because everyone jumps on it and fixes it. small issue is big since everyone ignores and it causes catastrophy later. #devilisinthedetails
Post Reply