Linux FIFO 文件和读/写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14173268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:36:13  来源:igfitidea点击:

FIFO files and read/write

clinuxfifo

提问by Kreweta

I've been trying to get some insight on FIFO and this low level I/O before my laboratories on monday and I came to this situation I don't quite understand.

我一直试图在周一的实验室之前对 FIFO 和这种低级 I/O 有所了解,但我遇到了这种情况,我不太明白。

The program should:

该计划应:

Server:

服务器:

  • creates FIFOs,
  • sends 5 messages: "Message #i", with interval of 5 sec,
  • deletes FIFOs,
  • 创建 FIFO,
  • 发送 5 条消息:“Message #i”,间隔为 5 秒,
  • 删除 FIFO,

Client:

客户:

  • reads from FIFO and displays the message,
  • terminates if there was no msg for 6 seconds,
  • 从 FIFO 读取并显示消息,
  • 如果 6 秒内没有 msg 则终止,

And it does communicate however, client is displaying not exactly what I did send him, and more importantly, seems to be reading from the beginning every time a new msg arrives. I've been trying to figure out, for quite a while, and it just doesn't seem to match what the documentation said... Please help! :(

并且它确实进行了通信,但是客户端显示的并不是我发送给他的内容,更重要的是,每次新消息到达时,它似乎都从头开始阅读。我一直试图弄清楚,很长一段时间,它似乎与文档所说的不符......请帮忙!:(

server

服务器

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>  
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

int main(int argc, char* argv[])
{       
    int s2c, c2s, i; 
    char fifo_name1[] = "/tmp/fifo1";
    char fifo_name2[] = "/tmp/fifo2";
    char msg[80], buf[10];
    struct stat st;

    // if no fifos, create 'em
    if (stat(fifo_name1, &st) != 0)
        mkfifo(fifo_name1, 0666);
    if (stat(fifo_name2, &st) != 0)
        mkfifo(fifo_name2, 0666);

    s2c= open(fifo_name1, O_WRONLY);
    c2s= open(fifo_name2, O_RDONLY);

    // start sending messages, with 5s interval
    for (i=0; i<5; i++)
    {
        printf("Message #%d \n", i);

        strcat(msg, "Message #"); 
        strcat(msg, itoa(i, buf, 10));
        strcat(msg, "
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
    int c2s, s2c, c=0;
    char buf[10];

    char fifo_name1[] = "/tmp/fifo1";
    char fifo_name2[] = "/tmp/fifo2";
    s2c= open(fifo_name1, O_RDONLY);
    c2s= open(fifo_name2, O_WRONLY);

    // receive messages
    while (1)
    {
        if (read(s2c, &buf, sizeof(char)*10) > 0)
        {
            printf("%s \n", buf);
            c=0;
        }
        sleep(1);
        c++;    
        if (c>6) 
            break;
    }

    printf("client exit successfully");
    return EXIT_SUCCESS;
}       
"); write(s2c, msg, strlen(msg)+1); sleep(5); } // delete fifos unlink(fifo_name1); unlink(fifo_name2); printf("server exit successfully"); return EXIT_SUCCESS; }

client

客户

##代码##

采纳答案by Anton Kovalenko

strcat(msg, "Message #");always appends to the end of the string already in msg, and the string is never reset during the loop. Replace it with strcpy(msg, "Message #");to start each new message from scratch.

strcat(msg, "Message #");总是附加到已经在 中的字符串的末尾msg,并且字符串在循环期间永远不会被重置。将其替换strcpy(msg, "Message #");为从头开始每条新消息。