[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: help with socket programming[OFF TOPIC]



Quoting Shao Zhang (shao@cia.com.au):
> 
> 	while (1) {
> 		if ((NewHandle = accept(CGIHandle, (struct sockaddr *)&CGI_addr, \
> 		&sin_size)) == -1) {
> 			perror("cannot accept");
> 			continue;
> 		}
> 
>        		if (!fork() ) { /* this is the child */
> 			/* process the cgi request; */
> 
> 			close(NewHandle);
> 			exit(0);
> 		}
> 
> 		close(NewHandle); /* parent does not need this */
> 	}
> 
> 	My problem is I have to close the NewHandle for the parent process.
> 	But once I did this, when another request comes
> 	in, accept will allocate a new file descriptor with the same number it
> 	allocated before, since it is thinking the last one is closed.
> 	If the last child process still has not finished, then I will have two
> 	child process with the same descriptor, and then the child
> 	will return the request to the wrong location.

You've answered this yourself. "a new file descriptor with the same number".
So you have, for example, file descriptors p(arent)-n(umber) which becomes
p-n=c(hild)1-n at the fork. p-n is then closed, leaving c1-n. Now the second
request leads to p-n again, which becomes p-n=c2-n, and p-n gets closed.
At any one time, there exist: p-n=cc-n .. c2-n c1-n and they're all
unique except the first pair. n is the same in all cases, but they're in
different processes, and so distinguishable.

Cheers,

-- 
Email:  d.wright@open.ac.uk   Tel: +44 1908 653 739  Fax: +44 1908 655 151
Snail:  David Wright, Earth Science Dept., Milton Keynes, England, MK7 6AA
Disclaimer:   These addresses are only for reaching me, and do not signify
official stationery. Views expressed here are either my own or plagiarised.


Reply to: