Hi All,
Can you any one explain me about what is difference between fork and thread ..?
Thanks
Hi All,
Can you any one explain me about what is difference between fork and thread ..?
Thanks
A fork gives you a brand new process, which is a copy of the current process, with the same code segments. As the memory image changes (typically this is due to different behaviour of the two processes) you get a separation of the memory images, however the executable code remains the same. Tasks do not share memory unless they use some Inter Process Communication (IPC) primitive.
In contrast a thread is another execution thread of the same task. One task can have multiple threads, and the task memory object are shared among threads, therefore shared data must be accessed through some primitive and synchronization objects that allow you to avoid data corruption.
Dacav's answer is excellent, I just wanted to add that not all threading models give you true multi-processing.
For example, Ruby's default threading implementation doesn't use true OS / kernel threads. Instead it mimics having multiple threads by switching between the Thread objects within a single kernel thread / process.
This is important on multiprocessor / multi-core systems, because these types of lightweight threads can only run on a single core - you don't get much in the way of performance boost from having multiple threads.
The other place this makes a difference is when one thread blocks (waiting on I/O or calling a driver's IOCTL), all Threads block.
This isn't very common nowadays - most threading implementations use kernel threads which don't suffer from these issues - but its worth mentioining for completeness.
By contrast, fork gives you another process which is runnable simultaneously on another physical CPU while the original process is executing. Some people find IPC more suitable for their app, others prefer threading.
Good luck and have fun! Multi-threading is both challenging and rewarding.