caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: John Max Skaller <skaller@maxtal.com.au>
To: Ari Heitner <aheitner@andrew.cmu.edu>
Cc: web-caml@quatramaran.ens.fr, caml-list@inria.fr
Subject: Re: [Caml-list] Web Development with OCaml
Date: Thu, 19 Jul 2001 08:24:50 +1000	[thread overview]
Message-ID: <3B560CB2.9ACF98BF@maxtal.com.au> (raw)
In-Reply-To: <20010717145028.A24276@andrew.cmu.edu>

Ari Heitner wrote:

> This is how I've been wanting to do communication for some time.
> 
> My background for this is actually videogames -- something I started doing
> in high school in a DOS extender.
>
	I did that too, but commercially. Didn't pan out.
But I got to use the best C++ compiler around (Metaware: had nested
functions with closures, and iterators modelled as continuations!)

> I've looked at Felix a tiny bit, but not enough to know how it interprets
> event-driven stuff.

	The current, deficient, model is easy to explain.
You write:

	// blah blah sequential code
	read x;
	// blah blah using x
	read x;

and when you hit 'read x', you lose control.

The dispatch loop then grabs the next
message from the input queue, and writes it into the object 
that should get it, and then 'resumes' the code just after
the 'read' it did. Its done by:

	switch(pc) {
	case 1: ...
	case 2:
		// read x
		data_required_in = &x;
		pc = 3;
		return this;
	case 3:
		...

This is exactly what the OS does when you say 'fread()' in C.

The difference is that your Felix code yields to YOUR driver
program, not the OS. You have to write that driver yourself.

>..I never felt uncomfortable programming event-driven
> stuff in C++, 

	Try something difficult. Like writing a compiler
whose parser is _called_ with each line of the source,
instead of reading it. :-)

> but then, I literally grew up on it :) you just have to keep a
> good tag on how how long any even takes :)

	That's not the problem: the difficulty is that you
are back to programming in a flat data space without
a stack. That is, you're back to computer science as it was
pre- structured programming. You're writing COBOL or worse.

	Now look at the immense power of functional programming
languages and imagine what would happen if you didn't have a stack.
It's all gone. No recursion, for example! So you have to emulate
a stack. You have to write your own homebrew interpreter/OS.

> I never seriously looked at implementing any of this -- it's way off topic
> for my normal stuff. But surely Linux or the BSDs have some kind of raw
> ethernet interface? 

	Sure, but thats WAY too low level for me!

> But doesn't this sound a lot like what's happening in kernel space anyhow? 

	Absolutely. The problem, as I said, is that the OS provides a rich
set of facilities for scheduling. As a result, context switching
is extremely slow. For many applications, such as GUI and telephony
and web servers, you just don't need that rich set of tools for the
bulk of the work (you DO need a few processes/threads, but NOT one
per connection!)

> So what's the hangup? I'm no longer sure :) 

	A Web server must play the TCP/IP game because that's
what clients are playing. That means, the server must obey the
TCP/IP protocol. 

> Is it just the expense of having
> zillions of threads/processes managing all these connections, and context
> switching expensively to get tiny bits of work done? 

	Yes. The problem is deciding which thread to run next,
out of the 100,000 you have waiting on the 100,000 sockets you have open
for each of the 100,000 users buying your merchandise by e-commerce.
[Imagine Amazon, or an online newspaper, or a bank .. you could easily
have that
many users connected]

You can't easily get
> away w/o at least a process or thread for your DB work, 

	Of course. You need some threads/processes. But not one per connection:
the connections are all typically idle most of the time, and the 
response will be maximised by serialising the requests (into one stream
per CPU).

> database needs fore each query are complicated and it would be *really* hard
> to manage that in an event-oriented way. 

	Event driven programming of anything non-trivial is almost impossible
by hand, because you have to maintain ALL the state yourself ..
including
the current locus of control (program counter) .. because the stack
has to be empty at each blocking point.

> So even if your webserver was a
> single thread, or even entirely in kernel space, you spend a lot on managing
> the assebly of data from your DB -- which has to be done (at least) a thread
> per conceptual request?

	No. Access to the DB is done by passing messages to the DB server.
Typically, thats a small collection of processes. Your request just
joins the queue. That is, it must be done asynchronously. You don't
get an answer back immediately: you have to 'read' the answer
(that is, block until the DB gets around to responding to your request).

> Or does Felix do that nicely too? Or did I totally miss the point?
> 
> [or am i just rambling?]

	Felix doesn't do ANY of the threading work. It doesn't
even do any scheduling. You have to write all that in C/C++ (or Ocaml,
or whatever). What Felix does is builds objects with resume methods
that you can call from your driver, and it builds them from ML like
code in which you 'read' data, but you actually get called _with_ 
the data.

	BTW: none of this is mentioned in the tutorial yet.
One reason is that the design is deficient: there's only ONE
input queue, because you can only say 'read x'. You can't
read _from_ something. Which means you can't write a Felix dispatcher
in Felix, which isn't acceptable. At present, the continuations
are transparent: they need to be brought into the type system
(and I don't know how to do that) Its possible that reading
_from_ a channel isn't the right idea (JoCaml doesn't do that,
for example).

-- 
John (Max) Skaller, mailto:skaller@maxtal.com.au 
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
New generation programming language Felix  http://felix.sourceforge.net
Literate Programming tool Interscript     
http://Interscript.sourceforge.net
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


  reply	other threads:[~2001-07-18 22:25 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-07-10  0:01 Jimmie Houchin
2001-07-10  7:10 ` Jean-Christophe Filliatre
2001-07-10  7:15 ` Tom Hirschowitz
2001-07-10  8:19   ` David Mentre
2001-07-10  9:38   ` Alain Frisch
2001-07-11  5:49     ` Jimmie Houchin
2001-07-11  6:03       ` Alexander V. Voinov
2001-07-11 14:47         ` Jimmie Houchin
2001-07-12  1:58           ` John Max Skaller
2001-07-11  6:19       ` Alain Frisch
2001-07-11  9:09         ` Samuel Heriard Dubreuil
2001-07-11 14:11           ` Jimmie Houchin
2001-07-11 15:35       ` Francois Rouaix
2001-07-11 20:44         ` Gerd Stolpmann
2001-07-12  2:32         ` Jimmie Houchin
2001-07-13  5:37       ` William Chesters
2001-07-13 10:29         ` Alain Frisch
2001-07-13 11:16           ` Vitaly Lugovsky
2001-07-13 14:04             ` Xavier Leroy
2001-07-13 17:08               ` [web-caml] " Vitaly Lugovsky
2001-07-15 18:03               ` Ari Heitner
2001-07-15 20:19                 ` Gerd Stolpmann
2001-07-16  8:23                 ` wakita
2001-07-17 16:18                 ` John Max Skaller
2001-07-17 18:50                   ` Ari Heitner
2001-07-18 22:24                     ` John Max Skaller [this message]
2001-07-13 16:12           ` Lyn A Headley
2001-07-13 17:50             ` William Chesters
2001-07-13 16:51           ` Miles Egan
2001-07-13 18:12           ` Jimmie Houchin

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=3B560CB2.9ACF98BF@maxtal.com.au \
    --to=skaller@maxtal.com.au \
    --cc=aheitner@andrew.cmu.edu \
    --cc=caml-list@inria.fr \
    --cc=web-caml@quatramaran.ens.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).