一、实习内容
模拟实现同步机构,以避免发生进程执行时可能出现的与时间有关的错误。
二、实习目的
当进程并发执行时,如果对进程访问的共享变量不加限制,就会产生“与时间有关”的错误。为了防止这类错误,系统必须用同步机构来控制进程对共享变量的访问。
一般说,同步机构是由若干条同步原语所组成。本实验要求学生模拟P、V操作同步机构的实现,模拟进程的并发执行,了解进程并发执行时同步机构的作用。
三、实习题目
模拟P、V操作实现同步机构,且用P、V操作解决生产者—消费者问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//每运行一条指令都要敲一下回车
namespace CaoZuoXiTong5
{
class Program
{
static int[] area = new int[10];
static int empty = 10;
static int full = 0;
static int direction = 0;//0表示都可以,1表示只能运行producer,2表示只能运行consumer
static void Main(string[] args)
{
int a;
Console.WriteLine("随机运行Producer Consumer");
Producer producer = new Producer();
Consumer consumer = new Consumer();
while (true)
{
Random x = new Random();
a = x.Next(1, 3);
if (direction == 1)
{
a = 1;
}
else if (direction == 2)
{
a = 2;
}
switch (a)
{
case 1:producer.Choose();break;
case 2:consumer.Choose();break;
default:break;
}
Console.WriteLine("---------------------\n缓冲区:{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}",area[0], area[1], area[2], area[3], area[4], area[5], area[6], area[7], area[8], area[9]);
Console.WriteLine("Producer:{0} Consumer:{1}",producer.Statement,consumer.Statement);
Console.WriteLine("Producer断点:{0} Consumer断点:{1}",producer.Point,consumer.Point);
Console.ReadLine();
}
}
static int P( ref int x)
{
int a = x;
a--;
if (a< 0) return 0;//不能运行,需要等待
else
{
x--;
return 1;//可以运行
}
}
static void V(ref int x)
{
x++;
}
class Consumer
{
public int Point { get; set; }
public string Statement { get; set; }
public Consumer()
{
Point = 0;
Statement = "运行";
}
public void Choose()
{
switch (this.Point)
{
case 0:A(); break;
case 1: B(); break;
case 2: C(); break;
case 3: D();break;
case 4: E(); break;
case 5: A(); break;
default: A(); B(); C(); D(); E(); break;
}
}
public void A()
{
int judge = P(ref full);
Console.WriteLine("Consumer:产品P操作");
if (judge == 0)
{
Console.WriteLine("Consumer:Consumer因P操作进程阻塞");
this.Point = 0;
this.Statement = "等待";
direction = 1;
}
else
{
this.Point = 1;
this.Statement = "运行";
}
}
public void B()
{
Console.WriteLine("Consumer:取出一个产品");
for (int i = 0; i < 10; i++)
{
if (area[i] == 1)
{
area[i] = 0;
break;
}
}
this.Point = 2;
}
public void C()
{
V(ref empty);
direction=0;
Console.WriteLine("Consumer:缓冲区V操作");
this.Point = 3;
}
public void D()
{
Console.WriteLine("Consumer:消耗一个产品:1");
this.Point = 4;
}
public void E()
{
Console.WriteLine("Consumer:GOTO");
this.Point = 5;
}
}
class Producer
{
public int Point { get; set; }
public string Statement { get; set; }
public Producer()
{
Point = 0;
Statement = "运行";
}
public void Choose()
{
switch (this.Point)
{
case 0: A(); break;
case 1: B(); break;
case 2: C(); break;
case 3: D(); break;
case 4: E(); break;
case 5: A();break;
default: A(); B(); C(); D(); E(); break;
}
}
public void A()
{
Console.WriteLine("Producer:生产了一个产品:1");
this.Point = 1;
}
public void B()
{
int judge = P(ref empty);
Console.WriteLine("Producer:缓冲区P操作");
if (judge == 0)
{
Console.WriteLine("Producer:Producer因P操作进程阻塞");
direction = 2;
this.Point = 1;
this.Statement = "等待";
}
else this.Point = 2;
this.Statement = "运行";
}
public void C()
{
for (int i = 0; i < 10; i++)
{
if (area[i] == 0)
{
area[i] = 1;
break;
}
}
Console.WriteLine("Producer:放入一个产品");
this.Point = 3;
}
public void D()
{
V(ref full);
direction=0;
Console.WriteLine("Producer:产品V操作");
this.Point = 4;
}
public void E()
{
Console.WriteLine("Producer:GOTO");
this.Point = 5;
}
}
}
}
才疏学浅,如有错误请多指正