title: C Program daemon uses 100% cpu usage
body: <p>I'm initializing a daemon in C in a Debian:</p>
  
  <pre><code>/**
   * Initializes the daemon so that mcu.serial would listen in the background
   */
  void init_daemon()
  {
      pid_t process_id = 0;
      pid_t sid = 0;
  
      // Create child process
      process_id = fork();
  
      // Indication of fork() failure
      if (process_id &lt; 0) {
          printf("Fork failed!\n");
          logger("Fork failed", LOG_LEVEL_ERROR);
          exit(1);
      }
  
      // PARENT PROCESS. Need to kill it.
      if (process_id &gt; 0) {
          printf("process_id of child process %i\n", process_id);
          exit(0);
      }
  
      //unmask the file mode
      umask(0);
      //set new session
      sid = setsid();
  
      if(sid &lt; 0) {
          printf("could not set new session");
          logger("could not set new session", LOG_LEVEL_ERROR);
          exit(1);
      }
  
      // Close stdin. stdout and stderr
      close(STDIN_FILENO);
      close(STDOUT_FILENO);
      close(STDERR_FILENO);
  }
  </code></pre>
  
  <p>The main daemon runs in the background and monitors a serial port to communicate with a microcontroller - it reads peripherals (such as button presses) and passes information to it. The main functional loop is</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      // We need the port to listen to commands writing
      if (argc &lt; 2) {
          fprintf(stderr,"ERROR, no port provided\n");
          logger("ERROR, no port provided", LOG_LEVEL_ERROR);
          exit(1);
      }
      int portno = atoi(argv[1]);
  
      // Initialize serial port
      init_serial();
  
      // Initialize server for listening to socket
      init_server(portno);
  
      // Initialize daemon and run the process in the background
      init_daemon();
  
      // Timeout for reading socket
      fd_set setSerial, setSocket;
      struct timeval timeout;
      timeout.tv_sec = 0;
      timeout.tv_usec = 10000;
  
      char bufferWrite[BUFFER_WRITE_SIZE];
      char bufferRead[BUFFER_READ_SIZE];
      int n;
      int sleep;
      int newsockfd;
      while (1)
      {
          // Reset parameters
          bzero(bufferWrite, BUFFER_WRITE_SIZE);
          bzero(bufferRead, BUFFER_WRITE_SIZE);
          FD_ZERO(&amp;setSerial);
          FD_SET(fserial, &amp;setSerial);
          FD_ZERO(&amp;setSocket);
          FD_SET(sockfd, &amp;setSocket);
  
          // Start listening to socket for commands
          listen(sockfd,5);
          clilen = sizeof(cli_addr);
          // Wait for command but timeout
          n = select(sockfd + 1, &amp;setSocket, NULL, NULL, &amp;timeout);
          if (n == -1) {
              // Error. Handled below
          } 
  
          // This is for READING button 
          else if (n == 0) {
              // This timeout is okay 
              // This allows us to read the button press as well
  
              // Now read the response, but timeout if nothing returned
              n = select(fserial + 1, &amp;setSerial, NULL, NULL, &amp;timeout);
              if (n == -1) {
                  // Error. Handled below
              } else if (n == 0) {
                  // timeout
                  // This is an okay tiemout; i.e. nothing has happened
              } else {
                  n = read(fserial, bufferRead, sizeof bufferRead);
                  if (n &gt; 0) {
                      logger(bufferRead, LOG_LEVEL_INFO);
                      if (strcmp(stripNewLine(bufferRead), "ev b2") == 0) {
                          //logger("Shutting down now", LOG_LEVEL_INFO);
                          system("shutdown -h now");
                      }
                  } else {
                      logger("Could not read button press", LOG_LEVEL_WARN);
                  }
              }
          } 
  
          // This is for WRITING COMMANDS
          else {
              // Now read the command
              newsockfd = accept(sockfd, (struct sockaddr *) &amp;cli_addr, &amp;clilen);
  
              if (newsockfd &lt; 0 || n &lt; 0) logger("Could not accept socket port", LOG_LEVEL_ERROR);
  
              // Now read the command
              n = read(newsockfd, bufferWrite, BUFFER_WRITE_SIZE);
              if (n &lt; 0) {
                  logger("Could not read command from socket port", LOG_LEVEL_ERROR);
              } else {
                  //logger(bufferWrite, LOG_LEVEL_INFO);
              }
  
              // Write the command to the serial
              write(fserial, bufferWrite, strlen(bufferWrite));
              sleep = 200 * strlen(bufferWrite) - timeout.tv_usec;  // Sleep 200uS/byte
              if (sleep &gt; 0) usleep(sleep);
  
              // Now read the response, but timeout if nothing returned
              n = select(fserial + 1, &amp;setSerial, NULL, NULL, &amp;timeout);
              if (n == -1) {
                  // Error. Handled below
              } else if (n == 0) {
                  // timeout
                  sprintf(bufferRead, "err\r\n");
                  logger("Did not receive response from MCU", LOG_LEVEL_WARN);
              } else {
                  n = read(fserial, bufferRead, sizeof bufferRead);
              }
              // Error reading from the socket
              if (n &lt; 0) {
                  logger("Could not read response from serial port", LOG_LEVEL_ERROR);
              } else {
                  //logger(bufferRead, LOG_LEVEL_INFO);
              }
  
              // Send MCU response to client
              n = write(newsockfd, bufferRead, strlen(bufferRead));
              if (n &lt; 0) logger("Could not write confirmation to socket port", LOG_LEVEL_ERROR);
          }
  
          close(newsockfd);
      }
  
      close(sockfd);
      return 0;
  }
  </code></pre>
  
  <p>But the CPU usages is always at 100%. Why is that? What can I do?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I commented out the entire while loop and made the main function as simple as:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      init_daemon();
      while(1) {
          // All commented out
      }
      return 0;
  }
  </code></pre>
  
  <p>And I'm still getting 100% cpu usage</p>