资源简介

实验一:生产者消费者[C#][test1] 实验二:处理机调度[C#][test2][时间片轮转法] 实验三:存储管理[C#][test3-1][test3-2][分页存储、缺页中断] 实验四:文件系统[C#][test4]

资源截图

代码片段和文件信息

using System;
using System.Threading;

public class Synchronized
{
    private string[] buffer = { ““ ““ ““ };
    private int bufferCount = 0;
    private int readLocation = 0 writeLocation = 0;

    public Synchronized()
    {
    }

    public string getBuffer()
    {
        lock (this)
        {
            Thread.Sleep(300);
            while(bufferCount == 0)
            {
                Console.WriteLine(“数据为空,无法读取“);
                Monitor.Wait(this);
            }
            string readValue = buffer[readLocation];
            bufferCount--;
            readLocation=(readLocation+1)%buffer.Length;
            Monitor.PulseAll(this);
            return readValue;
        }
    }
    public void setBuffer(string writeValue)
    {
        lock (this)
        {
            Thread.Sleep(300);
            while (bufferCount == buffer.Length)
            {
                Console.WriteLine(“缓冲区满,无法写入“);
                Monitor.Wait(this);
            }
            buffer[writeLocation] = writeValue;
            bufferCount++;
            writeLocation = (writeLocation + 1) % buffer.Length;
            Monitor.PulseAll(this);
        }
    }
}

public class Producer
{
    Synchronized shared;
    public Producer(Synchronized sharedLocation)
    {
        shared = sharedLocation;
    }
    public void produce(){
        string name = Thread.CurrentThread.Name;
        string tag = “(No.“ + name[name.Length - 1]+“)“;
        for (int count = 1; count <= 100; count++)
        {
            shared.setBuffer(tag+“.“+count);
            Console.WriteLine(name + “ 生产:“ + tag + “.“ + count);
        }
        
        Console.WriteLine(name + “done prodcuing.“);
    }
}

public class Consumer
{
    private string value;
    Synchronized shared;
    public Consumer(Synchronized sharedLocation)
    {
        shared = sharedLocation;
    }
    public void consome()
    {
        string name = Thread.CurrentThread.Name;
        for (int count = 1; count <=100; count++)
        {
            value = shared.getBuffer();
            Console.WriteLine(name + “ 消费:“ + value);
        }
        
        Console.WriteLine(name+“done consuming.“);
    }
}

public class ThreadTest
{
    public static void Main()
    {
        Synchronized shared = new Synchronized();
        Producer producer1 = new Producer(shared);
        Consumer consumer1 = new Consumer(shared);
        Thread producerThread = new Thread(new ThreadStart(producer1.produce));
        producerThread.Name = “生产者1“;
        Thread producerThread2 = new Thread(new ThreadStart(producer1.produce));
        producerThread2.Name = “生产者2“;
        Thread producerThread3 = new Thread(new ThreadStart(producer1.produce));
        producerThread3.Name = “生产者3“;
        Thread producerThread4 = new Thread(new ThreadStart(producer1.produce));
        producerThread4.Name = “生产者4“;
        Thread consum

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        3549  2018-06-01 20:14  test1.cs
     文件        3451  2018-06-01 20:12  test2.cs
     文件        3786  2018-06-01 20:12  test3-1.cs
     文件        4754  2018-06-01 20:11  test3-2.cs
     文件       16105  2018-06-01 20:13  test4.cs

评论

共有 条评论