0

我正在开发一个 UDP 客户端-服务器应用程序,其中一台服务器应该处理 40 个可以同时登录的客户端。

现在在 UNIX 中,通过使用fork基本上创建一个子进程来处理客户端并让服务器接受新连接的功能来解决这些问题。

我在互联网上搜索,发现fork在windows中不可用,但CreateProcess可以使用。

我之前的研究还向我介绍了线程池。所以现在我有两个问题:

  1. 我可以fork()通过CreateProcess()在 Windows 中使用来实现功能吗?
  2. 如果这是可能的,我应该去做什么:线程池或创建多个进程?
4

2 回答 2

0

1. Short answer is no, there is nothing like fork() in the Win32 API, but it should be possible to implement it since CygWin provide a fully featured fork() on Windows. But you don't seem to actually need fork()for your application.

2. Actually your options are :

  • Create at least one thread/process for each client/connexion
  • Create only one thread/process for all client/connexion (using an event-driven approach as instance)
  • Mix the two above

The best approach depends on your application but for handling a maximum of only 40 simultaneous client, you can very well have one thread per client.

You could use the POSIX standard thread library pthread to create and manipulate threads. This` library is standard in all POSIX-conformant OS (GNU/Linux, Mac OS X, BSD, etc.) and has been ported to Windows. So this approach would allow you to have a very nice portability.

Although if you want to use the Win32 API, you should take a look at CreateThread.

于 2013-04-18T09:58:46.093 回答
0

在 linux 中,fork 函数用于创建新进程。另外对于每个进程,都有不同的虚拟内存空间。对于线程,只有一个公共虚拟内存。此外,fork API 可以使用原生 API RtlCloneUserProcess 在 Windows(在某种程度上)进行模拟。

于 2013-04-18T10:10:53.787 回答