ALL > Computer and Education > courses > university courses > undergraduate courses > Operating System > ZSTU class(2019-2020-1) > student directories > >
homework-4 Version 0
šŸ‘¤ Author: by 992632427qqcom 2019-10-09 10:53:52
Questionļ¼›There is a reading room for 100 people. Every reader has to regist for the number of seat before enter the room and cancle it when he go out.

Solution:
There are two actions of the reader. One is to fill out a form to regist and enter the reading room. The otherĀ is leaving the reading room. the operation at this time must to consider whether there are readers in the reading room. Besides, readers in the reading roomĀ isĀ not a action change.

using System;
using System.Threading;
namespace ConsoleApp1 {
class Program {
private static Semaphore empty,reader, mutex;
private static int num = 0;
static void Main(string[] args) {
empty = new Semaphore(100, 100);
reader = new Semaphore(0, 100);
mutex = new Semaphore(1, 1);
Thread GetIn = new Thread(getIn);
Thread GetOut = new Thread(getOut);
GetIn.Start(); GetOut.Start();
}
protected static void getIn() {
while (true) {
empty.WaitOne(); // wait for the seat
mutex.WaitOne(); // Apply for registration.Ā Enter the critical area.
Console.WriteLine("Regist for the number of seat");
Thread.Sleep(1000);
Console.WriteLine("Enter the room "+(++num)); Console.WriteLine("");
Thread.Sleep(1000); mutex.Release(); //Registration is completedĀ .LiveĀ the critical area.
//reader.Release(); // readers+1
}
}
protected static void getOut() {
while (true) {
//reader.WaitOne(); // readers-1
mutex.WaitOne(); // Wait forĀ deleting theĀ registration information. Enter the critical area.
Console.WriteLine("Delete theĀ registration information.");
Thread.Sleep(1000);
Console.WriteLine("Live the room "+(--num)); Console.WriteLine("");
Thread.Sleep(1000);
mutex.Release(); // LiveĀ the critical area.
empty.Release(); // Free a seat resource.
}
}
}
}

Please login to reply. Login

Reversion History

Loading...
No reversions found.