Thread & Process
Difference Between Thread and Process
A process is an instance of a program in execution. A process is an independent entity to which system resources (e.g., CPU time and memory) are allocated.
A thread exists within a process and shares the process' resources. Multiple threads within the same process will share the same heap space.
Both processes and threads are independent sequences of execution. The typical difference is that threads (of the same process) run in a shared memory space, while processes run in separate memory spaces.
Lock & Sync
Lock
In computer science, a lock is a synchronization mechanism for enforcing limits on access to a resource in an environment where there are manythreads of execution. A lock is designed to enforce a mutual exclusion concurrency control policy.
Race condition
When several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place, it's called race condition.
Critical Section
Each process has a segment of code, called critical section, in which the process may be changing common variables, updating a table, writing a file, and so on.
The code between lock and unlock calls to a mutex.
Spinlock
Spinlock is a lock which causes a thread trying to acquire it to simply wait in a loop ("spin") while repeatedly checking if the lock is available. Since the thread remains active but is not performing a useful task, the use of such a lock is a kind of busy waiting. Once acquired, spinlocks will usually be held until they are explicitly released.
Spinlock vs. Mutex
Semaphore
A semaphore S is an integer variable that, apart from initialization, is accessed only through two standard atomic operations: wait() and signal().
counting semaphore: range over an unrestricted domain
binary semaphore: range only between 0 and 1
On some systems, binary semaphore are known as mutex locks, as they are locks that provide mutual exclusion.
Busy Waiting
In software engineering, busy-waiting or spinning is a technique in which a process repeatedly checks to see if a condition is true, such as whether keyboard input or a lock is available.
Deadlock
Two (or more) threads have stopped execution or are spinning permanently. For example, a simple deadlock situation: thread 1 locks lock A, thread 2 locks lock B, thread 1 wants lock B and thread 2 wants lock A.
Deadlock Requirement
1. Mutual exclusion. At least one resource must be held in a nonsharable mode; that is, only one process at a time can use the resource.
2. Hold and wait. A process must be holding at least one resource and waiting to acquire additional resources that are currently being held by other processes.
3. No preemption. Resources cannot be preempted; that is, a resource can be released only voluntarily by the process holding it, after that process has completed task.
4. Circular wait. A set{P0, P1...Pn} of waiting processes must exist such that P0 is waiting for resource hold by P1, P1 is waiting for...
Dining Philosopher
http://blog.csdn.net/b_end_an/article/details/11910651