site_archiver@lists.apple.com Delivered-To: darwin-kernel@lists.apple.com Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=domainkey-signature:mime-version:received:date:received:message-id :subject:from:to:content-type; bh=GdkvNfjLDb9X9v2OP9JPv40Kex1Ndya5ywbgpNnz6fY=; b=TkMJYGXatSnXQxEluj+LXUGrF1l4cyctMGfQRnbDjcwDgolRFcAuGApuLGpWI9xqQT oWiHBp+Eh23lQVCNd4IDvP+Isq1+yYlJUq7ShYGzmws3wHoKSlkqmbWmgGYK7KYn2Wc5 KxcJaqt/j+i9AKTsewlqI73Cq2re9eIL50c+E= Domainkey-signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=V6lrQu6Jt4PlvK8fln9xYccezo48GqS1To7vpMvuW7PO4+uG+GNYCyALAr5Nr87xVG PpApwuLqEgHN8Zq5GRqOnIFnFrIUbcC+A9lqNAoG68Jr5XhkiSRWJZidiobjZFCvZGz1 Ffajh+6jsNwVsZCKp/vMSXfD26/UOKOur9zXM= 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 (Darwin-kernel@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-kernel/site_archiver%40lists.a... This email sent to site_archiver@lists.apple.com