caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* printf and fork question
@ 2005-03-07 13:44 Michael
  2005-03-07 13:57 ` [Caml-list] " Gerd Stolpmann
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Michael @ 2005-03-07 13:44 UTC (permalink / raw)
  To: caml-list

Hi,

I don't understand why this:

open Unix;;
let _ = 
	Printf.printf "Hi! %d\n" (getpid());
	match fork() with
	| 0 -> if fork() <> 0 then exit 0;
	       ()
	| id -> ignore (waitpid [] id)

prints out that:
Hi! 12215
Hi! 12215
Hi! 12215


with ocaml 3.08.2. Any hints?

 Michael


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] printf and fork question
  2005-03-07 13:44 printf and fork question Michael
@ 2005-03-07 13:57 ` Gerd Stolpmann
  2005-03-07 14:03 ` Basile STARYNKEVITCH
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Gerd Stolpmann @ 2005-03-07 13:57 UTC (permalink / raw)
  To: Michael; +Cc: caml-list

Am Montag, den 07.03.2005, 14:44 +0100 schrieb Michael:
> Hi,
> 
> I don't understand why this:
> 
> open Unix;;
> let _ = 
> 	Printf.printf "Hi! %d\n" (getpid());
> 	match fork() with
> 	| 0 -> if fork() <> 0 then exit 0;
> 	       ()
> 	| id -> ignore (waitpid [] id)
> 
> prints out that:
> Hi! 12215
> Hi! 12215
> Hi! 12215

You have three copies of the stdout buffer. Call "flush stdout" after
printf.

Gerd
-- 
------------------------------------------------------------
Gerd Stolpmann * Viktoriastr. 45 * 64293 Darmstadt * Germany 
gerd@gerd-stolpmann.de          http://www.gerd-stolpmann.de
------------------------------------------------------------



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] printf and fork question
  2005-03-07 13:44 printf and fork question Michael
  2005-03-07 13:57 ` [Caml-list] " Gerd Stolpmann
@ 2005-03-07 14:03 ` Basile STARYNKEVITCH
  2005-03-07 14:05 ` David Fox
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Basile STARYNKEVITCH @ 2005-03-07 14:03 UTC (permalink / raw)
  To: caml-list

Le Mon, Mar 07, 2005 at 02:44:22PM +0100, Michael écrivait/wrote:
> Hi,
> 
> I don't understand why this:
> 
> open Unix;;
> let _ = 
> 	Printf.printf "Hi! %d\n" (getpid());

I suggest adding a 
        flush stdout;
at this point. Flushing buffers is always good (and not language
related) before forking under Unix.

> 	match fork() with
> 	| 0 -> if fork() <> 0 then exit 0;
> 	       ()
> 	| id -> ignore (waitpid [] id)
> 
> prints out that:
> Hi! 12215
> Hi! 12215

I don't know what is happening, but if the the flushing occurs after
the forks - eg at program exit - the behavior is understandable.

Regards.
-- 
Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
email: basile<at>starynkevitch<dot>net 
aliases: basile<at>tunes<dot>org = bstarynk<at>nerim<dot>net
8, rue de la Faïencerie, 92340 Bourg La Reine, France


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] printf and fork question
  2005-03-07 13:44 printf and fork question Michael
  2005-03-07 13:57 ` [Caml-list] " Gerd Stolpmann
  2005-03-07 14:03 ` Basile STARYNKEVITCH
@ 2005-03-07 14:05 ` David Fox
  2005-03-07 14:10 ` Olivier Andrieu
       [not found] ` <6b8a9142050307060758200ad3@mail.gmail.com>
  4 siblings, 0 replies; 6+ messages in thread
From: David Fox @ 2005-03-07 14:05 UTC (permalink / raw)
  To: Michael; +Cc: caml-list

It looks like a side effect of Printf.printf.  Try prerr_endline 
(Printf.sprintf "Hi! %d\n" (getpid ())) instead.

Michael wrote:

>Hi,
>
>I don't understand why this:
>
>open Unix;;
>let _ = 
>	Printf.printf "Hi! %d\n" (getpid());
>	match fork() with
>	| 0 -> if fork() <> 0 then exit 0;
>	       ()
>	| id -> ignore (waitpid [] id)
>
>prints out that:
>Hi! 12215
>Hi! 12215
>Hi! 12215
>
>
>with ocaml 3.08.2. Any hints?
>
> Michael
>
>_______________________________________________
>Caml-list mailing list. Subscription management:
>http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
>Archives: http://caml.inria.fr
>Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>Bug reports: http://caml.inria.fr/bin/caml-bugs
>  
>
--

This message contains information which may be confidential and privileged. Unless you are the 
addressee (or authorized to receive for the addressee), you may not use, copy or disclose to anyone 
the message or any information contained in the message. If you have received the message in error, 
please advise the sender and delete the message.  Thank you.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] printf and fork question
  2005-03-07 13:44 printf and fork question Michael
                   ` (2 preceding siblings ...)
  2005-03-07 14:05 ` David Fox
@ 2005-03-07 14:10 ` Olivier Andrieu
       [not found] ` <6b8a9142050307060758200ad3@mail.gmail.com>
  4 siblings, 0 replies; 6+ messages in thread
From: Olivier Andrieu @ 2005-03-07 14:10 UTC (permalink / raw)
  To: micha-1; +Cc: caml-list

 > Michael [Mon, 7 Mar 2005]:
 > Hi,
 > 
 > I don't understand why this:
 > 
 > open Unix;;
 > let _ = 
 > 	Printf.printf "Hi! %d\n" (getpid());
 > 	match fork() with
 > 	| 0 -> if fork() <> 0 then exit 0;
 > 	       ()
 > 	| id -> ignore (waitpid [] id)
 > 
 > prints out that:
 > Hi! 12215
 > Hi! 12215
 > Hi! 12215
 > 
 > 
 > with ocaml 3.08.2. Any hints?

You haven't flushed stdout : so the Hi! stays in the buffer and the
children inherit it.

-- 
   Olivier


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] printf and fork question
       [not found] ` <6b8a9142050307060758200ad3@mail.gmail.com>
@ 2005-03-07 14:12   ` Michael
  0 siblings, 0 replies; 6+ messages in thread
From: Michael @ 2005-03-07 14:12 UTC (permalink / raw)
  To: caml-list


thanks for the answers - I realized it at once after reading the word "buffered" in the replys :-)

 Michael


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-03-07 14:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-03-07 13:44 printf and fork question Michael
2005-03-07 13:57 ` [Caml-list] " Gerd Stolpmann
2005-03-07 14:03 ` Basile STARYNKEVITCH
2005-03-07 14:05 ` David Fox
2005-03-07 14:10 ` Olivier Andrieu
     [not found] ` <6b8a9142050307060758200ad3@mail.gmail.com>
2005-03-07 14:12   ` Michael

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).