首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C程序守护进程使用100%的cpu使用率。

问C程序守护进程使用100%的cpu使用率。
EN

Stack Overflow用户
提问于 2015-07-03 23:49:52
回答 1查看 2.5K关注 0票数 3

我在Debian中用C初始化一个守护进程:

代码语言:javascript
复制
/**
 * 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 < 0) {
        printf("Fork failed!\n");
        logger("Fork failed", LOG_LEVEL_ERROR);
        exit(1);
    }

    // PARENT PROCESS. Need to kill it.
    if (process_id > 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 < 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);
}

主守护进程在后台运行,并监视与微控制器通信的串行端口--它读取外围设备(例如按下按钮)并将信息传递给它。主要功能回路是

代码语言:javascript
复制
int main(int argc, char *argv[])
{
    // We need the port to listen to commands writing
    if (argc < 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(&setSerial);
        FD_SET(fserial, &setSerial);
        FD_ZERO(&setSocket);
        FD_SET(sockfd, &setSocket);

        // Start listening to socket for commands
        listen(sockfd,5);
        clilen = sizeof(cli_addr);
        // Wait for command but timeout
        n = select(sockfd + 1, &setSocket, NULL, NULL, &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, &setSerial, NULL, NULL, &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 > 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 *) &cli_addr, &clilen);

            if (newsockfd < 0 || n < 0) logger("Could not accept socket port", LOG_LEVEL_ERROR);

            // Now read the command
            n = read(newsockfd, bufferWrite, BUFFER_WRITE_SIZE);
            if (n < 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 > 0) usleep(sleep);

            // Now read the response, but timeout if nothing returned
            n = select(fserial + 1, &setSerial, NULL, NULL, &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 < 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 < 0) logger("Could not write confirmation to socket port", LOG_LEVEL_ERROR);
        }

        close(newsockfd);
    }

    close(sockfd);
    return 0;
}

但是CPU的使用率总是100%。为什么会这样呢?我能做什么?

编辑

我注释掉了整个while循环,并将主要功能简化为:

代码语言:javascript
复制
int main(int argc, char *argv[])
{
    init_daemon();
    while(1) {
        // All commented out
    }
    return 0;
}

我仍然得到了百分之百的cpu使用率

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-04 00:10:57

您需要在每次迭代中将timeout设置为想要的值,在Linux上修改结构,所以我认为除了第一次之外,您的循环不会暂停,也就是说,select()只是第一次阻塞。

尝试在tv_sec和tv_usec之后打印select(),然后查看,它被修改了,以反映select()返回前还有多少时间。

移动这部分

代码语言:javascript
复制
timeout.tv_sec = 0;
timeout.tv_usec = 10000;

在select()调用之前的循环中--它应该像您预期的那样工作--您也可以在循环中移动许多增量,这将使您的代码更易于维护,例如,您可以在将来将循环内容移动到一个函数中,这可能会有所帮助。

这是来自select(2)的

在Linux上,select()修改超时以反映没有睡眠的时间;大多数其他实现不这样做。(POSIX.1-2001允许两种行为。)当将读取超时的Linux代码移植到其他操作系统时,以及将代码移植到一个循环中重用多个struct timeval select()**s的的Linux 而不重新初始化它**时,都会产生问题。在select()返回之后,考虑将超时定义为未定义。

我认为这首歌中大胆的部分是很重要的。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31215422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档