Program quits if pipe is closed
Posted
by
givemelight
on Stack Overflow
See other posts from Stack Overflow
or by givemelight
Published on 2013-07-01T04:17:04Z
Indexed on
2013/07/01
4:21 UTC
Read the original article
Hit count: 140
I am trying to write to a pipe using C++. The following code gets called in an extra thread:
void writeToPipe()
{
int outfifo;
char buf[100];
char outfile[] = "out";
mknod(outfile, S_IFIFO | 0666, 0);
if ((outfifo = open(outfile, O_WRONLY)) < 0) {
perror("Opening output fifo failed");
return false;
}
int currentTimestamp = (int)time(0);
int bufLen = sprintf(bug, "Time is %d.", currentTimestamp);
write(outfifo, buf, bufLen);
}
The thread is called in main using:
thread writeThread(writeToPipe);
writeThread.detach();
If the pipe is not opened by another process, the C++ program just quits without an error. I don't know how to check if the pipe is opened.
© Stack Overflow or respective owner