public DiningPhilosopher() { forks = new[] { new Fork(1), new Fork(2), new Fork(3), new Fork(4), new Fork(5), }; philosophers = new Philosopher[5]; for (int i = 0; i < philosophers.Length; i++) { if (i == philosophers.Length - 1) { philosophers[i] = new Philosopher(i + 1, forks[i], forks[0]); } else { philosophers[i] = new Philosopher(i + 1, forks[i], forks[i + 1]); } } }
public static void TestDiningPhilosophers() { var philosophers = new Philosopher[5]; for (int i = 0; i < philosophers.Length; i++) { philosophers[i] = new Philosopher(philosophers, i); } foreach (var philosopher in philosophers) { philosopher.LeftFork = philosopher.LeftPhilosopher.RightFork ?? new Semaphore(1, 1); philosopher.RightFork = philosopher.RightPhilosopher.LeftFork ?? new Semaphore(1, 1); } var philosopherThreads = new Thread[philosophers.Length]; for (int i = 0; i < philosophers.Length; i++) { philosopherThreads[i] = new Thread(philosophers[i].EatAll); philosopherThreads[i].Start(); } foreach (var thread in philosopherThreads) { thread.Join(); } }
public ActionResult DeleteConfirmed(int id) { Philosopher philosopher = db.Philosophers.Find(id); db.Philosophers.Remove(philosopher); db.SaveChanges(); return(RedirectToAction("Index")); }
public static void Main() { //Crea un array de mutex de tamaño 5, un mutex por cada palillo Mutex[] ArrayPalillosMutex = new Mutex[5]; //inicializa en falso cada uno de los mutex for (int i = 0; i < 5; i++) { ArrayPalillosMutex[i] = new Mutex(false); } //Crea los 5 filosofos for (int i = 0; i < 5; i++) { //cada filosofo tiene su Estructura pd PhilosopherData pd; //Asigna el id i+1 para que no empiece en 0 cada uno de los ID de los filosofos pd.IdFilosofo = i + 1; /*Conditional x ? y : z Evaluates to y if x is true, z if x is false*/ // si (i - 1 >= 0 ) es verdadero entonces hace (i-1), sino 4 /* * Codigo original pero se me hace que esta mal, en esta parte guarda el numero del palillo de cada uno de los * filosofos en la estructura, me parece que el error esta en la asignacion del numero de palillo de la derecha * si seguis la secuencia se obtiene * (palillo izq, palillo der) * (0,4) * (1,4) * (2,1) * (3,2) * (4,3) * * pero si lo dibujas no coinciden con cada filosofo * * pd.PalilloDerecho = ArrayPalillosMutex[i - 1 >= 0 ? (i - 1) : 4]; * pd.PalilloIzquierdo = ArrayPalillosMutex[i]; * pd.CantidadAComer = 5; * pd.TotalFood = 35; * Philosopher p = new Philosopher(pd); * p.Start(); */ /*yo creo que la secuencia correcta es asi*/ pd.PalilloDerecho = ArrayPalillosMutex[i == 4 ? 0 : (i + 1)]; pd.PalilloIzquierdo = ArrayPalillosMutex[i]; pd.CantidadAComer = 5; pd.TotalFood = 35; Philosopher p = new Philosopher(pd); p.Start(); Console.WriteLine("Filosofo {0} palillo izq:{1} palillo der{2} ", pd.IdFilosofo, i, (i == 4 ? 0 : (i + 1))); } //Console.ReadLine(); }
public void HadDeadlock(Philosopher philosopher) { _deadlockMutex.WaitOne(Timeout); _deadLockedPhilosopher.Add(philosopher); if (_deadLockedPhilosopher.Count == 1) { FirstDeadlockOccured = DateTime.Now - Process.GetCurrentProcess().StartTime; } _deadlockMutex.ReleaseMutex(); }
public void Ate(Philosopher philosopher) { _ateMutex.WaitOne(Timeout); if (!_eatRank.ContainsKey(philosopher)) { _eatRank.Add(philosopher, 0); } _eatRank[philosopher] = ++_eatRank[philosopher]; _ateMutex.ReleaseMutex(); }
public void TakeFork(Philosopher acquisitor) { if (acquisitor is null) { throw new ArgumentNullException(nameof(acquisitor)); } WriteLine($"Philosopher {acquisitor.Index} wants to take fork {Index}."); Monitor.Enter(_locker); _acquiredBy = acquisitor; WriteLine($"Philosopher {acquisitor.Index} took fork {Index}."); }
public ActionResult Edit([Bind(Include = "ID,FistName,LastName,DateOfBirth,DateOfDeath,IsAlive,NationalityID,AreaID,Description")] Philosopher philosopher) { if (ModelState.IsValid) { db.Entry(philosopher).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.AreaID = new SelectList(db.Areas, "ID", "AreaName", philosopher.AreaID); ViewBag.NationalityID = new SelectList(db.Nationalities, "ID", "NationalityName", philosopher.NationalityID); return(View(philosopher)); }
public Philosopher[] CreateDiningPhilosophers(int count) { var philosophers = new Philosopher[count]; var forks = Enumerable.Range(0, count).Select(i => new Fork()).ToArray(); for (int i = 0; i < count; i++) { var philosopher = new Philosopher( new DijkstraHands(forks[i], forks[(i + 1) % count])); philosophers[i] = philosopher; } return(philosophers); }
// GET: Philosopher/Details/5 public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Philosopher philosopher = db.Philosopher.Find(id); if (philosopher == null) { return(HttpNotFound()); } return(View(philosopher)); }
public Philosopher[] CreateDiningPhilosophers(int count) { Philosopher[] philosophers = new Philosopher[count]; ChandyMisraHands[] hands = new ChandyMisraHands[count]; for (int i = 0; i < count; i++) { var handsInstance = new ChandyMisraHands(); hands[i] = handsInstance; philosophers[i] = new Philosopher(handsInstance); } AssignForks(philosophers, hands); return(philosophers); }
public ActionResult Delete(int id) { try { Philosopher philosopher = db.Philosopher.Find(id); db.Philosopher.Remove(philosopher); db.SaveChanges(); } catch (DataException ex) { return(RedirectToAction("Delete", new { id = id, saveChangesError = true })); } return(RedirectToAction("Index")); }
// GET: Philosopher/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Philosopher philosopher = db.Philosopher.Find(id); if (philosopher == null) { return(HttpNotFound()); } ViewBag.AreaID = new SelectList(db.Area, "AreaID", "Name", philosopher.AreaID); ViewBag.NationalityID = new SelectList(db.Nationality, "NationalityID", "Name", philosopher.NationalityID); return(View(philosopher)); }
public void ReleaseFork(Philosopher acquiredBy) { if (acquiredBy is null) { throw new ArgumentNullException(nameof(acquiredBy)); } if (_acquiredBy is null || _acquiredBy != acquiredBy) { throw new InvalidOperationException(); } _acquiredBy = null; WriteLine($"Philosopher {acquiredBy.Index} released fork {Index}."); Monitor.Exit(_locker); }
public void PrepareToEat() { for (int i = 0; i < count; i++) { forks[i] = new object(); if (i == 0) { philosophers[i] = new Philosopher(i, (i + 1) % count, i); } else { philosophers[i] = new Philosopher(i, i, (i + 1) % count); } //philosophers[i] = new Philosopher(i, i, (i + 1) % count); } }
public Lunch() { _threads = new Thread[PHILOSOPHERS_COUNT]; _philosophers = new Philosopher[PHILOSOPHERS_COUNT]; _forks = new Fork[FORKS_COUNT]; for (int i = 0; i < FORKS_COUNT; ++i) { _forks[i] = new Fork(i); } for (int i = 0; i < PHILOSOPHERS_COUNT; ++i) { Fork[] curForks = new Fork[2] { _forks[GetForkPosition(i, +0)], _forks[GetForkPosition(i, -1)] }; _philosophers[i] = new Philosopher(i, curForks); _threads[i] = new Thread(new ThreadStart(_philosophers[i].Activate)); } }
private static void SetupDinnerTable() { for (int i = 0; i < _mutexs.Length; i++) { _mutexs [i] = new Mutex(); } for (int i = 0; i < _philosophers.Length; i++) { if (i != 0) { _philosophers [i] = new Philosopher("Philosopher " + (i + 1), _mutexs [i], _mutexs [i - 1]); } else { _philosophers [i] = new Philosopher("Philosopher " + (i + 1), _mutexs [i], _mutexs [_mutexs.Length - 1]); } } }
public ActionResult Delete(int?id, bool?saveChangesError = false) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } if (saveChangesError.GetValueOrDefault()) { ViewBag.ErrorMessage = "Delete failed. Try again. If unable to resolve contact the administrator"; } Philosopher philosopher = db.Philosopher.Find(id); if (philosopher == null) { return(HttpNotFound()); } return(View(philosopher)); }
static void OnPhilosopherStateChanged(Philosopher philosopher) { switch (philosopher.State) { case PhilosopherState.Eating: Console.WriteLine(philosopher.Name + " is now eating with " + philosopher.LeftFork.Name + " and " + philosopher.RightFork.Name); break; case PhilosopherState.Thinking: Console.WriteLine(philosopher.Name + " is thinking..."); break; case PhilosopherState.Waiting: Console.WriteLine(philosopher.Name + " is waiting..."); break; default: break; } }
public ActionResult Create([Bind(Include = "FirstName,LastName,DateOfBirth,DateOfDeath,IsAlive,Description,NationalityID,AreaID")] Philosopher philosopher) { try { if (ModelState.IsValid) { db.Philosopher.Add(philosopher); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (DataException ex) { ModelState.AddModelError("", "Unable to save changes. Try again. If unable to resolve contact the administrator."); } ViewBag.AreaID = new SelectList(db.Area, "AreaID", "Name", philosopher.AreaID); ViewBag.NationalityID = new SelectList(db.Nationality, "NationalityID", "Name", philosopher.NationalityID); return(View(philosopher)); }
static void Main(string[] args) { Student poitStudent = new Student(); poitStudent.Sleep(); poitStudent.WrigthCode(); IPosebel isitStudent = new Student(); isitStudent.Jump(56); isitStudent.Math(); isitStudent.Go(); IPosebel professor = new Philosopher(); professor.Go(); professor.Math(); DoWork(professor); DoWork(isitStudent); }
// Task 3: Dining Philosophers public static void RunTask3() { int numberOfForksAndPhilosophers = 5; Philosopher[] philosophers = new Philosopher[numberOfForksAndPhilosophers]; Task[] philosophersTasks = new Task[numberOfForksAndPhilosophers]; Fork[] forks = new Fork[numberOfForksAndPhilosophers]; for (int i = 0; i < numberOfForksAndPhilosophers; i++) { forks[i] = new Fork(); } Fork leftFork, rightFork; Task philosopherTask; for (int i = 0; i < numberOfForksAndPhilosophers; i++) { leftFork = forks[i]; rightFork = forks[(i + 1) % numberOfForksAndPhilosophers]; if (i == numberOfForksAndPhilosophers - 1) { philosophers[i] = new Philosopher(i + 1, rightFork, leftFork); } else { philosophers[i] = new Philosopher(i + 1, leftFork, rightFork); } philosopherTask = philosophers[i].Run(); philosophersTasks[i] = philosopherTask; } Task.WaitAll(philosophersTasks); }
public Fork() { InUse = false; Philosopher = null; }
private static void OnPhilosopherDoneEating(Philosopher philosopher) { Console.WriteLine(philosopher.Name + " is now done eating"); }
public PhilospherModelView(Philosopher philosopher) { this.philosopher = philosopher; }
/// <summary> /// 产生死锁的哲学家吃饭 /// </summary> /// <returns></returns> static Task PhilosopherEat() { Task t = new Task(() => { int thinkMs = 1000, handMs = 100, eatMs = 100; Chopsticks chopsticks1 = new Chopsticks() { ID = 1 }, chopsticks2 = new Chopsticks() { ID = 2 }, chopsticks3 = new Chopsticks() { ID = 3 }, chopsticks4 = new Chopsticks() { ID = 4 }, chopsticks5 = new Chopsticks() { ID = 5 }; APhilosopher <Chopsticks> p1 = new Philosopher("P1", chopsticks1, chopsticks2) { ThinkMs = thinkMs, HandMs = handMs, EatMs = eatMs }, p2 = new Philosopher("P2", chopsticks2, chopsticks3) { ThinkMs = thinkMs, HandMs = handMs, EatMs = eatMs }, p3 = new Philosopher("P3", chopsticks3, chopsticks4) { ThinkMs = thinkMs, HandMs = handMs, EatMs = eatMs }, p4 = new Philosopher("P4", chopsticks4, chopsticks5) { ThinkMs = thinkMs, HandMs = handMs, EatMs = eatMs }, p5 = new Philosopher("P5", chopsticks5, chopsticks1) { ThinkMs = thinkMs, HandMs = handMs, EatMs = eatMs }; TaskFactory tf = new TaskFactory(); Task t1 = tf.StartNew(p1.Run), t2 = tf.StartNew(p2.Run), t3 = tf.StartNew(p3.Run), t4 = tf.StartNew(p4.Run), t5 = tf.StartNew(p5.Run); Task.WaitAll(t1, t2, t3, t4); }); return(t); }
public PhilosopherRunner(Philosopher philosopher) { philosopher_ = philosopher; }
public Quote(string content, Philosopher philosopher ) { Content = content; Philosopher = philosopher; }