Sažetak | U ovom diplomskom radu dan je pregled razvoja programske podrške za višedretvene aplikacije na principima dijeljene memorije i slanja poruka među programskim nitima. Obje metode promatrane su u C++ programskoj okolini. Kao prirodni način implementacije višedretvenih programa u C++ programskoj okolini nameće se princip dijeljene memorije, upravo jer programske niti međusobno dijele adresni prostor te stoga nije potrebno implementirati posebne mehanizme za dijeljenje resursa među njima. Međutim, u tom se slučaju javlja problem prilikom zaštite dijeljenih resursa od nekonzistentnosti, pa je potrebno koristiti veći broj lokota i njihovo međusobno ugnježđivanje. Stoga su složeniji programi podložniji pogreškama uzrokovanim potpunim zastojem ili stanjima natjecanja. S druge strane, princip koji se zasniva na bazi slanja poruka manje je sklon ovakvim pogreškama, ali se potrebno dodatno pobrinuti o mehanizmima razmjene poruka i o broju dijeljenih resursa. Broj dijeljenih resursa potrebno je svesti na minimum, po mogućnosti na samo jedan, koji bi zapravo predstavljao medij za razmjenu poruka među programskim nitima - najčešće red. Ovaj pristup zahtjeva dodatne procesorske i memorijske resurse u usporedbi s pristupom s dijeljenom memorijom. U diplomskom radu razvijena je biblioteka za razmjenu poruka među programskim nitima te su, korištenjem razvijene biblioteke, implementirani algoritmi simulacije rada bankomata i problem pet filozofa. Medij za razmjenu poruka upravo je red koji je osiguran sa samo jedim lokotom. Tim lokotom je osigurana konzistentnost elemenata u redu, pa je stoga takav red sigurno koristiti u višedretvenoj okolini. Okosnicu biblioteke čini taj red, koji je upakiran u klase za slanje i primanje poruka, te je time njegova implementacija skrivena od korisnika biblioteke. Dodatno je osigurana fleksibilnost razmjene poruka tako da se različiti tipovi poruka mogu slati i primati kroz jedan te isti medij. Na kraju rada zaključujem da je pristup s dijeljenom memorijom bolji prilikom razvoja jednostavnih sustava kojima mehanizmi zaštite dijeljenih resursa nisu složeni. Dok s druge strane, pristup s razmjenom poruka ima bolje performanse u složenijim aplikacijama iz perspektive otpornosti na nepredviđene pogreške, ali zahtjeva veće ulaganje na početku u razvoj mehanizma razmjene poruka i skuplji je po pitanju hardverskih resursa u usporedbi s prethodnim pristupom. |
Sažetak (engleski) | In this master’s thesis, the outline of multi-threaded application development based on shared memory and message passing methods, is given. Both methods were observed in C++ environment. Straightforward way of multi-threaded application development in C++ is shared memory approach, due to the fact that threads share the address space, hence it is not necessary to implement further sharing mechanisms. However, in that case many issues related to the data integrity can occur, so the programmers have to use a larger number of locks and their nesting. The outcome of such approach, in more complex solutions, is a higher risk of having more issues caused by deadlocks or race conditions. On the other hand, development approach based on message passing between threads is less likely to suffer from a higher number of such issues, but it is necessary to take care of additional data sharing mechanisms and of number of shared data. The number of shared data should be reduced as much as possible, preferably to only one, which will then serve as a medium for message passing. The most usual data structure for this solution is the queue. Compared to shared memory, this approach requires additional CPU and memory resources. In this master’s thesis, the library for message passing was developed, and the algorithms for cash machine simulation and Dining Philosophers issue was implemented using that library. The medium for message passing is the queue which is secured using a single lock. That lock assures the data integrity in the queue in the concurrent program, so the queue can be considered as a thread safe. The central part of the library is right that queue, which is wrapped in the higher classes for sending and receiving messages. That way the queue implementation is hidden for the library users. Additionally, the flexibility of message sharing is ensured in a way that different message structures can be shared through the same media. The conclusion, at the end of the thesis, is that approach with shared memory is better to be used when locking mechanisms requires simple solutions. While, the message passing approach has better performance in more complex solutions from the undefined behavior point of view, but requires big initial invest to develop message passing interface, and it is also more expensive in terms of hardware resources, compared to the first approach. |