[comp.os.research] lightweight process

yunyau@bingvaxu.cc.binghamton.edu (Yun Yau Shih) (05/16/91)

Hi there:

A few days ago, I post a question to comp.os.misc but received one
reply only. So, I post to this group and hope I can get much more
comment/reference. 

My question is :

Could anyone give me a simple *example* for lightweight process?
I know the definition and try to catch a much more concrete idea.

Any comment is appreciated.


-- 

yunyau

montra@ghost.unimi.it (Paolo Montrasio) (05/20/91)

yunyau@bingvaxu.cc.binghamton.edu (Yun Yau Shih) writes:

>Hi there:

>A few days ago, I post a question to comp.os.misc but received one
>reply only. So, I post to this group and hope I can get much more
>comment/reference. 

>My question is :

>Could anyone give me a simple *example* for lightweight process?
>I know the definition and try to catch a much more concrete idea.

>Any comment is appreciated.

I never used them, but I _think_ they are something a UNIX fork() that
doesn't duplicate the process memory image. The lightweight processes
share the same code, so they are not as indipendent as normal (heavyweight)
processes are. The contest-switch time is greatly reduced because the OS
needs only to save the registers.

Lightweight processes are sometime referred (in implementatios)
as "threads". I know that the Mach O.S. (the kernel of Next BSD O.S.) has
threads.

I hope somebody will be able to help you more than I did, however this is
all I know about this topic.
                             _____
                            /     \ 
.--------------------------|\ P M /|---------------------------------------.
|                          |_\   /.|   |        Paolo Montrasio            |
| Don't study, don't work, |--\_/--|   |        montra@ghost.unimi.it      |
|      play the Core War!   \ \_/ /    |        montra@[131.175.10.64]     |
|                          __|---|__   |        MILANO - ITALY             |
`------------------------/           \-------------------------------------'

lm@slovax.Eng.Sun.COM (Larry McVoy) (05/23/91)

yunyau@bingvaxu.cc.binghamton.edu (Yun Yau Shih) writes:
>Could anyone give me a simple *example* for lightweight process?
>I know the definition and try to catch a much more concrete idea.

This is actually a harder question then I thought.  Bummer.

The classic example is the window system, with one thread handling
events for each window (somethimes more than one thread).  But
that's not a simple example.

Here's (maybe) a better example.  Usually, events arrive over a file
descriptor (socket, tty, etc).  If we have a program that is managing
many of these file descriptors, the canonical form uses select to
multiplex all of the file descriptors into one thread of control.

/*
 * multiplexing events
 */
while (1) {
	int	fds[20];

	/*
	 * Build the bitmask for select and wait for something to happen
	 */
	for (i = mask = 0; i < 20; ++i)
		mask |= (1 << fds[i]);
	if (select(20, &mask, 0, 0, 0) > 0) {
		for (i = 0; i < 20; ++i) {
			if (mask & (1 << i)) {
				read_it_and_return(fds[i]);
			}
		}
	}
}

/*
 * Same thing done with threads.
 * Here we thread_fork() all the threads and they go off and block in reads,
 * each on their respective file descriptor.
 * Much cleaner but requires threads w/ kernel support.
 */
extern	read_in_a_loop();
for (i = 0; i < 20; ++i) {
	thread_create(read_in_a_loop, fds[i]);
}
wait_for_all_threads();
---
Larry McVoy, Sun Microsystems     (415) 336-7627       ...!sun!lm or lm@sun.com