fifo阅读有时会阻止

时间:2015-02-13 20:28:26

标签: c block ipc fifo

我正在写一篇IPC作业。在服务器端,我使用popen执行ls并将结果写入fifo以供客户端读取。我认为一切都很顺利,但有时如果我非常快地输入命令,客户端将被卡住。我不知道会发生这种情况。

客户端:

else if(memcmp(cmd, CMD_REMOTE_DIR, strlen(CMD_REMOTE_DIR)) == 0){
        if(write(client_writer_fifo_fd, cmd, strlen(cmd)) == -1){
            perror("write cmd ERROR");
            exit(EXIT_FAILURE);
        }
        for(;;){
            memset(read_buffer,0,sizeof(read_buffer));
            num_read = read(client_reader_fifo_fd, read_buffer,
                    sizeof(read_buffer));
            if(num_read < 0){
                perror("Read dir ERROR");
                exit(EXIT_FAILURE);
            }
            if(num_read == 0)
                break;
            //read_buffer[strlen(read_buffer)] = '\0';
            printf("%s",read_buffer);

        }
    }

服务器:

else if(memcmp(read_buffer, CMD_REMOTE_DIR,
                            strlen(CMD_REMOTE_DIR)) == 0){
                    fp = popen(CMD_LS_POPEN, "r");
                    if(fp == NULL){
                        perror("popen fail");
                        continue;
                    }
                    while(fgets(write_buffer,sizeof(write_buffer),fp)
                            != NULL){
                        if(write(client_reader_fifo_fd, write_buffer
                                    ,strlen(write_buffer)) == -1)
                            perror("write dir ERROR");
                    }
                    close(client_reader_fifo_fd);
                    pclose(fp);

                }

0 个答案:

没有答案