It is only the pair (pid, fd) which is unique so the behavior you see is as expected.
Why not use inetd style servers? Then just handle data on stdin (and to stdout) letting
inetd fret about listen, accept and friends.
I wish this were the case but file id 10 is file id 10. This is verified by 2
facts. First child 2 becomes held by child 1, and secondly both clients get serviced
by child 1. To use your example of file 0,1,2, If you have 2 or more seperate apps
running on a single console that both write to stdout all output comes out on the
same console. However you have to remember that you have to specifically write to
those files if you simply do an fd=fopen(filename) the OS will never assign 0,1 or
2 to that file descriptor because fd 0,1,2 have non zero reference counts.
The fact of the matter is that when requesting a NEW descriptor the OS should not be
assigning a descriptor that has a reference count greater than 0.
Fred Allen <> FredAllen@fortna.com> > wrote:
File descriptors are supposed to be system wide. The os should not assign that
descriptor again until ALL references to it are closed. This code came directly
from sample code and works on every other flavor of unix I have ever used.
Why “file descriptors are system wide” ? Doesn’t any of your
program have “file descriptors” 0, 1, 2 opened for stdin, stdout,
File descriptors are NOT system wide, they are “process wide”.
so, fd 10 in first child, is different to the fd 10 in second
child, which is perfectly OK.
Ivan Bannon wrote:
I think you need to look at the logic of your code and at what you intend to
Since the parent closes the “sockalt” that id is now available to the parent
again, it has no knowledge in your code of the child using that id.
Try not closing the sockalt in the parent until you receive a death message
the child has died.
“Fred Allen” <> FredAllen@fortna.com> > wrote in message
news:> 3BE191AF.30F0F597@fortna.com> …
I am writing a tcp server and having a problem.
here is the basic code
after opening the socket and binding and starting a listener
// printf(“got a connection”);
sockalt=accept(sock,(struct sockaddr *)&myname,&mylen);
if((tid=fork()) == 0) // I am the child
close(sock); // child closed parrent socket
close(sockalt); //parent closes chile socket
The code works fine for the first connection. however the next time
accept returns it returns the same altsock id as the previous time.
first time sock=9
accept returns altsock=10
process forks and parent closes altsock (child should still have file
descriptor 10 open)
Accept returns altsock=10 again when a new connection is made now the 2
child processes are accessing the same file descriptor.
any help would be appreciated