睡到自然醒454 2023-11-29 19:09 采纳率: 100%
浏览 17
已结题

Linux操作系统中的,管道通信问题


#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
int main(void) {
    printf("%d进程:创建管道...\n", getpid());
    if (mkfifo("myfifo", 0666) == -1) {
        perror("mkfifo");
        return -1;
    }
    printf("%d进程:打开管道...\n", getpid());
    int fd = open("myfifo", O_WRONLY);
    if (fd == -1) {
        perror("open");
        return -1;
    }
    printf("%d进程:发送数据...\n", getpid());
    char buf[1024];
    for (;;) {
        printf("> ");
        fgets(buf, sizeof(buf) / sizeof(buf[0]), stdin);
        if (!strcmp(buf, "!\n"))
            break;
        if (write(fd, buf, strlen(buf) * sizeof(buf[0])) == -1) {
            perror("write");
            return -1;
        }
    }
    printf("%d进程:关闭管道...\n", getpid());
    if (close(fd) == -1) {
        perror("close");
        return -1;
    }
    printf("%d进程:删除管道...\n", getpid());
    if (unlink("myfifo") == -1) {
        perror("unlink");
        return -1;
    }
    printf("%d进程:完成任务!\n", getpid());
    return 0;
}

这个代码是不是输入!和回车跳出循环,但是现在一直卡在输入界面

img

  • 写回答

8条回答

  • curating 2023-11-29 22:30
    关注

    1、代码有错误,具体如下。 2、运行测试方式不对,正确步骤如下:
    1、代码问题具体如下:
    第24行的的第一个!为全角字符,第二个为英文!。

            if (!strcmp(buf, "!\n");
    
     编译会报错:
    
    test.c: In function ‘main’:
    test.c:24:13: error: stray ‘\357’ in program
             if (▒▒▒strcmp(buf, "!\n"))
                 ^
    test.c:24:14: error: stray ‘\274’ in program
             if (▒▒▒strcmp(buf, "!\n"))
                  ^
    test.c:24:15: error: stray ‘\201’ in program
             if (▒▒strcmp(buf, "!\n"))
    

    修改为英文!后,编译通过。

    [root@kylin101 dm8]# gcc test1.c
    [root@kylin101 dm8]# ls
    a.out  arch  backup  data  dmdbms  test1.c  test.c  tmp
    

    2、运行测试方式不对,正确步骤如下:
    1、执行a.out后,程序会立即创建myfifo文件,程序的提示为下面(不要输入任何内容):

    [root@kylin101 dm8]# ./a.out
    17329进程:创建管道...
    17329进程:打开管道...
    17329进程:发送数据...
    
    

    2、开新的窗口,执行 tail -f myfifo,模拟接受管道内容。
    可看到程序窗口的提示增加一个>,表示可以输入内容了。

    [root@kylin101 dm8]# ./a.out
    17329进程:创建管道...
    17329进程:打开管道...
    17329进程:发送数据...
    >
    
    

    3、程序窗口输入任意内容,最后输入一个英文!。

    [root@kylin101 dm8]# ./a.out
    17329进程:创建管道...
    17329进程:打开管道...
    17329进程:发送数据...
    >
    > asdf
    > 中华人国
    > 123
    > !
    17329进程:关闭管道...
    17329进程:删除管道...
    17329进程:完成任务!
    
    

    程序端结束。
    tail -f 窗口可以看到接收的内容。

    [root@kylin101 dm8]# tail -f myfifo
    
    asdf
    中华国
    123
    tail: cannot watch 'myfifo': No such file or directory
    tail: inotify cannot be used, reverting to polling
    
    

    最后的两行提示为myfifo文件不存在了,tail -f 异常。 使用crtl+c结束即可。

    ^C
    [root@kylin101 dm8]# ls
    a.out  arch  backup  data  dmdbms  test1.c  test.c  tmp
    [root@kylin101 dm8]#
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(7条)

报告相同问题?

问题事件

  • 系统已结题 12月8日
  • 已采纳回答 11月30日
  • 创建了问题 11月29日

悬赏问题

  • ¥20 python爬虫遇到空文本取不出来
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络
  • ¥66 关于川崎机器人调速问题
  • ¥15 winFrom界面无法打开
  • ¥30 crossover21 ARM64版本安装软件问题
  • ¥15 mymetaobjecthandler没有进入