php and apache

Mario Hoerich lists at MHoerich.de
Mon Jan 24 19:21:02 PST 2005


# Gert Cuykens:
> can somebody explain what the difference is between forks and
> threads

Nutshell version: fork(2) produces a new process, which may consist
of multiple threads.

fork(2)ing used to be slightly more expensive, as it creates a new
process with an accompanying process control block (PCB) and
allocates its own memory pages. Threads just use their processes'
data segment and thus share pages. Which basically means one thread
can trash another's data, whereas related processes can not.

OTOH the time slices handed out by the process scheduler 
("Hey! PID 384! Your turn for the next 20ms!") are further subdivided
by the thread scheduler. Since both thread scheduler and context
switches between threads produce some overhead (storing local data,
instruction pointer and such) threads used to reduce the real CPU 
time a process could actually use for its algorithms.

I said "used to", because this is basically the theory introductory
textbooks on OS design will tell you.[1] There's plenty of ways to
adapt costs, i.e. by making the process scheduler hand out larger
slices to multithreaded processes or employing copy-on-write, which
means that the parent processes' pages are just mapped into the child 
process, until the child actually writes to them. Traditionally,
Unices had pretty cheap processes but rather expensive threads.
(Windows, for example, had it the other way around).

I didn't delve into this for quite a while, so sadly, I can't give
you any details on the current state of things. 

HTH,
Mario

[1]:
Be warned, this is from the top of my head and it's 4am in the
morning with my bed waiting for me. I just hope I've been at
least *somewhat* coherent... ;)


More information about the freebsd-questions mailing list