Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: forking madness Date: Tue, 03 Mar 2015 09:44:31 -0800 Organization: None to speak of Lines: 37 Message-ID: <878ufe562o.fsf@kst-u.example.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="c77ff4d35e33d1c939d3461ebde8dd0d"; logging-data="8781"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX198SFrlebximXeepz5JUBuZ" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:5vmQt2COKhy5xHQZJvAojTBUir8= sha1:XBRx5ew1UNyj+dlZ0pBGLJ98p6A= Xref: csiph.com comp.lang.c:57855 Lőrinczy Zsigmond writes: > I don't really get what your question is, but I suggest > you don't use printf/puts when multiprocessing, > instead try something like this > (handle==1 for stdout, handle==2 for stderr): > > static void mylog (int handle, const char *fmt, ...) > { > char buff [1024]; > int len; > va_list ap; > > va_start (ap, fmt); > len= vsnprintf (buff, sizeof (buff), fmt, ap); > va_end (ap); > if (len<0 || (size_t)len>=sizeof (buff)) len= (int)sizeof(buff); > write (handle, buff, len); > } > > Note: if you want to write into file, use append mode > (eg ./myprog >>logfile) not to lose messages. I presume the point of that is to avoid buffering, so output appears immediately. What is the advantage of your (POSIX-only) solution over the standard printf("Your message here\n"); fflush(stdout); ? -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"