如何使用管道在两个程序之间发送一个简单的字符串?

106 c unix pipe

我试着在网上搜索,但几乎没有任何资源.一个小例子就足够了.

编辑我的意思是,两个不同的C程序相互通信.一个程序应发送"Hi",另一个程序应接收它.这样的事情.

jsc*_*ier 153

常规管道只能连接两个相关进程.它由一个进程创建,并在最后一个进程关闭时消失.

命名管道,还要求它的行为一个FIFO,可以用于连接两个不相关的进程和独立的处理的存在; 这意味着即使没有人使用它也可以存在.使用mkfifo()库函数创建FIFO .

writer.c

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    int fd;
    char * myfifo = "/tmp/myfifo";

    /* create the FIFO (named pipe) */
    mkfifo(myfifo, 0666);

    /* write "Hi" to the FIFO */
    fd = open(myfifo, O_WRONLY);
    write(fd, "Hi", sizeof("Hi"));
    close(fd);

    /* remove the FIFO */
    unlink(myfifo);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

reader.c

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

#define MAX_BUF 1024

int main()
{
    int fd;
    char * myfifo = "/tmp/myfifo";
    char buf[MAX_BUF];

    /* open, read, and display the message from the FIFO */
    fd = open(myfifo, O_RDONLY);
    read(fd, buf, MAX_BUF);
    printf("Received: %s\n", buf);
    close(fd);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

注意:为简单起见,上述代码中省略了错误检查.

  • 可能是通过一个或多个父/子关系(例如包括兄弟姐妹)相关的过程.共同的祖先会创造管道的两端.不相关的过程缺乏共同的祖先. (7认同)
  • 什么是*相关流程*? (6认同)
  • 如果读者首先开始,这将不起作用.快速解决方法是将读者的`open()`放在循环中.但是+1因为你提供了两个程序的例子. (4认同)

Ste*_*hen 41

C中的创建管道,这将向您展示如何分叉程序以使用管道.如果你不想fork(),你可以使用命名管道.

此外,你可以得到的效果prog1 | prog2通过发送输出prog1到标准输出,并从阅读stdinprog2.您还可以通过打开一个名为/dev/stdin(但不确定其可移植性)的文件来读取标准输入.

/*****************************************************************************
 Excerpt from "Linux Programmer's Guide - Chapter 6"
 (C)opyright 1994-1995, Scott Burkett
 ***************************************************************************** 
 MODULE: pipe.c
 *****************************************************************************/

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

int main(void)
{
        int     fd[2], nbytes;
        pid_t   childpid;
        char    string[] = "Hello, world!\n";
        char    readbuffer[80];

        pipe(fd);

        if((childpid = fork()) == -1)
        {
                perror("fork");
                exit(1);
        }

        if(childpid == 0)
        {
                /* Child process closes up input side of pipe */
                close(fd[0]);

                /* Send "string" through the output side of pipe */
                write(fd[1], string, (strlen(string)+1));
                exit(0);
        }
        else
        {
                /* Parent process closes up output side of pipe */
                close(fd[1]);

                /* Read in a string from the pipe */
                nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
                printf("Received string: %s", readbuffer);
        }

        return(0);
}
Run Code Online (Sandbox Code Playgroud)


小智 8

dup2( STDIN_FILENO, newfd )
Run Code Online (Sandbox Code Playgroud)

并阅读:

char reading[ 1025 ];
int fdin = 0, r_control;
if( dup2( STDIN_FILENO, fdin ) < 0 ){
    perror( "dup2(  )" );
    exit( errno );
}
memset( reading, '\0', 1025 );
while( ( r_control = read( fdin, reading, 1024 ) ) > 0 ){
    printf( "<%s>", reading );
    memset( reading, '\0', 1025 );
}
if( r_control < 0 )
    perror( "read(  )" );    
close( fdin );    
Run Code Online (Sandbox Code Playgroud)

但是,我认为这fcntl可能是一个更好的解决方案

echo "salut" | code
Run Code Online (Sandbox Code Playgroud)


Joh*_*han 6

一个程序写入stdout的内容可以由另一个程序通过stdin读取.所以简单地说,使用c,写入prog1使用打印内容printf()prog2使用读取内容scanf().然后跑吧

./prog1 | ./prog2
Run Code Online (Sandbox Code Playgroud)