Can I share fd between processes?

I have one processA in charge of listening the socket and get the fd,I need to using the fd in process B to send data,Is it possible?yes,i know it is easy to do in one process by two thread,but i need to do it in two process.

Actually i think it is possible to share the fd (since fd´s are managed by OS), but you have to check if your fd is valid in both process.

Edith says:
there is an sopen() for opening for shared access

Edith also knows of:
another function sopenfd()

how to use the sopen() between two process?can you give me one example?thank you

Errr,
Process A Pseudo Code:

shm_open(YourSharedMem);
map_your_shm_to_Address();
fd=sopen_your_file();
write_your_fd_to_Address();

Process B Pseudo Code:
shm_open(YourSharedMem);
map…
read_your_fd_from_Address();
sopenfd(yourfd…);

But i think its much easier if you just open the file/socket on both processes :stuck_out_tongue_winking_eye:
Process A+B:

Create_your_socket();
fork_your_process();
if(pid==0){
Open_your_Socket_reading();
Do_some_stuff();
}else{
Open_your_socket_writing();
Do_some_stuff();
}

If fork is out of option:
return to the shm-model for fd retrival or send some messages or create a named pipe.

There are a lot of examples in the doce at fork() shm_open() pipe() etc.

But actually i still wonder why you need to read and write from two different processes?

sorry i can understand what i should change.Actually i have get the fd from processB using sharemem,I can see the fd is 16 in process A,but when I use send(fd,…) in process B(fd=16),it tells me the bad file descriptor.

Send assumes the first argument to be a socket, created by socket().
write(fd,…) should be for sopen/sopenfd…

What are you trying to accomplish?
Are you trying to communicate between this two processes via the socket/fd?
Are you trying to use a socket as AF_INET socket with 2 processes for reading writing data through your network?
Are you trying to …put anything in here…?

Maybe I’m missing something, but the discussion so far seems to have missed the obvious. Here’s my recollection of how things work.

  1. Can I share an fd between processes
    Yes!
    You can fork process A to get process B and it will have all the same fd’s.
    If B is a different module, you can fork and then exec and still have the fd’s around, although I think there are some controls that govern this, close some fd’s, not others, etc.
    I don’t think openfd() has any use here. To use openfd() I think you already need the same fd in both processes, although I can’t remember or lookup right now the difference between openfd(), dup(), and dup2().

  2. Can I share an fd by putting it in shared memory.
    NO! This makes no sense. An fd is a small integer that is meaningful in a thread only if the OS associates it with some object, File, Device, Object. In QNX the generic association is with some resource manager.

  3. Does sopen() or sopenfd() have any relevance here.
    NO! Shared opens have to do with file access where you don’t want separate threads stepping on each other’s toes while updating files. Remember those statements that go something like this: “If two threads are reading the same fd at the same time, the results are unpredictable”. Well what if you want things to be predictable? Take a look at sopen().

You cannot just tell another process that you have a fd=16 and expect the other process to be able to recv()/send() from/to it. There are some prerequisities to that. But if you can use threads then you can tell the other thread that it can use fd=16 for recv()/send(). Threads share file descriptors by definition. Processes share them only under specific conditions.

For example you can do this :

  • connect(fd) or fd=accept()
  • fork()
  • now you have 2 processes (parent and child)
  • now you can use one of them to read(fd)
  • and the other one to write(fd)

see en.wikipedia.org/wiki/Pipeline_%28Unix%29

thinks a lot

Sorry, i think i read a bit wrong on sopen and sopenfd :stuck_out_tongue_winking_eye:
But i thought FD´s are put on OS-lvl, this is also if you have fd0 == 2, the next fd even in another process would be fd == 3

Each process have the same fd-s numbers

now I change to share fd between threads,it seems work well,btw my process works as server,one thread listens and gets socket fd by using accept,another thread sends data when the fd!=-1(btw the initial value of fd is -1),the client side can receive data correctly,but when I close connection on client side,the process exits abnormally,I have checked and found the reason is the send() function,when the connection is closed it doesn’t return anything and causes the process exit.why?

Xuyong,

Your getting a SIGPIPE error.

Look at the send documentation in the helpview for more info.

The easiest thing to do is ignore SIGPIPE errors and then check the return code of send and then check errno to see if you got EPIPE which means the connection is closed.

Tim

think you