|
|
[PHP]
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
main()
{
int pid, ssock, x;
for (x = 1; x < 5; x++)
{
pid = fork();
if (pid == 0)
{
ssock = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
printf("ssock: %d ,cycle: %d\n",ssock,x);
return;
}
}
wait();
printf("main exit\n");
}
[/PHP]
运行结果:
woody:~/test# ./test1
pid: 21544,ssock: 3 ,cycle: 1
pid: 21545,ssock: 3 ,cycle: 2
pid: 21547,ssock: 3 ,cycle: 4
pid: 21546,ssock: 3 ,cycle: 3
main exit
woody:~/test#
为什么每次fork出来的进程用socket函数在系统注册的通信端口都是同一个,这样各个进程用同一个通信端口会出问题的. |
|