Friday, May 28, 2010

Exercise 10 : Concurrency and Threading demonstration in Phyton

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

1. Thread Synchronisation
Mehta (2002) explained that Thread Synchronization refers to the act of shielding against multithreading issues such as data, races, deadlocks and starvation, in which the synchronization event classes allow one thread to notify the other threads of some event.

Daelin (n.d.) pointed out that thread synchronization requires a running thread gaining a “lock” on an object before it can access it. The thread need to work with another thread that is using the method/data member. Thread synchronization helps prevent or eliminate problem being happened when two threads try to change a variable or execute the same method at the same time.

Hence, each thread has its own local thread stack and registers. To avoid any object and data corruption when multiple threads try to read and acces the same data, thread synchronization with two stages apply. They are signaled and non-signaled. The signaled state allows objects to access and modify data, while the non-signaled state allows access and modify the data in thread local stack.


2. Locks
Oracle explained that Thread locks are a kind of spin that uses to lock a thread for purpose of changing thread state.

Locks are used for enforcement of concurrency control policies, while a lock is a synchronization mechanism for enforcing limits on access to a resource in an environment where existence of many threads of execution.

Locks require hardware support for implementation in some cases. For example, atomic instructions allow a single process to perform if the lock is free, then, acquire the lock in a single atomic operation if free. (Wikipedia, 2010a)


3. Deadlock
A deadlock is happened where two or more threads or actions are waiting each other to finish and release the resource, however, none of them does, no thread or action is able to execute, which is resulting in timeout eventually. This deadlock arises in a system with multiple active asynchronous processes. (Wikipedia, 2010b and Deadlock, n.d.)


4. Semaphores
Semaphore is a protected variable or abstract data type that constitutes a classic method of controlling access by several processes to a common or shared resource in a multiprogramming environment. It can be classified as binary and counting. A binary semaphore is a simple locked or unlocked flag that controls access to a single resource, while a counting semaphore is a counter for a set of variable resources. Both semaphore types can be used to prevent a race condition, but do not have value in preventing resource deadlock. (Wikipedia, 2010c)


5. Mutex (mutual exclusion)
Mutex known as Mutual exclusion, which is a program object that allows multiple program threads to share the same resource including file access, and avoid simultaneous use of a common resource. A mutex is created with a unique name when a program is started. Thread that needs the resource must lock the mutex from other threads while it is using the resource. The mutex is set to unlock when the data is no longer in use or the task is finished. (Webopedia, 2001)


6. Thread
A thread can be referred to executing concurrent operation. A thread of execution results from a fork of a computer program into two or more concurrent running tasks. The implementation of threads and processes differs from one operating system to another, but it is contained inside a process in most cases. Multiple threads can be existed within the same process and share resources like memory, while different processes do not share these resources. (Wikipedia, 2010d)


7. Event
Event-driven programming is a programming paradigm where the flow of program is determined by events such as sensor outputs or user actions, or instructions from other programs or threads. (Wikipedia, 2010e)

So (n.d.) illustrated that simple programs have clearly defined main flow that is sequential from beginning to the end within event-driven programming. A stream of events coming into a program, a dispatcher determines the handler to response for the even, while the event is sent to the handler for proper handling.


8. Waitable timer
Microsoft (2010) documented that a waitable timer object is a sunchronization object which state is defined to signaled when the specified due time arrives. Two types of waitable timers available, which can be created manual-reset and synchronization. Both can be defined as periodic timer basis


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 lowa. The code and sample output below are worth a look. Report your findings.

The program tries to control the maximum number of tasks to be run at a time interval. The maximum number of tasks being run is 3, while the others are locked and queued until the first one being completed.

According to the results, the first 3 tasks run in sequence and in parallel, while thread 4 to 9 are pending. When thread 0 completed, message displayed to show the finished status, and current tasks in progress reduced to 2, follow immediate message to display the concurrent tasks being increased to 3 again. These status were repeated once a thread is finished and another thread is append. The program ended until all thread is completed.


Reference:-

Deadlock. (n.d.). Retrieved 26 May 2010 from http://personal.cityu.edu.hk/~dcykcho/book/Chapter9.htm.

Daelin. (n.d.), What is Thread Synchronization?. In WikiAnswers. Retrieved 26 May 2010 from http://wiki.answers.com/Q/What_is_Thread_Synchronization.

Mehta, Manisha. (2002). Multithreading Part 3: Thread Synchronization. Retrieved 26
May 2010 from http://www.c-sharpcorner.com/UploadFile/mmehta/Multithreading311162005045743AM/Multithreading3.aspx.

Microsoft. (2010). Waitable Timer Objects. In Microsoft.com. Retrieved 26 May 2010 from http://msdn.microsoft.com/en-us/library/ms687012(VS.85).aspx.

Oracle. (n.d.). Thread Locks. In Oracle. Retrieved 26 May 2010 from http://docs.sun.com/app/docs/doc/817-6223/6mlkidlmf?a=view

So, H. (n.d.). Event-Drive Programming – Designing Graphical User Interface. Retrieved 26 May 2010 from http://www.eee.hku.hk/~hso/engg1002eee/handouts/4-gui.pdf.

Wikipedia. (2010a). Lock (computer science). In Wikipedia, the free encyclopedia. Retrieved 20 May 2010 from http://en.wikipedia.org/wiki/Lock_(computer_science).

Wikipedia. (2010b). Deadlock. In Wikipedia, the free encyclopedia. Rertieved 20 May 2010 from http://en.wikipedia.org/wiki/Deadlock.

Wikipedia. (2010c). Semaphore (programming). In Wikipedia, the free encyclopedia. Retrieved 20 May 2010 from http://en.wikipedia.org/wiki/Semaphore_(programming).

Wikipedia. (2010d). Thread (computer science). In Wikipedia, the free encyclopedia. Retrieved 20 May 2010 from http://en.wikipedia.org/wiki/Thread_(computer_science).

Wikipedia. (2010e). Event-driven programming. In Wikipedia, the free encyclopedia. Retrieved 20 May 2010 from http://en.wikipedia.org/wiki/Event-driven_programming.

Webopedia. (2001). Mutex. In Webopedia.com. Retrieved 20 May 2010 from http://www.webopedia.com/TERM/M/mutex.html.

1 comments:

Faiza Jee said...

I am impressed. I don't think Ive met anyone who knows as much about this subject as you do. You are truly well informed and very intelligent. You wrote something that people could understand and made the subject intriguing for everyone. Really, great blog you have got here. Eyelash Extension in Melbourne

Post a Comment