【发布时间】:2011-02-27 14:54:50
【问题描述】:
我被这段模拟服务器-客户端交互的代码 sn-p 卡住了,假设 sockfd 是在服务器端创建的套接字文件描述符。
我的问题是,当父进程和它的子进程“同时”运行时,在子进程的执行时间片中,它关闭了服务器套接字 sockfd,然后当执行流到,比如说,第二次循环,调用accpet函数,那里的参数sockfd是否有效,是否被子进程关闭,即从内核文件描述符表中解除分配?
while (1) {
//accept a connection from client,get the new socket from client
//is the sockfd valid here,is it closed by the child in
//the previous loop
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
pid = fork();
if (pid < 0)
error("ERROR on fork");
if (pid == 0) {
close(sockfd); // can't this cause problem ??
dostuff(newsockfd);
exit(0);
}
else close(newsockfd);
} /* end of while */
【问题讨论】:
标签: sockets process parent-child