- Parallel Programming with Python
- Jan Palach
- 516字
- 2021-07-16 11:22:19
Communicating in parallel programming
In parallel programming, the workers that are sent to perform a task often need to establish communication so that there can be cooperation in tackling a problem. In most cases, this communication is established in such a way that data can be exchanged amongst workers. There are two forms of communication that are more widely known when it comes to parallel programming: shared state and message passing. In the following sections, a brief description of both will be presented.
Understanding shared state
One the most well-known forms of communication amongst workers is shared state. Shared state seems straightforward to use but has many pitfalls because an invalid operation made to the shared resource by one of the processes will affect all of the others, thereby producing bad results. It also makes it impossible for the program to be distributed between multiple machines for obvious reasons.
Illustrating this, we will make use of a real-world case. Suppose you are a customer of a specific bank, and this bank has only one cashier. When you go to the bank, you must head to a queue and wait for your chance. Once in the queue, you notice that only one customer can make use of the cashier at a time, and it would be impossible for the cashier to attend two customers simultaneously without potentially making errors. Computing provides means to access data in a controlled way, and there are several techniques, such as mutex.
Mutex can be understood as a special process variable that indicates the level of availability to access data. That is, in our real-life example, the customer has a number, and at a specific moment, this number will be activated and the cashier will be available for this customer exclusively. At the end of the process, this customer will free the cashier for the next customer, and so on.
Understanding message passing
Message passing is used when we aim to avoid data access control and synchronizing problems originating from shared state. Message passing consists of a mechanism for message exchange in running processes. It is very commonly used whenever we are developing programs with distributed architecture, where the message exchanges within the network they are placed are necessary. Languages such as Erlang, for instance, use this model to implement communication in its parallel architecture. Once data is copied at each message exchange, it is impossible that problems occur in terms of concurrence of access. Although memory use seems to be higher than in shared memory state, there are advantages to the use of this model. They are as follows:
- Absence of data access concurrence
- Messages can be exchange locally (various processes) or in distributed environments
- This makes it less likely that scalability issues occur and enables interoperability of different systems
- In general, it is easy to maintain according to programmers
- 大話PLC(輕松動(dòng)漫版)
- CMDB分步構(gòu)建指南
- Web全棧工程師的自我修養(yǎng)
- Visual Basic程序設(shè)計(jì)與應(yīng)用實(shí)踐教程
- WordPress Plugin Development Cookbook(Second Edition)
- PhpStorm Cookbook
- Mastering JavaScript Design Patterns(Second Edition)
- Java程序設(shè)計(jì)入門
- C語(yǔ)言程序設(shè)計(jì)
- Swift 4 Protocol-Oriented Programming(Third Edition)
- Kotlin極簡(jiǎn)教程
- 愛上C語(yǔ)言:C KISS
- C#程序設(shè)計(jì)基礎(chǔ)入門教程
- C語(yǔ)言程序設(shè)計(jì)教程
- 優(yōu)化驅(qū)動(dòng)的設(shè)計(jì)方法