From owner-freebsd-chat Thu Nov 22 21:58:16 2001 Delivered-To: freebsd-chat@freebsd.org Received: from mail.thpoon.com (CPE0080c8f2c614.cpe.net.cable.rogers.com [24.42.106.79]) by hub.freebsd.org (Postfix) with SMTP id A81F637B405 for ; Thu, 22 Nov 2001 21:58:10 -0800 (PST) Received: (qmail 43039 invoked from network); 23 Nov 2001 05:58:09 -0000 Received: from unknown (HELO tea.thpoon.com) (qmailr@192.168.1.2) by 192.168.1.1 with SMTP; 23 Nov 2001 05:58:09 -0000 Received: (qmail 10075 invoked by uid 1000); 23 Nov 2001 05:58:08 -0000 To: freebsd-chat@freebsd.org Subject: Reentrant readn() and writen() for socket communication From: Arcady Genkin X-Face: 0=A/O5-+sE[Tf%X>rYr?Y5LD4,:^'jaJ!4jC&UR*ZrrK2>^`g22Qeb]!:d;}2YJ|Hq"LHdF OX`jWX|AT-WVFQ(TPhFVak)0nt$aEdlOq=1~D,:\z5QlVOrZ2(H,mKg=Xr|'VlHA="r Mail-Copies-To: never Date: 23 Nov 2001 00:58:08 -0500 Message-ID: <873d363u6n.fsf@tea.thpoon.com> Lines: 42 User-Agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Academic Rigor) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-freebsd-chat@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org Stevens' UNIX Network Programming show sample readn()[1] and writen() functions to read/write from/to a socket. The functions handle interrupted read()/write() system calls by examining errno value. This approach is not thread-safe (because of usage of errno global). Is there a reentrant way to detect under-reads/writes, or had I better off with just using recv() with MSG_WAITALL flag? Footnotes: [1] Here's the code of readn() for easy reference: ssize_t readn( int fd, void *vptr, size_t n ) { size_t nleft; ssize_t nread; char *ptr; ptr = vptr; nleft = n; while ( nleft > 0 ) { if ( (nread = read(fd, ptr, nleft)) < 0 ) { if (errno == EINTR) nread = 0; /* and call read() again */ else return (-1); } else if ( nread == 0 ) break; /* EOF */ nleft -= nread; ptr += nread; } return (n - nleft); /* >= 0 */ } Many thanks, -- Arcady Genkin i=1; while 1, hilb(i); i=i+1; end To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-chat" in the body of the message