Tuesday, May 25, 2010

Exercise 10: Concurrency and Threading demonstration in Python

1. Find definitions for eight terms and concepts used in threaded programming:

a.Thread Synchronisation

Thread synchronization requires that a running thread gain a "lock" on an object before it can access it. The thread will wait in line for another thread that is using the method/data member to be done with it. This is very important to prevent the corruption of program data if multiple threads will be accessing the same data. If two threads try to change a variable or execute the same method at the same, this can cause serious and difficult to find problems. Thread synchronization helps prevent this ("What is Thread Synchronisation?", n.d.)

b.Locks

Lock is a fundamental synchronization mechanism for enforcing limits on access to a resource in a shared environment where there are many threads of execution. Locks are one way of enforcing concurrency control policies (Lundh, 2007).

c.DeadLock

Dictionary Wikipedia (n.d.) defines: "deadlock refers to a specific condition when two or more processes are each waiting for each other to release a resource, or more than two processes are waiting for resources in a circular chain" ("DeadLock", 2010).

d.Semaphores

A semaphore is a data structure that is useful for solving a variety of synchronization problems. It is typical used to limit accesses to a resource with limited capacity. A semaphore has an internal counter rather than a lock flag, and it only blocks if more than a given number of threads have attempted to hold the semaphore. The counter is incremented when the semaphore is acquired and decremented when the semaphore is released. If the counter equals zero when the semaphore is acquired, the acquiring thread will be blocked(Downey, 2008).

e.Mutex (mutual exclusion)

According to Wikipedia (2010), Mutual Exclusion (often abbreviated to mutex) algorithms are used in concurrent programming to avoid the simultaneous use of a common resource, such as a global variable, by pieces of computer code called critical sections. A critical section is a piece of code in which a process or thread accesses a common resource. The critical section by itself is not a mechanism or algorithm for mutual exclusion. A program, process, or thread can have the critical section in it without any mechanism or algorithm which implements mutual exclusion ("Mutual Exclusion", 2010).

f.Thread

Ince(2004) defined “A thread is an execution of a chunk of code which can be carried out in parallel with the execution of other chunks of code”.

g.Event

An event, in a computing context, is any identifiable occurrence that has significance for system hardware or software. User-generated events include keystrokes and mouse clicks, among a wide variety of other possibilities. System-generated events include program loading and errors, also among a wide variety of other possibilities. An event typically represents some message, token, count, pattern, value, or marker that can be recognized within an ongoing stream of monitored inputs, such as network traffic, specific error conditions or signals, thresholds crossed, counts accumulated, and so on. ("What Is an Event", 2007)

h.Waitable timer.

According to msdn library(n.d.), a waitable timer object is a synchronization object whose state is set to signaled when the specified due time arrives. There are two types of waitable timers that can be created: manual-reset and synchronization. A timer of either type can also be a periodic timer.


2.A simple demonstration of the threading module in Python (threaddemo.py) that uses both a lock and semaphore to control concurrency is by Ted Herman at the University of Iowa. The code and sample output below are worth a look. Report your findings.

The program initializes 10 threads in which each threat is give a random delay time, but only allows three of them to be running at a time. Only when one of these three jobs is completed, one of the waiting threads is allowed to start. The program is ended when all the 10 threads are finished.

In order to control the limitation of running threads to be 3, the program employ a function called "BoundedSemaphore([Value])" of the "local" class. This is a factory function that returns a new bounded semaphore object. A bounded semaphore checks to make sure its current value doesn't exceed its initial value. If it does, ValueError is raised. In most situations semaphores are used to guard resources with limited capacity. If the semaphore is released too many times it's a sign of a bug. If not given, value defaults to 1. ("Python Library Reference", 2008).

# create a semaphore bounded up to 3
sema = threading.BoundedSemaphore(value=3)


Besides the program uses funtion Rlock() to enable only one of the three threads update variable "running" at a time using "acquire() and release()" mechanism. RLock( ) is a factory function that returns a new reentrant lock object. A reentrant lock must be released by the thread that acquired it. Once a thread has acquired a reentrant lock, the same thread may acquire it again without blocking; the thread must release it once for each time it has acquired it. ("Python Library Reference", 2009)

# create a Read Lock
mutex = threading.RLock()

mutex.acquire()
running = running + 1
mutex.release()


Refereces

Downey, A. B.(2008).The Little Book of Semaphores(2nd E.d.). Retrieved 13 May 2010 from http://www.greenteapress.com/semaphores/downey08semaphores.pdf

"DeadLock".(2010). Viewed 17 May 2010 from http://en.wikipedia.org/wiki/Deadlock

Lock(computer science).(n.d.)viewed 13 May 2010 from http://en.wikipedia.org/wiki/Lock_%28computer_science%29

Lundh, F. 2007.Thread Synchronization Mechanisms in Python.Viewed 13 May 2010 from http://effbot.org/zone/thread-synchronization.htm

"Mutual Exclusion".(2010). Viewed 17 May 2010 from http://en.wikipedia.org/wiki/Mutual_exclusion

msdn Library(n.d.).Waitable Timer Object.viewed 13 May 2010 from http://msdn.microsoft.com/en-us/library/ms687012(VS.85).aspx

"Python Library Reference".(2008).Viewed 05 May 2010 from http://www.python.org/doc/2.5.2/lib/module-threading.html

"What Is an Event".(2007).Viewed 12 May 2010 from http://searchsoa.techtarget.com/sDefinition/0,,sid26_gci1274431,00.html

"What is Thread Synchronisation?". (n.d.).Viewed 10 May 2010 from http://wiki.answers.com/Q/What_is_Thread_Synchronization

No comments:

Post a Comment