Process based code vs. Thread based code

I am just wondering on which is the best way to write programs on UNIX. Which one is better from below:

a) Spawning threads per client connection/request?
b) fork-exec new processes per client connection/request?

Assume that I am doing some database system on linux which is supposed to run on unix systems as well.

The basic differences as I know are:

Using processes - have it's own address space, communicate using SysV/POSIX shared memory + semaphores etc & context switching is costly. Also implementing processes instead of threads is that process are more stable and reliable than threads. Just because if one thread malfunctions, the entire process would break.

On the other side, using thread the context switching is less cheaper compared to that of process context switches. No need for shared memory as the memory is shared and accessible by all threads with in the process & hence can avoid costly IPC mechanism's like SysV/POSIX shared memory and semaphores. Instead use simple mutexes and condition variables.

Being said all the above, I am still not in a position to decide up on which way to go i.e. use threads or use processes?

Please help.

Regards
Kalyan

Hi ,
Mostly the diffrence is in execution time.threads require less time to execute,whereas the execution speed is more with processes.
you can get the user space/system space time using getrusage() function call.
By comparing the execution time you can decide between threads & processes.
But one more thing with threads you have to deal with synchronization.

best luck.
Raj

Hi ,
Mostly the diffrence is in execution time.threads require less time to execute,whereas the execution speed is more with processes.
you can get the user space/system space time using getrusage() function call.
By comparing the execution time you can decide between threads & processes.
But one more thing with threads you have to deal with synchronization.

best luck.
Raj

If you are handling clients on a system that must scale you will use neither. Using non-blocking I/O and a fast event mechanism like epoll or kqueue is a better answer.