Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

bidirectional IPC (Linux)

by Mirage (Sexton)
on Nov 07, 2001 at 03:16 UTC ( [id://123724]=perlquestion: print w/replies, xml ) Need Help??

Mirage has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,

I'm(still..)searching for a method of communicating between forked processes in both directions, I tried named pipes, pipes, Unix Domain Sockets but so far I have found nothing which is really useful in my case. I have a server which forks everytime a user connects, then the data the child gets from the client has to get back to the parent, but as every child does this, the connection has to be between one end and many ends, not as domain sockets are between one and one.

also i want to be able to read from it just for a short time, then do other things and read again. Or maybe someone can explain me how this 'often used' type of servers is done, with multiple clients which send and receive data from the server, at the same time as other clients of course, like a chat with tcp

(I dont want to do a chat nor do i want to use udp)

So my question is: Which method is to be preferred, and how would a chat server with tcp be done?

Thanks in advance.

Mirage

2001-11-06 Edit by Corion : Removed superfluous blank lines

Edit: chipmunk 2001-11-06

Replies are listed 'Best First'.
Re: bidirectional IPC (Linux)
by traveler (Parson) on Nov 07, 2001 at 03:37 UTC
    From your description, it sounds as though pipes would be fine. You can read them when you want and you can have many pipes: one to each child. One parent can talk to each child with its own pipe. Why do you think "but as every child does this, the connection has to be between one end and many ends"? Do you need to send one message to all the clients at once? Are you trying to send a message from the parent and let a single client pick it up?

    I'm not sure exactly what you need to do, but you can thread the server (that's what a lot of chat servers do) or you can use select(the four argument one) to decide when you need to read from a child.

    If you give us some more information, or maybe an example of what you've tried, we might be able to be of more help.

    HTH, --traveler

      >...can read them when you want and you can have many pipes: one to each child.

      but I have to have two pipes to each child, don't I?

      >Do you need to send one message to all the clients at once?

      thats right, this wont work...

      >I'm not sure exactly what you need to do, but you can thread the server (that's what a lot of chat servers do)
      >or you can use select(the four argument one) to decide when you need to read from a child.

      hmm threads will not be present in their actual form
      in future releases of perl, as I have extracted from man
      thread, so i think thats not the best way to do it.
      Select is then a method without fork, but I haven't
      understood it completely, what does the Select do, it
      determines from which socket 'data comes in', and then
      reads it?

      Thanks
        but I have to have two pipes to each child, don't I?

        Yes. If you want to follow the rules and if you are sending data bidirectionally. Or you can use a socket. Sockets are bidirectional and work very much like pipes once established.

        thats right, this wont work...

        I'm not sure what you mean by this.

        Select is then a method without fork, but I haven't understood it completely, what does the Select do, it determines from which socket 'data comes in', and then reads it?

        No. select tells you when a filehandle is ready for reading or writing or when it has an error condition. You need to do the I/O yourself. It can be used with or without fork.

        While the implementation of threads, and maybe the syntax may change, using threads may still be your best bet.

        HTH, --traveler

Re: bidirectional IPC (Linux)
by Zaxo (Archbishop) on Nov 07, 2001 at 06:39 UTC

    From man IO::Select:

    Here is a short example which shows how `IO::Select' could be used to write a server which communicates with several sockets while also listening for more connections on a listen socket
    use IO::Select; use IO::Socket; $lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080) +; $sel = new IO::Select( $lsn ); while(@ready = $sel->can_read) { foreach $fh (@ready) { if($fh == $lsn) { # Create a new socket $new = $lsn->accept; $sel->add($new); } else { # Process socket # Maybe we have finished with the socket $sel->remove($fh); $fh->close; } } }

    After Compline,
    Zaxo

Re: bidirectional IPC (Linux)
by Rex(Wrecks) (Curate) on Nov 07, 2001 at 05:13 UTC
    If you don't like pipes there are other solutions, one is shared memory. tie a hash into shared memory using IPC::Shareable and used that.

    Also looking into Network Programming with Perl would be a good idea. It's a really good book and has several good examples of TCP Servers including a chat server.

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
Re: bidirectional IPC (Linux)
by jepri (Parson) on Nov 07, 2001 at 14:49 UTC
    I'm pumping my own work here, but I wrote a module called ForkMe to do exactly this, with a nice OO interface. I use it heavily for my own forking code.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

      thanks, I will look it up later and reply or msg if I have problems(hopefully this is what I was looking for)
      After looking at your module i think its of no real use
      for me, nor for any other real server application, or
      forking application, because having one one port for each
      process is too much, and why don't you use unix-domain-sockets?
        Couple of responses:

        • I was under the impression that every network socket has an associated domain socket (not that I ever checked).
        • You said you had already tried UDS
        • Exactly how many processes were you planning to use? There are 64000 possible sockets which means that you can have 32000 forked processes before you run out of sockets.
        • If you are trying to write code for a machine that can handle 32000 processes, grab the Apache source code since that is the canonical example of a really good forking server. And then give me an account on your machine :)
        • If you read the pod, my goal was portable reliable forking IPC.
        • I am currently reworking the architecture so that you can drop in any transport layer that you want to use - I have a demo using SysV message queues, and I can't wait to hear your comments on that one.
        • The basic architecture of my code is useful for a preforking server, regardless of the transport layer. For someone who doesn't even know what select does, you seem to be pretty good at pointing out why code you don't understand is unsuitable for your purpose.

        ____________________
        Jeremy
        I didn't believe in evil until I dated it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://123724]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 14:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found