Apologies if this question seems a tad open ended or too vague, I'm not the best C/Cpp programmer (either language is acceptable to solve this problem).
Suppose there are two running processes, a client and a server (they could be viewed as producer and consumer as well, but I think client and server might be better here). The server is a "child" (sort of, see below) process of the client such that it has been created to act somewhat as an offload. Over time the client generates several jobs which it then offloads to the server it created. Depending on the job, the server may or may not send back to the client information regarding job completion. As an aside, some might suggest that this could be done with threads. For reasons I won't get into, threads will not work here. The client and server should not be sharing memory (there may or may not be shared memory, its possible the client and server are two different machines: the code I'm writing should support both possibilities).
The server has a very long initialization period and thus must always be running, hence the idea of the second process being a server. It therefore must always be listening for any messages from the client. A simple pseudo-code/C example is given below
int main() {
...
client_pid = getpid();
pid = fork();
if(pid > 0) {
/* sets up connection with client, based on what connection type has been
* given (shared memory, sockets, etc). I don't know anything
* about what type of connection is established only that all
* communication is handled by the wait_For_Jobs and generate_Jobs functions
*/
start_Server(client_pid, connection_type);
wait_For_Jobs();
} else {
// gets information needed to send messages to server
contact_info = wait_for_connection();
generate_Jobs(contact_info);
}
}
This is a very, very rough outline of what I want. The question that I have is related to the "wait_For_Jobs" function. Unfortunately, the connection_type will not be known until runtime and thus this question might have several different answers depending on what type of communication method is used (ie shared memory, sockets, etc). For simplicity then, assume that shared memory is the communication type being used (say boost interprocess). With this in mind, what is the best and most efficient way for the server to wait for input from the client? One possible approach is to use a while loop somewhat in the fashion given below.
void wait_For_Jobs() {
while(true) {
if(check_If_Message_Received_Over_Shared_Memory){
// handle message
}
}
}
However, I suspect that this will be very inefficient; the process is always "spinning its wheels". Somewhat of a fix would be to put the process to "sleep" at the end of the while loop for a period of time. This isn't really too different from just running the code in the while loop (in fact its the same thing); it just lowers resource usage at the expense of response time. Ideally, the process should just be in somewhat of a standby mode and start computing once it receives a message. However, I'm not sure how you would do such a thing in C or Cpp. With that in mind, is there a better alternative?
If you use shared memory you will need to block on a semaphore which is raised when another request appears.
If you use sockets, use a blocking receive.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.