Thursday, April 28, 2011

Parallelism

Threads and processes:
A computer will often appear to be doing many things simultaneously, such as checking for new e‐mail messages, saving a Word document, and loading a website. Each program is a separate “process”. Each process has one or more “threads”. If a process has several threads, they appear to run simultaneously. For example, an email client may have one thread that checks for new e‐mail messages and one thread for the GUI so that it can show a button being pressed. In fact, only one thread is being run at any given time. The processor switches between threads so quickly that they appear to be running simultaneously. Multiple threads in a single process have access to the same memory. By contrast, multiple processes have separate regions of memory and can only communicate by special mechanisms. The processor loads and saves a separate set of registers for each thread. Remember, each process has one or more threads, and the processor switches between threads.



Mutexes and semaphores:A mutex is like a lock. Mutexes are used in parallel programming to ensure that only one thread can access a shared resource at a time. For example, say one thread is modifying an array. When it has gotten halfway through the array, the processor switches to another thread. If we were not using mutexes, the thread might try to modify the array as well, which is probably not what we want. To prevent this, we could use a mutex. Conceptually, a mutex is an integer that starts at 1. Whenever a thread needs to alter the array, it “locks” the mutex. This causes the thread to wait until the number is positive and then decreases it by one. When the thread is done modifying the array, it “unlocks” the mutex, causing the number to increase by 1. If we are sure to lock the mutex before modifying the array and to unlock it when we are done, then we know that no two threads will modify the array at the same time.
Semaphores are more general than mutexes. They differ only in that a semaphore’s integer may start at a number greater than 1. The number at which a semaphore starts is the number of threads that may access the resource at once. Semaphores support “wait” and “signal” operations, which are analogous to the “lock” and ”unlock” operations of mutexes.


Deadlock:
Deadlock is a problem that sometimes arises in parallel programming. It is typified by the following, which is supposedly a law that came before the Kansas legislature:
“When two trains approach each other at a crossing, both shall come to a full stop and neither shall start up again until the other has gone.”
Strange as this sounds, a similar situation can occur when using mutexes. Say we have two threads running the following code:


Thread 1:
acquire(lock1);
acquire(lock2);
[do stuff]

release(lock1);
release(lock2);


Thread 2:
acquire(lock2);
acquire(lock1);
[do stuff]
release(lock2);
release(lock1);



Suppose that thread 1 is executed to just after the first statement. Then, the processor switches to thread 2 and executes both statements. Then, the processor switches back to thread 1 and executes the second statement. In this situation, thread 1 will be waiting for thread 2 to release lock1, and thread 2 will be waiting for thread 1 to release lock2. Both threads will be stuck indefinitely. This is called deadlock.

No comments:

Post a Comment