Skip to content

Releases

Ticket Table Synch

Choose a tag to compare

@KBruy KBruy released this 03 Jan 16:51
79a4f09

Obecny system monitorujący sprawdza czy sąsiadujące widelce są wolne oraz czy sąsiadujący filozofowie nie jedli dawniej niż "ten chcący".

Dzięki temu usunięto FIFO, które pozwalało na jedznie tylko przez jednego filozofa na raz.

Za utrzymanie sprawieliwości odpowiada teraz system biletów. Każdy filozof po zjedzeniu otrzymuje bilet który w przypadku sytuacji gdzie dwóch filozofów może jeść ale muszą zabrać "swoje" widelce, decyduje o tym kto ma priorytet.


The current monitoring system checks whether adjacent forks are free and whether neighboring philosophers have not eaten before the "one who wants to eat."

This eliminates the FIFO system, which allowed only one philosopher to eat at a time.

A ticket system is now responsible for maintaining fairness. After each philosopher eats, they receive a ticket, which, in the event that two philosophers can eat but must take their own forks, determines who has priority.

What's Changed

  • Refactor Table and UI components for improved functionality and clarity by @KBruy in #3

Full Changelog: v.1.0...v.2.0

ver. 1.0

ver. 1.0 Pre-release
Pre-release

Choose a tag to compare

@KBruy KBruy released this 02 Jan 18:26
1642dbc

Problem ucztujących filozofów rozwiązuję podejściem współbieżnym: każdy filozof działa w osobnym wątku, a wspólny stan (aktualny stan filozofa i liczba jego posiłków) trzymany jest w jednej współdzielonej strukturze, chronionej mutexem. Filozofowie tylko aktualizują ten stan, a osobny wątek UI okresowo robi krótki „snapshot” danych pod mutexem (kopiuje wektory do lokalnych zmiennych) i na tej podstawie rysuje widok w ncurses. Dzięki temu synchronizacja jest prosta (jeden mutex na wspólny stan), unika się wyścigów danych, a blokady są trzymane bardzo krótko, więc filozofowie i UI nie blokują się nawzajem na dłużej niż potrzeba.

Jednak występuje tu problem wydajności. Z racji na FIFO tylko jeden filozof może jeść w danej chwili. Inni są zatrzymani, czekają na odłożenie widelców i na swoją kolej.


I solve the problem of feasting philosophers with a concurrent approach: each philosopher runs in a separate thread, and the shared state (the philosopher's current state and the number of meals) is kept in a single shared structure, protected by a mutex. The philosophers only update this state, while a separate UI thread periodically takes a short "snapshot" of the data under the mutex (copying vectors to local variables) and draws the view in ncurses based on that. This makes synchronization simple (one mutex per shared state), avoids data races, and locks are held very briefly, so the philosophers and UI don't block each other for longer than necessary.

However, there is a performance problem. Due to the FIFO, only one philosopher can eat at a time. The others are held, waiting for their forks to be put away and for their turn.