Friday, May 28, 2010

exercise 10

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

Thread Synchronisation


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

Locks

The Java programming language provides multiple mechanisms for communicating between threads. The most basic of these methods is synchronization, which is implemented using monitors. Each object in Java is associated with a monitor, which a thread can lock or unlock. Only one thread at a time may hold a lock on a monitor. Any other threads attempting to lock that monitor 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)

Deadlock

For application programming, as opposed to server implementation, thread pools pose some concurrency risks. The reason is that the tasks making up an application tend to be dependent on each other. In particular, deadlock is a significant concern. A deadlock occurs when a set of threads creates a cycle of waiting and in such case; threads will be hold up and cannot proceed.

Semaphores

According to (Sun, 2009), Semaphores are a programming construct designed by E. W. Dijkstra in the late 1960s. Dijkstra’s model was the operation of railroads: consider a stretch of railroad in which there is a single track over which only one train at a time is allowed. Guarding this track is a semaphore. A train must wait before entering the single track until the semaphore is in a state that permits travel. When the train enters the track, the semaphore changes state to prevent other trains from entering the track. A train that is leaving this section of track must again change the state of the semaphore to allow another train to enter.

In the computer version, a semaphore appears to be a simple integer. A thread waits for permission to proceed and then signals that it has proceeded by performing a P operation on the semaphore.

Mutex (mutual exclusion)


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

Thread


A Thread is simply an agent spawned by the application 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 which shares that parent’s resources. And Threads fundamentally differ from processes in that they are “light weight” and “share memory”.

Event


According 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. Typically 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 devices such as a timer. A computer program that changes its behavior in response to events is said to be event-driven, often with the goal of being interactive.

Waitable timer


A waitable timer objectis a synchronization object whose state is set to signaled when the specified due time arrives (MSDN, 2009).

References:

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

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

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

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

MSDN, (2009). Waitable Timer Objects. Retrieved on May 21st, 2010 from, http://msdn.microsoft.com/en-us/library/ms687012(VS.85).aspx

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

The Python code by Ted Herman is simple and a very good illustration on lock and semaphore.

Coding on lock:


mutex=threading.RLock()
mutex.acquire()
mutex.release()

Coding on semaphore:


sema=threading.BoundedSemaphore(value=3)
sema.acquire()
sema.release()

Right after the lock statement mutex.acquire() , the flag indicator running is either increment or decrement, then a release lock must be followed: mutex.release(). But in the semaphore case, there could be several lock threads in between the acquire and release statements.

The semaphore here acts as an counting semaphore which is a counter for a set of available resources, rather than a locked/unlocked flag of a single resource.

No comments:

Post a Comment