Wednesday, May 19, 2010

Exercise 10

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

:

1. Thread Synchronisation

Thread Synchronisation is either to synchronize access to the shared resources, or to coordinate execution of threads. It is especially useful in avoiding conflicts when more than one thread requires the access of a single variable or other resources.


2. Locks

The Java programming language provides multiple mechanisms for communicating between threads. The most essential method is synchronization that is implemented using monitors. Every object in Java is joined with a monitor, which a thread can lock or unlock. Just a single thread at a time may hold a lock on a monitor. On the other hand, other threads attempt to lock that monitor so they are blocked until they can obtain a lock on that monitor. A thread t may lock a particular monitor multiple times; each unlock reverses the effect of one lock operation. (Sun, 2005)


3. Deadlock

Regarding application programming, as opposed to server implementation, thread pools pose some concurrency risks, because the tasks making up an application are inclined to be dependent on each other. Particularly, the deadlock is regarded as a significant concern. A deadlock appears when a set of threads creates a cycle of waiting and threads will be held up and cannot proceed.


4. Semaphores

Semaphores program the construct designed by E. W. Dijkstra in the late 1960s. Dijkstra’s model was the operation of railroads, considering a stretch of railroad in which there is a single track in which one train at a time is just allowed. Guarding this track is a semaphore. A train will keep waiting before entering the single track until the semaphore is in a state that permits transports. When the train enters the track, the semaphore changes state to protect other trains from entering the track. A train leaving this area of track should again change the state of the semaphore in order to enable another train to enter (Sun, 2009). In the computer version, a semaphore appears to be a simple integer. A thread waits for permission to proceed and then signals proceeded by performing a P operation on the semaphore.


5. Mutex (mutual exclusion)

Mutexis, as the abbreviation for “mutual exclusion, are one of the primary means for implementing thread synchronization and for protecting shared data when multiple writes happen. A mutex variable acts like “lock” protecting access to a shared data resource. The fundamental concept of a mutex as used in Pthreads is that a single thread can just either lock or own a mutex variable at any given time. Thus, even though several threads try to lock a mutex, merely one thread will be successful, and other thread will not own that mutex until the owning thread unlocks that mutex. Threads must take the turns accessing protected data (Barney, 2009)


6. Thread

A Thread is such an agent spawned by the application in order to perform work, independent of the parent process. While the term Thread and threading have referred to the concept of spawning (forking) multiple processes, more frequently they refer specifically to a pthread, or a worker which is spawned by the parent process, and it shares that parent’s resources. And Threads basically differ from processes in which they are “light weight” and “share memory”.


7. Event

In accordance to wikipedia, an event is an action that is usually initiated outside the scope of a program and that is handled by a piece of code inside the program. Normally, the events are handled synchronous with the program flow, that is, the program has one or more dedicated places where events are handled. Typical sources of events include the user (who presses a key on the keyboard, in other words, through a keystroke). Another source is a hardware device, such as a timer. A computer program that changes its behaviour, responding to events is said to be event-driven, often with the goal of being interactive.


8. Waitable timer

A waitable timer clarifies a synchronization object of which state is set to signalized when the specified due time arrives.

References

Barneym B., (2009). POSIX Threads Programming. Retrieved May 12, 2010 from https://computing.llnl.gov/tutorials/pthreads/

Sun Microsystems, (2005). Threads and Locks. Retrieved May 12, 2010 from http://java.sun.com/docs/books/jls/third_edition/html/memory.html

Sun Microsystems, (2009). Semaphores. Retrieved May 12, 2010 from http://docs.sun.com/app/docs/doc/805-5080/6j4q7emgq?a=view


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.


Process Explain:


The threading module has been created by running a set number of 10 threads concurrently.


numtasks = 10


A semaphore is used with the maximum number of threads set to 3. The ‘Bounded Semaphore’ is used to ensure that the value does not outweigh the initial set value of 3. On the other hand, if it does, the bounded semaphore should not allow this to happen without it being detected in some way.


sema = threading.BoundedSemaphore (value=3)


A lock is set, in this case a reentrant lock – ‘RLock’. This is used with the acquire () method for the lock and the the release() method to unlock the lock, so that other threads can run simultaneously up to the maximum of 3.


mutex = threading.RLock()


When the program runs, the first three threads encountered begin processing. The remaining 7 threads are all recognised and locked, waiting until one of the processing threads has completed its tasks. The output shows that the threads do not have run and finish in the order they started before the next thread can start. Thread 2 had a shorter process time than Thread 1, so it was able to be released first, and then the next thread, in this case Thread 4, was started.


Reference

Wikipedia.(2010). Event. Retrieved May 12, 2010 from http://en.wikipedia.org/wiki/Event_(computing)

No comments:

Post a Comment