LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2005, 02:52 AM   #1
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
connections used by different processes?


hiho@ll

if i do
int client=accept(...);
i get a connection to a client (tcp)
since client is int it's just a number, which identifies the connection

question:
if i share the number of the connection through some processes (they have nothing to do with each other, they all just get the number) is it possible
that many different processes use this connection at the same time?

i think it should because afaik the connections are handled by the kernel and i hope they are not associated with the adress space of the process who uses them, right?

thx@ll
 
Old 09-13-2005, 11:20 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Wrong - file descriptors are strictly per-process entities.

You're correct: the underlying TCP/IP connection (or open file, or IPC, or whatever) are indeed managed by the kernel. But the file descriptor the kernel hands to the process absolutely has no validity outside of that process.

'Hope that helps .. PSM
 
Old 09-13-2005, 12:52 PM   #3
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
WOW

that's a problem for my concept now

well, to be sure that i understand:

i do this:

1. have a TCP server (make accept(...)
2. using accept i get a number
3. system("./testprog connection_number_i_got_from_accept"); // call another prog, and use arguments to send the connection number
4. testprog reads it's command line arguments
5. testprog uses functions read(); or write(); to read/write using the connection number


the above example doesn't work because the connection can't be shared through many processes like this way, because the number is depending on the process mapped by the kernel??
right?

if this is right, ;-( ;-( do you have an alternative way/idea how i can share a connection through many programs?
 
Old 09-13-2005, 01:38 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
1. Yes, that's absolutely correct.

2. Yes, there is an alternative.

On Linux and Unix, you can use something called "Unix Sockets" to pass the descriptor from one process to another. There's a good discussion of this in Stevens:

W. Richard Stevens, Unix Network Programming, Vol 1

It's an "advanced technique". You can accomplish the same thing in Windows with "DuplicateHandle()" (all versions) and WSADuplicateSocket()" (Winsock2):

http://tangentsoft.net/wskfaq/articl...g-sockets.html

'Hope that helps .. PSM

PS:
I did some googling, but I wasn't able to find a good reference for using Linux. Some keywords I tried include "passing file descriptors between processes", "unix sockets" "domain sockets", "recvmsg" and "sendmsg". In my copious spare time, I really ought to write a sample app for Linux and post it... Oh well

Hopefully I've given you enough to go with. And please do consider buying a copy of Stevens, if you don't already have one...

PPS:
Here are a few other good Network Programming resources:
* Socket FAQs:
- http://www.sockets.com/
- http://beej.us/guide/bgnet/output/html/index.html
- http://www.developerweb.net/sock-faq/

Last edited by paulsm4; 09-13-2005 at 01:51 PM.
 
Old 09-14-2005, 02:46 AM   #5
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
using the links you posted i found this solution:

2) Assuming the clients and the "router" are in the same machine, and that you don't want to implement the 1st point:
The clients connects to the "router" using UNIX domain sockets, asks for the service, the "router" creates a connection to the server and forwards the file descriptor to the client using "sendmsg()" (See the man page of sendmsg()), so the client will have an open connection to the server.
- As the first point, the server won't be altered. The communication between client and "router" will be a little bit different, but the advantage is the client won't have to setup a new connection to the server, just use the file descriptor given by the "router".

i'm not sure if i'm right, but this example is the same problem we already discussed, not?
if i send the file descriptor (i think it's the "number" i told above) to another process using unix domain socket
don't i have the same problem as already discussed? because the "number" is (by the kernel) associated with a process and now process B want's to use the same descriptor created by process A
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
2000 MySQL connections, still "too many connections" newlinuxnewbie Linux - General 0 11-07-2005 01:03 PM
What are these processes doing? Dark Jedi Linux - Newbie 1 12-15-2004 07:44 AM
monitoring active processes and identifying the required processes. gajaykrishnan Programming 2 08-13-2004 01:58 AM
Too many processes? mooreted Linux - Software 2 04-05-2004 03:27 PM
processes nautilus_1987 Linux - Software 11 09-28-2002 06:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration