How to determine if a thread is closed in C before calling fclose ()

I have an unsuccessful C program, and I narrowed it down to an fork()ed child trying to close stdout and stderr that were closed by its parent process before the call fork()- I assume that these threads were passed to the child process.

how can I determine if a thread is closed in C before trying to close it using something like fclose(stdout)

+3
source share
4 answers

C programs on UNIX suggest that you start will be open file descriptors 0, 1 and 2. If you do not want them to go anywhere, go /dev/nulland dupfor these file descriptors.

+2
source

, , , FILE C (stdin co), .

, , lseek(fd, 0, SEEK_SET) , , .

0

ftell(), . -1, .

0

fclose() undefined.

, FILE * stdout, stdout, , /.

stdout , fd 1.

struct stat stbuf;
if(fstat(1,&stbuf) == -1) {
  if(errno == EBADF) {
    stdout isn't open/valid
  }
}
0

Source: https://habr.com/ru/post/1735186/


All Articles