caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: IKEDA Katsumi <ikeda@msi.co.jp>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Client/Server socket communication
Date: Wed, 18 Sep 2002 13:04:00 +0900	[thread overview]
Message-ID: <20020918130400I.ikeda@msi.co.jp> (raw)
In-Reply-To: <20020918031351.4EC427F4F@lobus.fungible.com>

Hi,
Thank you for your quick reply.

From: tim@fungible.com (Tim Freeman)
Message-ID: <20020918031351.4EC427F4F@lobus.fungible.com>
> Another workaround is to fork twice, so the original process is the
> parent of a temporary process, and the temporary process is the parent
> of the process you really wanted to create.  The temporary process
> exits immediately and the original parent waits for it immediately.
> This leaves no defunct processes.

I try this solution.
But <defunct> is occurred.
Do I misunderstand your advice?

Modified server.ml is the following.

---------------- server.ml ----------------
(* server.ml *)

let openSock port = 
  let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
  begin
    Unix.bind sock (Unix.ADDR_INET(Unix.inet_addr_any,port));
    Unix.listen sock 20;
    sock
  end
;;

let waitChild s = 
  print_string "server: waitChild called\n";
  flush stdout ;
  let s =  Unix.waitpid [ Unix.WNOHANG ] 0 in ()
;;

let run_child fd =
  let pid2 = Unix.fork() in
  match pid2 with
    0 ->
      begin
	print_string "run a child process.\n";
	flush stdout;
	let buf = String.create 10 in
	let ret = Unix.read fd buf 0 10 in
	begin
	  print_string "ret = "; print_int ret; print_newline();
	  print_string buf; print_newline();
	  flush stdout;
	  Unix.sleep(5);
	  print_string "exit a child process.\n";
	  flush stdout;
	  exit 0
	end
      end
  | _ ->
      begin
	print_string "exit temporary parent.\n";
	flush stdout;
	exit 0
      end
;;
	

let main() =
  Sys.set_signal Sys.sigchld (Sys.Signal_handle waitChild) ;
  print_string "server started.\n";
  flush stdout;
  let port = 999999 in
  let sock = openSock port in
  while true do
    try
      let (fd,a) = Unix.accept sock in 
      begin
        print_string "server accepted \n"; 
        flush stdout;
        let pid = Unix.fork() in
        match pid with
          0  ->
	    run_child fd
	| _ -> (* parent process *)
	    begin
	      print_string "server parent side\n";
	      flush stdout ;
	    end
      end
    with
      Unix.Unix_error(err, ctx1, ctx2) ->
	if (err = Unix.EINTR) then
	  print_string "Function interrupted by signal\n"
 	else
	  begin
	    print_string "Unix.Unix_error in main\n";
	  end
    | _ -> print_string "Unexpected Exception\n"
  done;
;;

main();;
--------------------------------

thanks.

-- 
IKEDA Katsumi  <ikeda@msi.co.jp>
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2002-09-18  4:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-09-18  2:51 IKEDA Katsumi
2002-09-18  3:07 ` Tim Freeman
2002-09-18  4:04   ` IKEDA Katsumi [this message]
2002-09-18 19:27     ` Tim Freeman
2002-09-19  1:52       ` IKEDA Katsumi
2002-09-18  8:23   ` Alessandro Baretta

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020918130400I.ikeda@msi.co.jp \
    --to=ikeda@msi.co.jp \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).