在C中打开文件描述符
问题描述:
我有客户端 - 服务器应用程序。服务器是C. 服务器有这样的结构:
在C中打开文件描述符
int main (...) {
FILE * fp;
fp = fopen("serverLog.log","w");
//init variables
//bind server
//listen server on port
while(1) {
//some code
//accept
//some code
int check = pthread_create(&thread, NULL, handle_client,&ctx);
}
fclose(fp);
return EXIT_SUCCSESS;
}
我运行服务器,并使用CTRL + C关闭服务器。 filedescriptor fd会发生什么?我想,它保持开放。如果是的话,我该怎么做? Thx
答
不,它将被操作系统关闭。当你的进程退出时(无论干净还是强制)内核都会清除所有悬挂的句柄。
与此问题类似:http://*.com/questions/8175827/what-happens-if-i-dont-call-fclose-in-ac-program - 如果你退出程序,它的资源(内存,文件描述符)应该被操作系统回收。 – birryree 2012-03-07 20:53:51