0

Producer consumer problem using c# from book Abraham Silberschatz-Operating System Concepts. I have wrote code of this pseudocode in C# ,,, but a warning occurs "Unreachable code detected in line 43"...... i am new to programming ..little guide is needed to solve this problem !

pseudo-code given in book:

 #define BUFFER_SIZE 5
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;

Producer:

item next_produced;
while (true) {
/* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}

Consumer:

item next_consumed;
while (true) { while (in == out)
; /* do nothing */ next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
/* consume the item in next consumed */
}

My Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace producer_consumer_problem_Csharp
{
    struct item
    {
        private int iData;
        public item(int x)
        {
            this.iData = x;
        }

    };
    class Program
    {

        static void Main(string[] args)
        {
            int Buffer_Size = 5;
            item[] buffer = new item[Buffer_Size];
            int inp = 0;
            int outp = 0;

            item next_produced;
            while(true)
            {
                Console.WriteLine("Enter the next produced item");
                int x = Convert.ToInt32( Console.ReadLine() );
                next_produced = new item(x);

                while ((inp + 1) % Buffer_Size == outp) ;
                // do nothing
                buffer[inp] = next_produced;
                inp = (inp + 1) % Buffer_Size;

            }


            item next_consumed = new item();
            while (true)
            {
                while (inp == outp);
                /*donothing*/
                next_consumed = buffer[outp];
                outp = (outp +1) % Buffer_Size; /* consume the item in next consumed */
                Console.WriteLine("Next consuumed item is: {0} ", next_consumed);
            }
        }
    }
}
MakesReal
  • 348
  • 5
  • 19
  • Your second `while` loop never be reached since your first one is infinite loop as `while(true)`. – Soner Gönül Mar 13 '16 at 09:17
  • then is it means pseudo code given in book is wrong! ...... i have implemented code from this pseudocode ..... infinite loop is given there . – MakesReal Mar 13 '16 at 09:21
  • 2
    No, the book does not lie. Producer and consumer should run in concurrent threads. – Diligent Key Presser Mar 13 '16 at 09:22
  • Possible duplicate of [Simplest way to run three methods in parallel in C#](http://stackoverflow.com/questions/7320491/simplest-way-to-run-three-methods-in-parallel-in-c-sharp) – Eugene Podskal Mar 13 '16 at 09:30

1 Answers1

0

As Soner mentioned, the first infinite loop prevents second one from running. To get your "consumer-producer" pair to work you have to run both parts in concurrent threads. Here's an example:

    struct item
    {
        private int iData;
        public item(int x)
        {
            this.iData = x;
        }

        public override string ToString()
        {
            return iData.ToString();
        }

    };
    class Program
    {

        static void Main(string[] args)
        {
            int Buffer_Size = 5;
            item[] buffer = new item[Buffer_Size];
            int inp = 0;
            int outp = 0;

            var console_lock = new object();

            new Thread(() =>
            {
                item next_produced;
                while (true)
                {
                    lock (console_lock)
                    {
                        Console.WriteLine("Enter the next produced item");
                        int x = Convert.ToInt32(Console.ReadLine());

                        next_produced = new item(x);
                    }
                    while ((inp + 1) % Buffer_Size == outp) ;
                    // do nothing
                    buffer[inp] = next_produced;
                    inp = (inp + 1) % Buffer_Size;

                }
            }).Start();


            new Thread(() =>
            {
                item next_consumed = new item();
                while (true)
                {
                    while (inp == outp) ;
                    /*donothing*/
                    next_consumed = buffer[outp];
                    outp = (outp + 1) % Buffer_Size; /* consume the item in next consumed */
                    lock (console_lock) Console.WriteLine("Next consuumed item is: {0} ", next_consumed);
                }
            }).Start();

            Thread.Sleep(Timeout.Infinite);
        }
    } 

Please keep in mind that this is a way unefficient implementation and should be considered as just a primitive example of "producer-consumer" pattern, bun not a guide. This code will needlessly consume a lot of your computing power for a /*donothing*/ which is basically a SpinWait.

Diligent Key Presser
  • 4,183
  • 4
  • 26
  • 34