T
T
TheHorse2011-09-08 23:25:03
Nginx
TheHorse, 2011-09-08 23:25:03

nginx. Why processes and not threads?

When making a web server (win api), I used threads to ensure speed on multi-core systems.
Nginx uses processes, not threads. And why?
Actually, there are several assumptions, but Google did not help to understand which one is correct:
1. Bypass the limit of file descriptors per process.
2. Fault tolerance (1 process has fallen - others live).
3. Security (I'm not sure I understand how).
I will accept other guesses, but the real reasons for this decision are better, thanks.
Clarification
Threads instead of processes != one thread per connection.
You can create worker processes (nginx), you can create worker threads (mywebserver), you can create a thread/process (apache) for each connection.
Interested, specifically, why nginx filed worker processes , and not worker threads.
Answer
Igor Sysoev:
Initially, threads were planned, but were not used for a number of reasons:
1) more time-consuming programming,
2) poor support for threads in FreeBSD and Linux in 2002-2004,
3) processes provide natural isolation of resources, reducing
competition for them.

Answer the question

In order to leave comments, you need to log in

13 answer(s)
G
Georgy Khromchenko, 2011-09-10
@TheHorse

I think that this is because FreeBSD, which was used by Rambler until recently and where Sysoev worked a long time ago, when nginx started, could not balance threads across processors.

S
sdramare, 2011-09-09
@sdramare

Security (not sure I understand how)

I can assume that in the case of ducts we have a common address space, which, firstly, poses a potential threat of the influence of one thread on another, and secondly, 4GB of address space on a 32-bit machine is shared between all threads, while in the case of processes Each process has 4GB available. Accordingly, a solution with processes is more profitable.

A
andrew_tch, 2011-09-10
@andrew_tch

The simple answer is that nginx itself can work with both threads and processes; but, unfortunately, making modules, including third-party modules, work thread-safe is not realistic. Therefore, processes are safer and more predictable.

V
vanxant, 2011-09-10
@vanxant

And security is very easy.
There are 100,500 users on shared hosting, each with their own folder and their own site config.
If each nginx-a process is launched with the rights of its user, then the entire headache of allowing or denying some users to the directories of other users lies with the OS (and it copes with it brilliantly). If there were streams from a single www-data user, then nothing would prevent user Petya from making a symlink or just a link in his config to the user Vasya's folder and digging into his files through a web server.

T
TheHorse, 2011-09-11
@TheHorse

Igor Sysoev:
Initially, threads were planned, but were not used for a number of reasons:
1) more time-consuming programming,
2) poor support for threads in FreeBSD and Linux in 2002-2004,
3) processes provide natural isolation of resources, reducing
competition for them.

H
homm, 2011-09-08
@homm

Security is achieved by the fact that the head process is launched from the root, the workers from the user of the web server (www-data in debian-like ones).

N
nicolnx, 2011-09-09
@nicolnx

The aforementioned wiki link says:
- In servers built according to the “one thread per connection” model, scalability problems arise for the following reasons:
the overhead of the operating system data structures necessary to describe a single task is too high (task state segment, stack );
the context switch overhead is too high.
Isn't that the answer to your question?

N
nicolnx, 2011-09-09
@nicolnx

Then habrahabr.ru/blogs/hi/108294/ part 3 - it is well written there about mutual blocking of threads under load, even with an example.

A
Alexey Sundukov, 2011-09-09
@alekciy

IMHO, because worker processes are easier to implement and more time-consuming. But it would be more logical to ask Igor himself.

I
ixSci, 2011-09-09
@ixSci

Maybe it's really worth writing a question to the author? This will definitely clarify the situation.

A
AlexeyK, 2011-09-08
@AlexeyK

en.wikipedia.org/wiki/Event-driven_programming
has both "why" and cons with pluses

V
Vlad Zhivotnev, 2011-09-09
@inkvizitor68sl

They would write directly to the nginx mailing list.

S
shagguboy, 2011-09-11
@shagguboy

probably because

nginx/Windows works with Win32 API (not Cygwin emulation) It uses select as connection handling method, so don't expect high performance and scalability: it's still in beta.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question