I am getting an error when calling fdopen
and it sets errno
to 22. I am using the exec command to call a child process. The child calls fdopen
on file descriptor 4. The first child works and sends data back to the parent and errno
is 0. After the parent creates the next child process, fdopen(4, "w");
is called again which is when errno
is set to 22.
From what I've read, errno
22 for fdopen()
could mean mode argument is incorrect. I also read that it could be an error from fnctl
and that could mean a bad file descriptor. I specify file descriptor 4 and it works on the first child process. Could that be why errno
is being set to 22 when I try to create another FILE*
?
I cannot figure out when it works for one child process but not the next. Can anyone shed some light on this for me?
Here is the code:
int main(int argc, char* argv[])
{
cout << "Child " << argv[argc-1] << " starting" << endl;
//close(3);
if(argc < 1) fatal("Not enough arguments provided to ChildMain");
int id = atoi(argv[argc-1]);
//Child kid((int) *argv[1]);
cout << "Error before fdopen(): " << errno << endl;
FILE* out = fdopen(4, "w");
if(out == NULL)
{
cout << "Child ID: " << id << endl;
cout << "\tError: " << errno << endl << endl;
}
int ret = fprintf(out, "%d", id);
fflush(out);
return 0;
}