12.27
FILE *fpin, *fpout;
fpin = fdopen(sockfd, "r");
fpout = fdopen(sockfd, "w");
// read and write
fclose(fpin);
fclose(fpout);
ref: 10.11
fdopen
open 2 stream on same sockfd, fdclose
will close sockfd under stream.
if you call fclose
2 stream on the same sockfd, the second fclose
will fail.
image one thread execute code and open 2 stream on fd N. after execution of linefclose(fpin);
, program create another thread and execute the same code.
but
after fclose(fpin);
in thread 1, fd N is reusable again. assume thread 2 use
the fd N again: thread 1 execute line fclose(fpout);
close the fd that thread
2 is using. it’ll cause something unpredicted.
当前内容版权归 DreamAndDead 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 DreamAndDead .