Saturday, April 3, 2010

Exercise 10 - Topic 8

1. Find definitions for wigth terms and concepts used in threaded programming:
i.   Thread Synchronisation
ii.  Locks
iii. Deadlock
iv.  Semaphores
v.   Mutex (mutual exclusion)
vi.  Thread
vii. Event
viii.Waitable timer.

Thread Synchronisation is the mechanism to ensure that two concurrently running threads do not execute specific sections of the program simultaneously.

Locks make sure that no two or more threads can access a particular resource at the same time.

Deadlock is the state no further progress on the program can be made.

A semaphore is a protected variable or abstract data type that constitutes a classic method of controlling access by several processes to a common resource in a parallel programming environment.

Mutual exclusion (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 thread is the unit of execution within a process.

An event (also called event semaphore) is a type of synchronization mechanism that is used to indicate to waiting processes when a particular condition has become true.

A waitable timer object is a synchronization object whose state is set to signaled when the specified due time arrives.

References:

http://en.wikipedia.org/wiki/Mutex
http://en.wikipedia.org/wiki/Semaphore_(programming)
http://en.wikipedia.org/wiki/Event_%28synchronization_primitive%29
http://msdn.microsoft.com/en-us/library/ms687012(VS.85).aspx


2. A simple demonstration of the threading module in Python (threddemo.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 loo. Report your findings.

The code is an example of how threads are run in Python. In the code, the numtasks variable is used to determine the max number of tasks. The variable sema is used to create a semaphore which determine how many threads can be run at a time.  The mutex variable is used to create a lock.
In the code, the threads are started and the semaphore is acquired. Once acquired, only 3 threads could run at a time. The first 3 threads ran and when each completed, a new thread started executing until all the threads completed to run.

No comments:

Post a Comment