From owner-freebsd-questions@freebsd.org Wed Jun 16 01:26:09 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 D5F35658748 for ; Wed, 16 Jun 2021 01:26:09 +0000 (UTC) (envelope-from johnl@iecc.com) Received: from gal.iecc.com (gal.iecc.com [IPv6:2001:470:1f07:1126:0:43:6f73:7461]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "gal.iecc.com", Issuer "Let's Encrypt Authority X3" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4G4SFD2zfYz4SB6 for ; Wed, 16 Jun 2021 01:26:08 +0000 (UTC) (envelope-from johnl@iecc.com) Received: (qmail 30474 invoked from network); 16 Jun 2021 01:26:07 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple; d=iecc.com; h=date:message-id:from:to:cc:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:cleverness; s=76e8.60c9532f.k2106; bh=PIpD/mv7BOPOP//RO+z/xoCXK2tvMXpWEVE8QI3IOtU=; b=ftf1rkF3Tp1cDalMJ/QsawlRJVOYyg3BiI9cUrwG/aUBJMQEKG78sGqw9VP2e6/rcaig6nPS3BcDuz9HRPPDk4lSifTppX+Bez1yVXwPBUqEjjdRjZsiUx1SMmuo9XACa++joLbKKcbNvQtXb1C0uSwzMlzR83Wora2unKYxD7xxDVdJ5mMNEe3I2Z8utJ60+Mz6bocdgfAGR9tAOqMU0Ubq/pzTnjoYJYHmG7gRBiMDqTcZKdy5AknQgGtPgKPgtCUfFadJ9JkwIXTfC+KlzYwXkimLNCd0qRbXvzuh46q7T1euwDNgjL3RmLoUtduuzVYK56OZzTPulll3flF5OA== Received: from ary.qy ([IPv6:2001:470:1f07:1126::78:696d:6170]) by imap.iecc.com ([IPv6:2001:470:1f07:1126::78:696d:6170]) with ESMTPS (TLS1.2 ECDHE-RSA AES-256-GCM AEAD) via TCP6; 16 Jun 2021 01:26:06 -0000 Received: by ary.qy (Postfix, from userid 501) id 3C77A1183464; Tue, 15 Jun 2021 21:26:04 -0400 (EDT) Date: 15 Jun 2021 21:26:04 -0400 Message-Id: <20210616012606.3C77A1183464@ary.qy> From: "John Levine" To: freebsd-questions@freebsd.org Cc: rfg@tristatelogic.com Subject: Re: Is a successful call to write(2) atomic? In-Reply-To: <26258.1623802141@segfault.tristatelogic.com> Organization: Taughannock Networks X-Headerized: yes Cleverness: minimal Mime-Version: 1.0 Content-type: text/plain; charset=utf-8 Content-transfer-encoding: 8bit X-Rspamd-Queue-Id: 4G4SFD2zfYz4SB6 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org; dkim=none (invalid DKIM record) header.d=iecc.com header.s=76e8.60c9532f.k2106 header.b=ftf1rkF3; dmarc=pass (policy=none) header.from=iecc.com; spf=pass (mx1.freebsd.org: domain of johnl@iecc.com designates 2001:470:1f07:1126:0:43:6f73:7461 as permitted sender) smtp.mailfrom=johnl@iecc.com X-Spamd-Result: default: False [-2.30 / 15.00]; RCVD_TLS_ALL(0.00)[]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; FROM_HAS_DN(0.00)[]; RBL_DBL_DONT_QUERY_IPS(0.00)[2001:470:1f07:1126:0:43:6f73:7461:from]; MV_CASE(0.50)[]; R_SPF_ALLOW(-0.20)[+ip6:2001:470:1f07:1126::/64]; MIME_GOOD(-0.10)[text/plain]; TO_DN_NONE(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; HAS_ORG_HEADER(0.00)[]; SPAMHAUS_ZRD(0.00)[2001:470:1f07:1126:0:43:6f73:7461:from:127.0.2.255]; TO_MATCH_ENVRCPT_SOME(0.00)[]; DKIM_TRACE(0.00)[iecc.com:~]; RCPT_COUNT_TWO(0.00)[2]; RCVD_IN_DNSWL_NONE(0.00)[2001:470:1f07:1126:0:43:6f73:7461:from]; NEURAL_HAM_SHORT(-1.00)[-1.000]; R_DKIM_PERMFAIL(0.00)[iecc.com:s=76e8.60c9532f.k2106]; DMARC_POLICY_ALLOW(-0.50)[iecc.com,none]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; SUBJECT_ENDS_QUESTION(1.00)[]; ASN(0.00)[asn:6939, ipnet:2001:470::/32, country:US]; RCVD_COUNT_TWO(0.00)[2]; 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: Wed, 16 Jun 2021 01:26:09 -0000 It appears that Ronald F. Guilmette said: >More to the point, if indeed each call to write() *is* "atomic"... at least >in the sense that the given buffer will be treated like an indivisable whole, >all of the way along its journey to some physical device... then why are >users nontheless being encouraged, still, to "use some form of concurrency >control"? I mean what would be the point of that if in fact write() never >busts up the hunks of data given to it into separate sub-hunks? I think it depends on the device. If I just want to write stuff to a log file and not get it scrambled, this should do the trick: fd = open("somefile", O_CREAT|O_WRONLY|O_APPEND); /* put some stuff in buf[] */ flock(fd, LOCK_EX); write(fd, buf, strlen(buf)): /* O_APPEND ensures it's added at the end */ flock(fd, LOCK_UN); R's. John