操作系统中的生产者和消费者问题

时间:2016-03-13 09:15:42

标签: c# c#-4.0 operating-system c#-3.0 c#-2.0

使用来自Abraham Silberschatz-Operating System Concepts的书中的c#生产者消费者问题。 我已经在C#中编写了这个伪代码的代码,但是出现了一个警告"第43行检测到无法访问的代码" ......我是编程新手。需要一些小指南来解决这个问题!

书中给出的伪代码:

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

制片:

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;
}

消费者:

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 */
}

我的代码:

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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

正如Soner所提到的,第一个无限循环阻止第二个循环运行。要使“消费者 - 生产者”对工作,您必须在并发线程中运行这两个部分。这是一个例子:

    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);
        }
    } 

请记住,这是一种无效的实施方式,应该被视为“生产者 - 消费者”模式的原始示例,而不是指南。这段代码将不必要地为/*donothing*/消耗大量的计算能力,这基本上是一个SpinWait。