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

producer and consumer issue in operating system

使用 Abraham Silberschatz 操作系统概念一书中的 c# 的生产者消费者问题。 我已经用 C# 编写了这个伪代码的代码,但是出现了警告 "Unreachable code detected in line 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);
            }
        }
    }
}

正如 Soner 提到的,第一个无限循环阻止了第二个 运行ning。为了让你的 "consumer-producer" 对工作,你必须 运行 在并发线程中的两个部分。这是一个例子:

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

请记住,这是一种低效的实现方式,应将其视为 "producer-consumer" 模式的原始示例,而不是指南。对于基本上是 SpinWait 的 /*donothing*/,此代码会不必要地消耗您的大量计算能力。