c++ - ZeroMQ share a context with all child processes in a forking server -


i'm writing forking chat server in c++, each incoming client being own process. server-client interactions done though normal sockets zeromq sockets handling message queuing , ipc. basic problem there when server forks accommodate new client, client's process has copy of context (which fork does, right?), when binds sockets context, none of other clients aware of socket. long story short: how each client thread have same context can talk each other through zeromq?

i've looked @ various ways share context between processes, , far i'm found this one. problem 1) uses thread pool, , understand what's written there, 5 threads created; server needs support @ least 256 , have @ least many threads, , 2) uses zeromq both talking clients , backend tasks; i'm limited using zeromq backend only.

i've looked @ zeromq mailing list , 1 message said fork() orthogonal how zeromq works. mean can't share context across forked child processes? if that's case, how share context across multiple processes while keeping in mind requirement of supporting @ least 256 clients , using zeromq backend?

edit: cleared thread/process confusion. sorry that.

edit2: reason why i'm favoring forking on threads i'm used having main process accepts incoming socket connection forks, giving new socket child. i'm not sure how in threading fashion (not well-practiced, not totally out of league)

edit3: so, starting rewrite threads. guess way?

edit4: further clarification, incoming connections server can either tcp or udp , have handle type when client connects, can't use zeromq socket listen in.

context sharing

the reason share zmq context in example code link is, server(main()) uses inproc socket communicate workers(worker_routine()). inproc sockets cannot communicate each other unless created same zmq context, settle in same process. in case, think it's not necessary share since no inproc sockets supposed used. so, code might like:

void *worker_routine (void *arg) {     // zmq::context_t *context = (zmq::context_t *) arg;    // it's not necessary now.     zmq::context_t context(1);    // it's fine create new context      zmq::socket_t socket (context, zmq_rep);     // socket.connect ("inproc://workers");    // inproc socket useless here.     socket.connect("ipc:///tmp/workers");    // need sockets can cross process.      // handling code omitted. }  int main () {     //  omitted...      // workers.bind ("inproc://workers");    // inproc socket useless here.     workers.bind("ipc:///tmp/workers");      //  launch pool of worker processes     (int = 0; < 5; ++i) {         if (fork() == 0) {             // worker process runs here             worker_routine(null);             return 0;         }     }     //  connect work processes client process via queue     zmq::proxy (clients, workers, null);     return 0; } 

handling process per request

and talk requirement, 1 process per request. last example code intended illustrate usage of zmq::proxy, provided simplify server code router-dealer pattern. can't fulfill requirement. so, have implement manually. looks another example. difference need invoke fork() when frontend socket readable , put while loop sub process.

if (items[0].revents & zmq_pollin) {     if (fork() == 0) {         // sub process runs here         while (1) {             // forward frames here         }         // sub process ends here         return 0;     } } 

suggestion

at end, have say, it's heavy create process 1 request unless scenario special. please use thread, or consider asynchronous io zmq::poll.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -