::-- ZoomQuiet [2007-01-30 06:25:45]
1.1. Concurrency is easy
We understand concurrency
A deep understanding of concurrency is hard-wired into our brains. We react to stimulation extremely quickly, in a part of the brain called the amygdala, without this reaction system we would die. Conscious thought is just too slow, by the time the thought "hit the brakes" has formed itself, we have already done it.
On a motorway, I mentally track the positions of dozens, or hundreds of cars, this is done without conscious thought. If I couldn't do this I would probably be dead.
The world is parallel
If we wish to write programs that behave as other objects behave in the real world then these programs will have a concurrent structure.
This is why we should program in a concurrent programming language
当然我们在现实世界中最常见的是使用序列语言进行编程,想解放思想很难. ErLang 是被设计为天然进行并发处理的语言,使用它来编写并发程序非常简单.
And yet most often we program real-world applications in sequential programming languages. This is uncessarily difficult.
By programming in a language what was designed for programming concurrent applications (like Erlang) concurrent programming becomes a lot easier.
1.1.3. Erlang 程序模仿我们的思想和交互
Erlang programs model how we think and interact
我们没有共享记忆,各有各的,我们共有两个大脑,它们并没有合并在一起. 为了改变你的记忆,我得发出消息,说话或是挥动手臂. 你听到,看到,你的记忆就变更了,但是不问一下你或是不观察你的反应,我没有办法确认你接受了我的消息.
这就是Erlang 的处理过程, ErLang的进程没有分享内存,每个进程各用自个儿的. 改变另外进程,你必须给其发送渻,并期望它可以接受并理解.
We don't have shared memory. I have my memory, you have yours, we have two brains, one each, they are not joined together. To change your memory I send you a message, I talk or wave my arms. You listen, you see, your memory changes, but without asking you a question or observing your response I do not know that you have received my messages.
This is how it is with Erlang processes. Erlang processes have no shared memory. Each processes has its own memory. To change the memory of some other process you must send it a message and hope that it receives and understands the message.
To confirm that another process has received your message and changed its memory, you must ask it (by sending it a message). This is exactly how we interact.
李四:"嘿,张三我电话是 12345678 "
Sue: "Hi Bill, my telephone number is 45 67 89 12"
Sue: "Did you hear me?"
Bill: "sure, your number is 45 67 89 12"
These interaction patterns well-know to us, from birth onwards we learn to interact with the world by observing it and by sending it messages and observing the responses.
People function as independent entities that communicate by sending messages
ErLang 程序生成N多小进程自由运行 -- 就象人群.
每个ErLang 可以制造几十, 可能上千的小进程 -- 所有进程的运行是独立的. 它们相互沟通就是使用消息的发送. 每个进程都有私有的内存. 如同一个大空间中的人群相互游离.
That's how Erlang processes work, and that's how we work so it's very easy to understand an Erlang program.
Erlang programs are made up of lots of little processes all chattering away to each other - just like people.
An Erlang program is made up of dozens, possible thousands or even hundreds of thousands of small processes - all these processes operate independently. They communicate with each other by sending messages. Each process has a private memory. They behave like a huge room of people all chattering away to each other.
这使得ErLang 程序天生的容易管理及衡量. 假定我们有10人(进程)而且都很忙,我们如果处理的? 如何管理人群,最简单的这是发出指令给所有人(广播).
ErLang 程序没有分享内存,所以不必进行锁操作--进行协同时, 否则: 锁在哪?钥匙在哪?如果哪天钥匙丢了会发生什么?... 你就得恐慌了.
This makes Erlang program inherently easy to manage and scale. Suppose we have ten people (processes) and they have too much work to do, what can we do? get more people. How can we manage these groups of people, easy just shout instructions at them (broadcasting).
Erlang processes don't have shared memory, so there is no need to lock the memory while it is being used. Where there are locks, there are keys, and when there are keys the keys will one day get lost and what happens when you loose a key? - you panic.
Distributed software systems with locks and keys always go wrong.
If somebody dies other people will notice
如果要在房间里突然昏倒死了,应该有人将注意到,至少最终应该 -- 我希望. ErLang 进程也正如人群,它们有时也会死. 不象人,他们会精确的在死亡之时咽下最后一口气.
ErLang 进程也正是这么作的,一个进程可以这么说:"我要求除以0,我要完了!" 另一个可能说:"我要完了,因为我向一空列表请求最后的元素!"
I I'm in a room and suddenly keel over and die, somebody will probably notice, well at least I hope so. Erlang processes are just like people, they can on occasions die. Unlike people when they die they shout out in their last breath exactly what they have died from.
Imagine a room full of people, suddenly one person will keel over and die and just as they die, they say "I'm dying of a heart attack" or "I'm dying of an exploded gastric wobbledgog". That's what Erlang processes do. One process might die saying.
"I'm dying because I was asked to divide by zero", another might say
"I'm dying because because I was asked what the last element in an empty list was"
现在我们可以想象那塞满了人的房间里,必然有专门的人来清理尸体. 让我们想象两个人 甲同乙, 如果甲死亡后由乙来处理关联的问题. 乙死亡后由甲来处理关联的问题, 甲和乙就象是被看不见的合约绑定在了一起 -- 不论谁死,都得由对方善后(好哥们儿哪;-)
Now in our room full of people we might imagine there are specially assigned people whose job it is to clear away the bodies. Let's imagine two people Jane and John. If Jane dies then John will fix any problems associated with Jane's death. If Jane dies then John will fix the problems. Jane and John are linked together with an invisible agreement which says that if one of them dies the other will fix up any problems caused by the death.
That's is how error detection in Erlang works, processes can be linked together. If one of the processes dies, the other process gets an error message saying why the first process dies.
That's basically it.
That's how Erlang programs work.
What we've learnt so far
ErLang 程序生成大量进程,进程能相互发送消息. 消息可能被接受并理解,也可能不.
Erlang programs are made of lots of processes. These processes can send messages to each other.
These message may or may not be received and understood. If you want to know if a message was received and understood you must send the process a message and wait for a reply.
Pairs of processes can be linked together. If one processes in a linked pair dies the other process in the pair will be sent a message containing the reason why the first process died.
这便是 面向并发程序 的简单模式说明. 进一步的可以阅读这里
This simple model of programming is part of a model I call Concurrency Oriented Programming. You can read more about this here.
:),0 HuangYi 2007-01-31 15:20:14 The world is parallel 的意思应该是： 世界是并行的 吧。
:\,0 ZoomQuiet 2007-02-01 00:11:45 good! fixed it!