One difference: Threads within a process share a single address space, and can, with care (synchronization), access one another's variables. Processes run in different address spaces, and must use inter-process communications mechanisms to exchange information.
Processes Versus Threads
Other Terms:
Heavyweight Process = Process
Lightweight Process = Thread
Advantages (Thread vs. Process):
Much quicker to create a thread than a process.
Much quicker to switch between threads than to switch between processes.
Threads share data easily
Disadvantages (Thread vs. Process):
No security between threads: One thread can stomp on another thread's data.
For threads which are supported by user thread package instead of the kernel:
If one thread blocks, all threads in task block
Thread States (Example Java):
New = New: Placed on the ready list.
Blocked = Wait for an event
Runnable = Ready or Running
Dead = Exit
Thread functions: POSIX (Standard UNIX C/C++):
pthread_create(): creates a thread
pthread_exit(): kills itself
pthread_kill(): sends a signal to a specified thread
pthread_join(): waits for a thread to exit.
pthread_self(): returns thread id
synchronization functions
Java Implementation
http://objectlayer.blogspot.ca/2012/06/java-threads-for-hello-world.html
Define a class to contain a thread, by defining:
class Worker1 extends Thread OR
class Worker2 implements Runnable (Recommended)
The parent thread creates the object and invokes its start() function.
The start function creates the child thread and calls the object’s run() function.
Thread terminates when thread returns from run(), or stop() is called.
Types of Thread Implementations
User Thread Package
A package outside the OS creates and schedules threads.
Model: Many-to-one: Many user threads associated with one process.
Thread creation/management is faster than Kernel Thread support
However if one thread blocks, then entire process blocks. Thus all threads block.
Kernel Thread Support
The OS creates and schedules threads.
Model: One-to-one: Each user thread is associated with one kernel thread.
If one thread blocks, the kernel can schedule other threads.
If multiprocessor configuration, kernel can allocate threads of same process on different processors.
Example: Windows NT, Solaris, Digital UNIX
Many-to-Many Model: Combination of above
N user threads share M kernel threads (where N > M)
There may be a flexible and changing assignment of user threads to kernel threads.
Bound: User-thread is assigned permanently to kernel thread
Unbound: User threads share available kernel threads
Example: Solaris
----------------------deadlock vs. race condition
DeadlocksA deadlock occurs when one or more threads are permanently blocked from executing because each thread waits on a resource held by another thread in the deadlock. A thread can also deadlock on itself. The simplest case of deadlock occurs when process p1 gains access to data structure a and process p2 gains access to b, but p1 then waits for b and p2 waits for a.
A deadlock
occurs when one or more threads are stuck waiting for something that never will
occur.(advanced linux program p.82)
Race Conditions
A race condition occurs when two or more threads perform an operation, and the result of the operation depends on unpredictable timing factors; specifically, when each thread executes and waits and when each thread completes the operation.
The result is trouble. Because both processes see the same scull device, each will
store its new memory in the same place in the quantum set. If A stores its pointer
first, B will overwrite that pointer when it does its store. Thus the memory allocated by A, and the data written therein, will be lost.