2

I've been trying to implement a web server gateway (for fun and educational purposes) and I have some questions about the core architecture behind FastCGI/SCGI with respect to the pre-fork model.

How do FastCGI/SCGI implementations handle communication in pre-fork scenarios? AFAIK, the gateway only has one socket to connect to the FastCGI server. Normally, there is a parent process that accepts connections from the gateway and hands off the work to one of the pre-forked workers.

Since the connections are established after the children are forked, how are you supposed to have the children use these sockets to communicate with the gateway?

André Caron
  • 44,541
  • 12
  • 67
  • 125

2 Answers2

1

I hope I understood the question.

The server socket should be created by the parent process; when it forks, children inherit that socket making it a shared resource. Then, I suppose, each child tries to accept() connections concurrently.

As a reference I found this document (see "accept serialization") discussing starvation issue when listening on multiple sockets, and this SO discussion on sharing sockets

Community
  • 1
  • 1
ftartaggia
  • 541
  • 2
  • 11
  • Nice find. The SO discussion is definitely worth a read. I also like that it works on both UNIX and Windows systems. For a solution that facilitates load balancing, check out [Nikolai's answer](http://stackoverflow.com/questions/6797222/fastcgi-scgi-pre-fork/7068718#7068718). – André Caron Aug 15 '11 at 18:28
1

One options is file descriptor passing over UNIX domain socket. Stevens UNP has basic example.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • This seems simple enough, and allows the parent process to perform load balancing more easily. It's a UNIX specific, but this type of design is sort of UNIX specific in the first place. Any idea how portable between UNIX systems this is (e.g. is this an optional but common behavior)? – André Caron Aug 15 '11 at 18:24
  • AFAIR Windows has comparable facility, see links in @ftartaggia answer. – Nikolai Fetissov Aug 15 '11 at 18:26