From owner-freebsd-hackers@freebsd.org Wed Oct 11 09:41:32 2017 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 288ACE26808 for ; Wed, 11 Oct 2017 09:41:32 +0000 (UTC) (envelope-from kpielorz_lst@tdx.co.uk) Received: from smtp.krpservers.com (smtp.krpservers.com [62.13.128.145]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "*.krpservers.com", Issuer "RapidSSL SHA256 CA" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id C58AB1A0A for ; Wed, 11 Oct 2017 09:41:31 +0000 (UTC) (envelope-from kpielorz_lst@tdx.co.uk) Received: from [10.12.30.106] (host31-48-228-38.range31-48.btcentralplus.com [31.48.228.38]) (authenticated bits=0) by smtp.krpservers.com (8.15.2/8.15.2) with ESMTPSA id v9B9Dqg2075943 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 11 Oct 2017 10:13:53 +0100 (BST) (envelope-from kpielorz_lst@tdx.co.uk) Date: Wed, 11 Oct 2017 10:13:35 +0100 From: Karl Pielorz To: freebsd-hackers@freebsd.org Subject: fprintf - threadsafe? - i.e. with process linked against '-pthread'? Message-ID: <03DA6274A199550235DC7351@[10.12.30.106]> X-Mailer: Mulberry/4.0.8 (Win32) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 Oct 2017 09:41:32 -0000 Hi, I have a number of 10.3-R amd64 boxes which runs a heavily threaded process. This is linked against '-pthread' - and compiles / runs fine. Using 'fprintf' to log data to a file - it sometimes doesn't complete writing the line - e.g. literally in code: fprintf( fd, "The quick brown %s jumped over the slow lazy animal\n", animal ); Will sometimes result in: " The quick brown fox ju" Being written to the file. Presumably (and from what I can see) fprintf is 'thread safe'? - And it also appears multiple threads could write to a single file using it (i.e. it provides for atomic writes so lines won't intermingle - the lines written don't seem to intermingle). The process doesn't crash - but I can't understand why / how frpintf could either stop, or get stopped 'mid way' through? e.g. If a signal occurred would it complete the write to file? This only happens very, very occasionally (one fprintf out of many millions, with hundreds of threads running). Just a bit stumped as to what to try looking at to fix / debug the problem - if anyone has any suggestions, or further reading I can look at. -Karl