From owner-freebsd-hackers@freebsd.org Wed Oct 11 18:54:12 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 DD9ADE33B3F for ; Wed, 11 Oct 2017 18:54:12 +0000 (UTC) (envelope-from otacilio.neto@bsd.com.br) Received: from mail-qt0-x232.google.com (mail-qt0-x232.google.com [IPv6:2607:f8b0:400d:c0d::232]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 9DED372A7C for ; Wed, 11 Oct 2017 18:54:12 +0000 (UTC) (envelope-from otacilio.neto@bsd.com.br) Received: by mail-qt0-x232.google.com with SMTP id n61so8214726qte.10 for ; Wed, 11 Oct 2017 11:54:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsd.com.br; s=capeta; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=0fLqCF/3yocmpRLN47y4G6x/v2wMum8RltHRXoYyghc=; b=P48cw7QcyP+Nd4le9E7PlRsGk2gH/65H2X9FZ1p9GbjPnPUVg76PrtcO6WXu7UyWnL a7H3J+MkvMwwNuN/RVgx5Kit0YeckDbGP0sE560adEaEcMblo7H4cbwJ5ZaIHD1oQeet BT9y3gtkalaxZl21dGO1UhJkF3VgAtzY/AGfM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=0fLqCF/3yocmpRLN47y4G6x/v2wMum8RltHRXoYyghc=; b=a88u4Y8CjA8jw+Ug+YD2XhQTey/E0Zqqrp06W8r0HOcyKG87pEmFhz+ZNXJuKiBvO6 U7kGdVCmrHORODbG47O9cSQLk+wUkNDgY7k1397U19KvcPqEisoscSqqEHc7Swpg7H7g 5pb66H9il0gwX1MKJW2CBN1T06Cenlx3cA15yIY/N7bzGPaQWS1UL0unpYx9TlWVrMil ZFAQ0ICpjmi0QXo1SKe5WGg/WZm2CSdc14hl5X9PqWyJufUPl1hUZtFLfSkUDE2v7hbq V2TaSliV3VTVPkwIRJ6sRQfY+I/qn6bUTAm+VqfbuX9ZDB+rndwc+T3Gvv1ASDVM115t Z25w== X-Gm-Message-State: AMCzsaUTCUhgWqhaBzxRNCSIhE5TFMkogSM1PPu/RWEysyJjizgbU8g/ 51hyB4iF9yl/gNt2077VE34E/2rT+8E= X-Google-Smtp-Source: ABhQp+T/wdMsYZeyckaffp6VhFqe+xSzmLyntQeIAJHE5p+qPRA8atp4g09lSp0W5rMyM8jiPLHw4g== X-Received: by 10.233.244.74 with SMTP id z10mr916038qkl.31.1507748051290; Wed, 11 Oct 2017 11:54:11 -0700 (PDT) Received: from [192.168.43.219] (179-240-150-42.3g.claro.net.br. [179.240.150.42]) by smtp.googlemail.com with ESMTPSA id l1sm3561320qtf.5.2017.10.11.11.54.09 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 Oct 2017 11:54:10 -0700 (PDT) Subject: Re: fprintf - threadsafe? - i.e. with process linked against '-pthread'? To: freebsd-hackers@freebsd.org References: <03DA6274A199550235DC7351@[10.12.30.106]> <20171011144809.GA95911@kib.kiev.ua> From: =?UTF-8?B?T3RhY8OtbGlv?= Message-ID: Date: Wed, 11 Oct 2017 15:54:01 -0300 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: <20171011144809.GA95911@kib.kiev.ua> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: pt-BR 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 18:54:13 -0000 Em 11/10/2017 11:48, Konstantin Belousov escreveu: > On Wed, Oct 11, 2017 at 10:13:35AM +0100, Karl Pielorz wrote: >> 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. > Does the program use cancellation ? If yes, it might be an issue solved > by the r321074. > > Otherwise, you need to debug the program. I usually use ktrace for start, > but if the event is rare and program intensively issues syscalls, you would > need to develop some ad-hoc tecnhique. > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > https://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org" This program is able to reproduce the wrong behavior that you report as it also contains a solution to it. I tested on a FreeBSD 11.1 running on the virtualbox with three processors. To compile with bug cc -Wall -O2 -o main main.c -lpthread -DBUGED ./main 10 out To compile without bug cc -Wall -O2 -o main main.c -lpthread ./main 10 out If your problem is something like this, apparently the solution is for all threads to use the same variable FILE * #include #include #include #include #include #include #ifndef MAX_THREADS #define MAX_THREADS    1000 #endif void *thread_main(void *file){     unsigned int i;     #ifdef BUGED     FILE *arquivo  = fopen((char*)file, "a");     #else     FILE *arquivo = (FILE*) file;     #endif     for(i=0; i<3000; i++){         fprintf(arquivo, "The quick brown %d jumped over the slow lazy animal %d\n", getpid(), i);         pthread_yield();     }     pthread_exit(NULL);     return NULL; } int main(int argc, char **argv){     unsigned int j;     unsigned int threads;     static pthread_t mythread[MAX_THREADS];     FILE *arquivo;     if(argc != 3){         fprintf(stderr,"Use %s \n", argv[0]);         exit(EXIT_FAILURE);     }     threads = (unsigned int)strtol(argv[1], (char **)NULL, 10);     if(threads>MAX_THREADS){         fprintf(stderr, "Max thread suppor %dt\n", MAX_THREADS);         exit(EXIT_FAILURE);     }     if((arquivo = fopen(argv[2],"w+"))==NULL){         fprintf(stderr, "fopen error\n");         exit(EXIT_FAILURE);     }     for(j=0; j < threads; j++){         #ifdef BUGED         if (pthread_create( &mythread[j], NULL, thread_main, argv[2])){         #else         if (pthread_create( &mythread[j], NULL, thread_main, arquivo)){         #endif             fprintf(stderr,"thread create error.\n");             exit(EXIT_FAILURE);         }     }     //Vamos esperar pela conclusão de cada um dos threads     for(j=0; j