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 line
fclose(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.