Designing using fork() and TCP connection in C

2019-08-14 07:33发布

问题:

I have a question regarding on how to design the following system:

My system is built of several clients listening to an environment. When a audio threshold is breached they send their information to a server, that has children listening on each connection. The server needs information from all the clients to make the necessary calculations.

Currently the server is working in UNIX and has forked out connections. They are working independently.

What I want to do is to tell the parent (in the server) that information has been sent and it's now time to process it. How should I do it?

I'm thinking of possible different ways to do it:

  1. Using signal()in Unix to somehow tell the parent that something has happened
  2. Convert to Threads and use some wait and notify functions

The signaling is preferable but I cannot figure out how to do it efficiently. Because the following can happen in my system:

  • If all the clients successfully sent information to their children of the server, how can I tell the parent that I'm ready in a efficient way? Don't know/I'm uncertain of how it will process them.
  • The server may not receive information from all clients. So the parent must wait for awhile for all the children but not too long. So I'm guessing some sort of timer?

回答1:

Doen't use fork, and don't use signals. Use a thread pool.



回答2:

What about a Unix Domain Socket for an inter-processes communication between children and father?

http://en.wikipedia.org/wiki/Unix_domain_socket

As soon as a child receives data through the TCP connection, the same data will be forwarded to the father process through the Unix Domain Socket and the latter process will be instantly notified



标签: c sockets tcp fork