~ubuntu-branches/debian/jessie/python-posix-ipc/jessie

« back to all changes in this revision

Viewing changes to demo/ReadMe.txt

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2014-04-05 23:52:32 UTC
  • Revision ID: package-import@ubuntu.com-20140405235232-8exuqr3hqajg0x3k
Tags: upstream-0.9.8
ImportĀ upstreamĀ versionĀ 0.9.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
This demonstrates use of shared memory and semaphores via two applications 
 
2
named after Mrs. Premise and Mrs. Conclusion of the Monty Python sketch. 
 
3
http://www.youtube.com/watch?v=crIJvcWkVcs
 
4
 
 
5
Like those two characters, these programs chat back and forth and the result 
 
6
is a lot of nonsense. In this case, what the programs are saying isn't the
 
7
interesting part. What's interesting is how they're doing it.
 
8
 
 
9
Mrs. Premise and Mrs. Conclusion (the programs, not the sketch characters)
 
10
communicate through POSIX shared memory with a semaphore to control access
 
11
to that memory.
 
12
 
 
13
Mrs. Premise starts things off by creating the shared memory and semaphore
 
14
and writing a random string (the current time) to the memory. She then sits
 
15
in a loop reading the memory. If it holds the same message she wrote, she
 
16
does nothing. If it is a new message, it must be from Mrs. Conclusion.
 
17
 
 
18
Meanwhile, Mrs. Conclusion is doing exactly the same thing, except that she
 
19
assumes Mrs. Premise will write the first message.
 
20
 
 
21
When either of these programs reads a new message, they write back an md5 
 
22
hash of that message. This serves two purposes. First, it ensures that
 
23
subsequent messages are very different so that if a message somehow gets
 
24
corrupted (say by being partially overwritten by the next message), it will
 
25
not escape notice. Second, it ensures that corruption can be detected if
 
26
it happens, because Mrs. Premise and Mrs. Conclusion can calculate what the
 
27
other's response to their message should be.
 
28
 
 
29
Since they use a semaphore to control access to the shared memory, Mrs. 
 
30
Premise and Mrs. Conclusion won't ever find their messages corrupted no
 
31
matter how many messages they exchange. You can experiment with this by
 
32
setting ITERATIONS in params.txt to a very large value. You can change 
 
33
LIVE_DANGEROUSLY (also in params.txt) to a non-zero value to tell Mrs. 
 
34
Premise and Mrs. Conclusion to run without using the semaphore. The shared
 
35
memory will probably get corrupted in fewer than 1000 iterations.
 
36
 
 
37
To run the demo, start Mrs. Premise first in one window and then run
 
38
Mrs. Conclusion in another. 
 
39
 
 
40
 
 
41
   The Fancy Version 
 
42
   =================
 
43
 
 
44
If you want to get fancy, you can play with C versions of Mrs. Premise and 
 
45
Mrs. Conclusion. The script make_all.sh will compile them for you. (Linux
 
46
users will need to edit the script and uncomment the line for the 
 
47
Linux-specific linker option.) 
 
48
 
 
49
The resulting executables are called premise and conclusion and work exactly 
 
50
the same as their Python counterparts. You can have the two C programs talk 
 
51
to one another, or you can have premise.py talk to the C version of 
 
52
conclusion...the possibilities are endless. (Actually, there are only four 
 
53
possible combinations but "endless" sounds better.)
 
54