Reading from a pty after slave exited
Reading from a pty after slave exited
- Subject: Reading from a pty after slave exited
- From: Julian Scheid <email@hidden>
- Date: Sat, 17 Apr 2010 00:36:27 +1200
The attached program works on Linux but fails on Darwin (OS X 10.6.3).
Linux will read "foo", on Darwin read returns 0.
It appears that the master-side file descriptor gets flushed when the
slave exits - any data written by the slave but not yet read by the
master appears to be lost at that point.
Moving the waitpid call behind the read solves the problem, but how
can I know for certain that the master has read all data? What if the
master process gets starved for whatever reason and doesn't manage to
drain the file descriptor by the time the slave exits?
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#ifdef __APPLE__
#include <util.h>
#else
#include <pty.h>
#endif
#include <string.h>
int main() {
const char* test_string = "foo";
int pid, fd;
pid = forkpty(&fd, NULL, NULL, NULL);
assert(pid >= 0);
if (pid == 0) {
/* slave */
int nwritten = write(fileno(stdout), test_string, sizeof test_string);
assert(nwritten == sizeof test_string);
exit(0);
}
else {
/* master */
waitpid(pid, NULL, 0);
char buf[32];
int nread = read(fd, buf, sizeof buf);
/* reads 0 bytes on darwin - works fine without waitpid call */
assert(nread > 0);
assert(0 == strcmp(buf, test_string));
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden