[comp.unix.wizards] STREAMS module / dup question

murrey@lehi3b15.csee.Lehigh.EDU (Erik Murrey) (05/14/89)

Here's one for the STREAMS experts out there...

[ UNIX System v.3.1 ]

What if:
	1. I open a STREAM.
	2. I dup that stream, so now I have 2 fd's referring to
		that stream.
	3. I push a module on one of the fd's.

Questions:
	1. Do both of those fd's have that module pushed on it?
	2. What happens during a fork()?

I'm sure this is a case of RTFM, but the indexes are terrible...

Thanks for the help.

... Erik-- 
Erik Murrey
Lehigh University
murrey@csee.Lehigh.EDU
erik@mpx.com

ka@june.cs.washington.edu (Kenneth Almquist) (05/14/89)

murrey@lehi3b15.csee.Lehigh.EDU (Erik Murrey) asks:

> What if:
> 	1. I open a STREAM.  [ UNIX System v.3.1 ]
> 	2. I dup that stream, so now I have 2 fd's referring to
> 		that stream.
> 	3. I push a module on one of the fd's.
>
> Questions:
> 	1. Do both of those fd's have that module pushed on it?

Yes.

> 	2. What happens during a fork()?

Both the parent and the child share the stream.  For example, if
the child process pushes a module onto the stream, and then the
parent process does a read from the stream, the input to the
parent process will pass through the module that the child process
pushed onto the stream.

(Disclaimer:  This is what I think it should do.  I haven't actually
tested the code.)
				Kenneth Almquist

dougm@ico.ISC.COM (Doug McCallum) (05/15/89)

In article <569@lehi3b15.csee.Lehigh.EDU> murrey@lehi3b15.csee.Lehigh.EDU (Erik Murrey) writes:
...
>Questions:
>	1. Do both of those fd's have that module pushed on it?
>	2. What happens during a fork()?
Both fd's refer to the same stream so both have the module pushed on it.
A file descriptor refers to a kernel file structure that points to the
stream.  When you dup the descriptor you inherit the same stream.
It is the same principle that when you dup a tty descriptor the new one
refers to the same device so later ioctl calls have an effect on both.

guy@auspex.auspex.com (Guy Harris) (05/16/89)

 >What if:
 >	1. I open a STREAM.
 >	2. I dup that stream, so now I have 2 fd's referring to
 >		that stream.
 >	3. I push a module on one of the fd's.
 >
 >Questions:
 >	1. Do both of those fd's have that module pushed on it?

Yes.  On the UNIX systems with which I'm familiar (all ultimately
AT&T-derived, e.g. V7, S3, S5, BSD, etc.):

	a "file descriptor" is a small integer that is used as an index
	into an array of pointers to "file table entries" in the kernel;

	a "file table entry" refers to an inode (or vnode, or whatever)

On those systems with streams with which I'm familiar (S5R3, and
SunOS 4.0 which uses streams code derived from S5R3), the (i|v)node
points to the stream head data structure.

When you do a "dup", those systems find an unused file descriptor and
make it point to the same file table entry as the one from which the
"dup" is being done.  This means that both file descriptors share
everything in the file table entry, and everything to which the file
table entry points; this includes the (i|v)node, and hence the stream.

>	2. What happens during a fork()?

In the aforementioned systems, when a "fork" is done, the new process
gets its own array of pointers to file table entries; this table starts
out containing the exact same set of pointers that the one in the old
process contains.  Thus, file descriptor N in the child refers to the
same file table entry as file descriptor N in the parent, and they thus
share everything in the file table entry, and everything to which it
points, including the stream.