4

I was going through nanomsg usage for IPC and wanted to use SURVEY-archetype architecture described here.
In this, processes run as client and server and exchange data. Now server has following code (also listed in the provided link):

int server (const char *url)
{
  int sock = nn_socket (AF_SP, NN_SURVEYOR);
  assert (sock >= 0);
  assert (nn_bind (sock, url) >= 0);
  sleep(1);                                              // wait for connections
  int sz_d = strlen(DATE) + 1;                           // '\0' too
  printf ("SERVER: SENDING DATE SURVEY REQUEST\n");
  int bytes = nn_send (sock, DATE, sz_d, 0);
  assert (bytes == sz_d);
  while (1)
    {
      char *buf = NULL;
      int bytes = nn_recv (sock, &buf, NN_MSG, 0);
      if (bytes == ETIMEDOUT) break;
      if (bytes >= 0)
      {
        printf ("SERVER: RECEIVED \"%s\" SURVEY RESPONSE\n", buf);
        nn_freemsg (buf);
      }
    }
  return nn_shutdown (sock, 0);
}

Since socket type is NN_SURVEYOR, the while-loop doesn't wait at nn_recv. For client, socket type is NN_RESPONDENT and so, while loop in client waits at nn_recv.

Now since this while loop runs continuously and infinitely, the CPU usage shoots up to 99%. Can you please tell me that is there any other way to make survey architecture using nanomsg.

sg7
  • 6,108
  • 2
  • 32
  • 40
Aman
  • 696
  • 1
  • 8
  • 26

1 Answers1

1

Try adding yield() call in the end of loop if nothing was received.

ivan.ukr
  • 2,853
  • 1
  • 23
  • 41