在接受连接之前或之后分叉?

发布于 2024-08-07 23:54:10 字数 691 浏览 3 评论 0原文

以下代码片段创建 4 个进程,所有进程共享同一个侦听套接字。

这样做有什么危险吗?我是否应该以传统方式始终有一个侦听进程并在连接被接受后进行分叉?

for (p = 0; p < 3; p++) {
  pid = fork();
  if (pid == 0) break;
}
while (1) { 
  unsigned int clientlen = sizeof(echoclient);
  /* Wait for client connection */
  if ((clientsock = 
       accept(serversock, (struct sockaddr *) &echoclient,
              &clientlen)) < 0) { 
    die("Failed to accept client connection");
  } 
  fprintf(stdout, "Process No. %d - Client connected: %s\n",
                  p,
                  inet_ntoa(echoclient.sin_addr));
  handle_client(clientsock);
}

(我知道接受后分叉允许程序为每个连接创建一个进程。我正在研究原始线程和各种异步内容,所以我只是考虑每个核心有一个进程。)

The following snippet of code creates 4 processes, all sharing the same listening socket.

Is there any danger in doing this? Should I always have one listening process and fork after connections are accepted, in the conventional manner?

for (p = 0; p < 3; p++) {
  pid = fork();
  if (pid == 0) break;
}
while (1) { 
  unsigned int clientlen = sizeof(echoclient);
  /* Wait for client connection */
  if ((clientsock = 
       accept(serversock, (struct sockaddr *) &echoclient,
              &clientlen)) < 0) { 
    die("Failed to accept client connection");
  } 
  fprintf(stdout, "Process No. %d - Client connected: %s\n",
                  p,
                  inet_ntoa(echoclient.sin_addr));
  handle_client(clientsock);
}

(I understand that forking after accepting allows a programme to make a process per connection. I'm playing around with proto-threads and various async stuff, so I'm just looking at having one process per core.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枯叶蝶 2024-08-14 23:54:10

无论哪种方式你都可以做到。

正如您所注意到的,接受后分叉是每个客户端/连接一个子级。在接受之前(但在侦听之后)进行分叉通常称为预分叉。每个子进程都等待接受,无论哪个子进程收到传入连接都会对其进行处理。只要接受是由内核完成的(我认为)任何现代 UNIX 都会这样做,那么这是安全的。如果没有,您必须在接受周围放置某种 IPC(互斥锁等)锁。预分叉的优点是您不需要为每个连接花费分叉的费用,您已经拥有一个现有的池。

You can do it either way.

As you note, forking after the accept is one child per client/connection. Forking before the accept (but after the listen) is generally known as pre-forking. Each of the children wait on the accept and whatever child gets the incoming connection processes it. This is safe so long as the accept is done by the kernel which (I think) any modern unix does. If not, you have to put some kind of IPC (mutex, etc.) lock around the accept. The advantage to pre-forking is that you don't need to go through the expense of a fork for each connection, you already have an existing pool.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文