Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Collections.Generic;
  4.  
  5. namespace philosophers_os
  6. {
  7. public class Fork
  8. {
  9. private Mutex m = new Mutex();
  10.  
  11. public void take(){
  12. m.WaitOne();
  13. }
  14.  
  15. public void put() {
  16. m.ReleaseMutex();
  17. }
  18. };
  19.  
  20. public class Philosopher
  21. {
  22. public int id;
  23. public Fork leftFork;
  24. public Fork righFork;
  25. public uint eatCount;
  26. public double waitTime;
  27. public DateTime waitStart;
  28. public bool stopFlag;
  29. public bool debugFlag;
  30. Random random;
  31.  
  32. public bool fL = false;
  33. public bool fR = false;
  34.  
  35. void think()
  36. {
  37. if (this.debugFlag){
  38. Console.WriteLine(this.id + " thinking");
  39. }
  40.  
  41. Thread.Sleep(this.random.Next(0, 100));
  42.  
  43. if (this.debugFlag){
  44. Console.WriteLine(this.id + " hungry");
  45. }
  46.  
  47. this.waitStart = DateTime.Now;
  48. }
  49.  
  50. void eat()
  51. {
  52. this.waitTime += DateTime.Now.Subtract(this.waitStart).TotalMilliseconds;
  53. if (this.debugFlag){
  54. Console.WriteLine(this.id + " eating");
  55. }
  56.  
  57. Thread.Sleep(this.random.Next(0, 100));
  58.  
  59. eatCount++;
  60. }
  61.  
  62. public Philosopher(int number, Fork left, Fork right, bool dbg)
  63. {
  64. this.id = number;
  65. this.leftFork = left;
  66. this.righFork = right;
  67. this.eatCount = 0;
  68. this.waitTime = 0;
  69. this.debugFlag = dbg;
  70. this.stopFlag = false;
  71. this.random = new Random();
  72.  
  73.  
  74. }
  75.  
  76. public void run()
  77. {
  78. while (!stopFlag)
  79. {
  80. think();
  81.  
  82.  
  83. if (Waiter.WantFork(this, Side.Left))
  84. {
  85.  
  86. //Waiter.freeForks--;
  87. this.leftFork.take();
  88. fL = true;
  89. if (this.debugFlag)
  90. {
  91. Console.WriteLine(this.id + " took left fork");
  92. }
  93. }
  94. else {
  95. Console.WriteLine("-------------------------");
  96. }
  97.  
  98. if (Waiter.WantFork(this, Side.Right))
  99. {
  100. //Waiter.freeForks--;
  101. this.righFork.take();
  102. fR = true;
  103. if (this.debugFlag)
  104. {
  105. Console.WriteLine(this.id + " took right fork");
  106. }
  107. }
  108. if (fR && fL)
  109. eat();
  110.  
  111. if (fR)
  112. {
  113. this.righFork.put();
  114. Waiter.freeForks++;
  115. fR = false;
  116. //Waiter.PutFork(this, Side.Right);
  117. if (this.debugFlag)
  118. {
  119. Console.WriteLine(this.id + " put right fork");
  120. }
  121.  
  122. }
  123.  
  124. if (fL)
  125. {
  126. this.leftFork.put();
  127. Waiter.freeForks++;
  128. fL = false;
  129. //Waiter.PutFork(this, Side.Left);
  130. if (this.debugFlag)
  131. {
  132. Console.WriteLine(this.id + " put left fork");
  133. }
  134.  
  135. }
  136. }
  137. }
  138.  
  139. public void stop(){
  140. stopFlag = true;
  141. }
  142.  
  143. public void printStats(){
  144. Console.WriteLine(this.id + " " + this.eatCount + " " + Convert.ToInt32(this.waitTime));
  145. }
  146. };
  147.  
  148. /*
  149. public static class Waiter
  150. {
  151. static List<Fork> forks = new List<Fork>();
  152. static List<Philosopher> phils = new List<Philosopher>();
  153. public static int freeForks = 0;
  154. static List<bool> forksToAc = new List<bool>();
  155.  
  156. public static void WaiterRem(Philosopher[] ps, Fork[] fs) {
  157. foreach (var p in ps) {
  158. Waiter.phils.Add(p);
  159. }
  160. foreach (var f in fs)
  161. {
  162. Waiter.forks.Add(f);
  163. forksToAc.Add(true);
  164. }
  165.  
  166. freeForks = fs.Length;
  167.  
  168. }
  169.  
  170. public static bool WantFork(Philosopher philosopher, Side side){
  171. bool perm = false;
  172. int n = phils.IndexOf(philosopher);
  173. if (freeForks > 1) {
  174. if (forksToAc[n] && side == Side.Right)
  175. {
  176. forksToAc[n] = false;
  177. perm = true;
  178. }
  179. else
  180. {
  181. if (forksToAc[(n + 1) % forks.Count] && side == Side.Left)
  182. {
  183. forksToAc[(n + 1) % forks.Count] = false;
  184. perm = true;
  185. }
  186. }
  187. }
  188. if (perm)
  189. {
  190. freeForks--;
  191. }
  192. else {
  193. Console.WriteLine("Waiter: NO(");
  194. }
  195.  
  196. return perm;
  197. }
  198.  
  199. public static void PutFork(Philosopher philosopher, Side side)
  200. {
  201. int n = phils.IndexOf(philosopher);
  202. if (side == Side.Right)
  203. {
  204. forksToAc[n] = true;
  205. }
  206. else {
  207. forksToAc[(n + 1) % forks.Count] = true;
  208. }
  209.  
  210. freeForks++;
  211. }
  212. }*/
  213.  
  214. public static class Waiter
  215. {
  216. //static List<Fork> forks = new List<Fork>();
  217. //static List<Philosopher> phils = new List<Philosopher>();
  218. public static int freeForks = 0;
  219. //static List<bool> forksToAc = new List<bool>();
  220.  
  221. /*
  222. public static void WaiterRem(Philosopher[] ps, Fork[] fs)
  223. {
  224. foreach (var p in ps)
  225. {
  226. Waiter.phils.Add(p);
  227. }
  228. foreach (var f in fs)
  229. {
  230. Waiter.forks.Add(f);
  231. forksToAc.Add(true);
  232. }
  233.  
  234. freeForks = fs.Length;
  235.  
  236. }
  237. */
  238. public static void WaiterRem(int count) {
  239. freeForks = count;
  240. }
  241.  
  242.  
  243. public static bool WantFork(Philosopher philosopher, Side side)
  244. {
  245. bool perm = false;
  246. if (freeForks > 1)
  247. {
  248. if (side == Side.Left)
  249. freeForks--;
  250. Console.WriteLine("Waiter: YES)))");
  251. return true;
  252. }
  253. Console.WriteLine("Waiter: NO(");
  254.  
  255. return perm;
  256. }
  257.  
  258. public static void PutFork(Philosopher philosopher, Side side)
  259. {
  260. freeForks++;
  261. }
  262. }
  263.  
  264. public enum Side{
  265. Left = 0,
  266. Right = 1
  267. }
  268.  
  269. class Program
  270. {
  271. static void Main(string[] args)
  272. {
  273. int N = 5;
  274. bool dbg = true;
  275. int duration = 60000;
  276.  
  277. Fork[] forks = new Fork[N];
  278. for (int i = 0; i < N; i++){
  279. forks[i] = new Fork();
  280. }
  281.  
  282. Philosopher[] phils = new Philosopher[N];
  283. for (int i = 0; i < N; i++){
  284. phils[i] = new Philosopher(i + 1, forks[(i + 1) % N], forks[i], dbg);
  285. }
  286.  
  287. //Waiter.WaiterRem(phils, forks);
  288. Waiter.WaiterRem(forks.Length);
  289.  
  290. Thread[] runners = new Thread[N];
  291. for (int i = 0; i < N; i++){
  292. runners[i] = new Thread(phils[i].run);
  293. }
  294. for (int i = 0; i < N; i++){
  295. runners[i].Start();
  296. }
  297.  
  298. Thread.Sleep(duration);
  299.  
  300. for (int i = 0; i < N; i++){
  301. phils[i].stop();
  302. }
  303.  
  304. for (int i = 0; i < N; i++){
  305. runners[i].Join();
  306. }
  307.  
  308. for (int i = 0; i < N; i++){
  309. phils[i].printStats();
  310. }
  311. }
  312. }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement