From owner-freebsd-stable Fri Mar 30 6:13:35 2001 Delivered-To: freebsd-stable@freebsd.org Received: from veldy.net (w028.z064001117.msp-mn.dsl.cnc.net [64.1.117.28]) by hub.freebsd.org (Postfix) with ESMTP id 3892A37B71E for ; Fri, 30 Mar 2001 06:13:32 -0800 (PST) (envelope-from veldy@veldy.net) Received: from HP2500B (fuggle.veldy.net [64.1.117.28]) by veldy.net (Postfix) with SMTP id 02F38BA45; Fri, 30 Mar 2001 08:12:20 -0600 (CST) Message-ID: <005901c0b923$8d04f480$3028680a@tgt.com> From: "Thomas T. Veldhouse" To: "Valentin Nechayev" , "Brian Matthews" Cc: "'freebsd-stable@freebsd.org'" References: <20010330005243.A298@iv.nn.kiev.ua> Subject: Re: Threads vs. blocking sockets Date: Fri, 30 Mar 2001 08:13:09 -0600 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Sender: owner-freebsd-stable@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > > It is not correct even in case of single-threaded program. Consider, > for example, a case when waiting on blocking call is interrupted > with signal. If send() already transferred some data, it will return > number of bytes sent. If none data transferred, it will return -1 and > EINTR in errno. If you rely on sending all data in send() without > checking, your code is buggy. > > > As the standard FreeBSD threads are user space, they obviously can't let the > > socket call really block, so behind the scenes all sockets are created as > > nonblocking. However, I would then expect the threaded versions of the data > > transfer calls (send*, etc.) to loop over the actual system calls. They > > actually do so to catch EWOULDBLOCK/EAGAIN (see > > /usr/src/lib/libc_r/uthread/uthreads_sendto.c for example), but don't to > > make sure all the requested data is transferred. This seems to me like a > > bug. > > > > Thoughts, comments? > > Amount of data sent is implementation-, environment- and randomness-dependent. > Your current code works in 99.9% cases, but it is buggy. Fix it. > Nice little tidbit for you: int sendall(int sd, const char * buf, int * len) { int total = 0; int bytesleft = *len; int n; while (total < *len) { n = send(sd, buf + total, bytesleft, 0); if (n == -1) break; total += n; bytesleft -= n; } *len = total; return n == -1 ? -1 : 0; } I think you can thank Beej for this function. Tom Veldhouse veldy@veldy.net To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-stable" in the body of the message