From owner-freebsd-questions@freebsd.org Tue Jun 15 07:06:27 2021 Return-Path: Delivered-To: freebsd-questions@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 8252F64132F for ; Tue, 15 Jun 2021 07:06:27 +0000 (UTC) (envelope-from rfg@tristatelogic.com) Received: from outgoing.tristatelogic.com (segfault.tristatelogic.com [69.62.255.118]) by mx1.freebsd.org (Postfix) with ESMTP id 4G3zrL5TyCz4fwB for ; Tue, 15 Jun 2021 07:06:26 +0000 (UTC) (envelope-from rfg@tristatelogic.com) Received: by segfault.tristatelogic.com (Postfix, from userid 1237) id A9A504E657; Tue, 15 Jun 2021 00:06:25 -0700 (PDT) From: "Ronald F. Guilmette" To: FreeBSD Questions Subject: Re: Is a successful call to write(2) atomic? In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <22439.1623740785.1@segfault.tristatelogic.com> Content-Transfer-Encoding: quoted-printable Date: Tue, 15 Jun 2021 00:06:25 -0700 Message-ID: <22440.1623740785@segfault.tristatelogic.com> X-Rspamd-Queue-Id: 4G3zrL5TyCz4fwB X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=pass (mx1.freebsd.org: domain of rfg@tristatelogic.com designates 69.62.255.118 as permitted sender) smtp.mailfrom=rfg@tristatelogic.com X-Spamd-Result: default: False [-2.30 / 15.00]; ARC_NA(0.00)[]; RBL_DBL_DONT_QUERY_IPS(0.00)[69.62.255.118:from]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; R_SPF_ALLOW(-0.20)[+mx]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(0.00)[tristatelogic.com]; NEURAL_HAM_LONG(-1.00)[-1.000]; RCPT_COUNT_ONE(0.00)[1]; SPAMHAUS_ZRD(0.00)[69.62.255.118:from:127.0.2.255]; TO_DN_ALL(0.00)[]; NEURAL_HAM_SHORT(-1.00)[-0.996]; RCVD_COUNT_ZERO(0.00)[0]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:14051, ipnet:69.62.128.0/17, country:US]; SUBJECT_ENDS_QUESTION(1.00)[]; MAILMAN_DEST(0.00)[freebsd-questions] X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 15 Jun 2021 07:06:27 -0000 In message , = Paul Procacci wrote: >I had a long response, but decided to shorten it....I hope you don't mind= . Not at all. >The only time atomicity is guaranteed is for pipes when the write(2) size >fits in PIPE_BUF (512 bytes on my version of FreeBSD). >Otherwise, it depends is really the best answer as it depends on the file >system. >Sometimes filesystems like ext4 require O_DIRECT to be set, in order to >avoid the garbled text as you've mentioned, and even then it's limited to >the number of bytes written. This is deeply distrubing. I never knew about this. I am furiously readi= ng the FreeBSD & Linux man pages for open(2). (It appears that both support that flags, O_DIRECT and O_SYNC, aqnd Linux also adds the O_DSYNC flag. I feel a bit dumb that I never nknew about these until now.) >A suggestion; the way I've done something similar in the past..... > >Parent Process - Orchestrator - creates pipes handing one end of the pipe >to each child. >Child Process - do'ers - write whatever it intends on writing to the pipe >the parent is waiting for data on. >Parent process then grabs the data the child wrote, and in turn writes it >to stdout (or whatever file descriptor you intend). Yes, yes. Believe me when I say that I am considering reworking my code to do exactly this as we speak. I don't even know for sure if I have a "{lack of} atomicity" problem in my existing code, but I am 100% sure that what you just described would completely solve the problem, if indeed it does exist. >This method, a bit more involved, ensures one writer to a file descriptor= , >is portable, and you can rest easy about it working on whatever latest an= d >greatest file system someone decides to make. Yes. Thank you. Regards, rfg