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

119

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

编辑 我的意思是,两个不同的C程序之间进行通信。一个程序应该发送“ Hi”,另一个程序应该接收它。就像这样的东西。


1
你可能不是指像 ls | grep ".o" 这样的东西吧?也许更详细地解释一下你的意思会有所帮助... - Jerry Coffin
14
加油啊,朋友。稍微努力一下吧。用谷歌搜索“c管道示例代码”。第一个结果是精确的:http://tldp.org/LDP/lpg/node11.html。 - Stephen
4
我希望两个完全不同的程序之间能够进行通信,但我找不到相关资源。 - user244333
1
如果您不想分叉一个进程,那么您需要查看“命名管道”。 - Judge Maygarden
8个回答

163

普通管道只能连接两个相关进程。它由一个进程创建,在最后一个进程关闭它时会消失。

命名管道(也称为FIFO)可以用于连接两个不相关的进程,并且独立于进程存在;这意味着即使没有人使用它,它也可以存在。FIFO是使用mkfifo()库函数创建的。

示例

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;
}

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;
}

注意:由于简化起见,上述代码中省略了错误检查。


6
什么被认为是“相关流程”? - Pithikos
7
可能是通过一个或多个父/子关系相关联的进程(例如包括兄弟姐妹)。共同的祖先将创建管道的两端。无关的进程缺乏这个共同的祖先。 - MSalters
6
如果读者先启动程序,那么这种方法将行不通。一个快速的解决办法是将读取器的 open() 放在一个循环内部。然而因为你提供了两个程序示例,所以加一分。 - gsamaras
是的,它需要针对Windows进行微调。 命名管道的维基百科文章讨论了一些Unix / Windows之间的差异,快速的谷歌搜索可以帮助实现Windows版本。 - jschmier
很抱歉 @Mohsin(好名字!),但我已经有一段时间没有碰这些了...我的意思是我几乎不记得我是如何回答这个问题的(https://dev59.com/s18d5IYBdhLWcg3wiSnI#26716300),更别提套接字了。祝你好运! - gsamaras
显示剩余5条评论

44

本文摘自 在C中创建管道,展示了如何fork一个程序来使用管道。如果不想使用fork(),可以使用命名管道

此外,您可以通过将prog1的输出发送到stdout并在prog2中从stdin读取以获得prog1 | prog2的效果。您还可以通过打开名为/dev/stdin的文件来读取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);
}

1
嗨 Stephen,我能用这段代码来完成两个不同的功能吗?也就是说,在一个函数中写入管道,在另一个函数中读取管道?如果有一个可行的代码,那就太好了。 - Mohsin
“Named pipes”链接现在重定向到https://www.oracle.com/java/technologies/。旧链接的存档:https://archive.is/VV7Mb。 - undefined

8
dup2( STDIN_FILENO, newfd )

并阅读:

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 );    

但是,我认为fcntl可能是更好的解决方案。
echo "salut" | code

6

一个程序写到标准输出的内容可以通过另一个程序的标准输入读取。因此,只需使用c语言编写prog1,使用printf()打印一些内容,使用prog2,使用scanf()读取一些内容。然后运行即可。

./prog1 | ./prog2

4

这里有一个示例

int main()
{
    char buff[1024] = {0};
    FILE* cvt;
    int status;
    /* Launch converter and open a pipe through which the parent will write to it */
    cvt = popen("converter", "w");
    if (!cvt)
    {
        printf("couldn't open a pipe; quitting\n");
        exit(1)
    }
    printf("enter Fahrenheit degrees: " );
    fgets(buff, sizeof (buff), stdin); /*read user's input */
    /* Send expression to converter for evaluation */
    fprintf(cvt, "%s\n", buff);
    fflush(cvt);
    /* Close pipe to converter and wait for it to exit */
    status=pclose(cvt);
    /* Check the exit status of pclose() */
    if (!WIFEXITED(status))
        printf("error on closing the pipe\n");
    return 0;
}

这个程序的重要步骤包括:
  1. popen() 调用建立了子进程和父进程之间管道的关联。
  2. fprintf() 调用使用管道作为普通文件,向子进程的 stdin 写入或从其 stdout 读取。
  3. pclose() 调用关闭管道并导致子进程终止。

我认为这个例子没有抓住问题的要点,尽管我承认“转换器”程序是一个不同的程序。第一条评论涉及完全独立的程序之间的通信,它们没有兄弟/父母/表亲关系。 - cmm

2
这个答案可能会为未来的谷歌搜索者提供帮助。
#include <stdio.h>
#include <unistd.h>

int main(){     
     int p, f;  
     int rw_setup[2];   
     char message[20];      
     p = pipe(rw_setup);    
     if(p < 0){         
        printf("An error occured. Could not create the pipe.");  
        _exit(1);   
     }      
     f = fork();    
     if(f > 0){
        write(rw_setup[1], "Hi from Parent", 15);    
     }  
     else if(f == 0){       
        read(rw_setup[0],message,15);       
        printf("%s %d\n", message, r_return);   
     }  
     else{      
        printf("Could not create the child process");   
     }      
     return 0;

}

您可以在这里找到一个高级的双向管道调用示例。


2

首先,让程序1将字符串写入stdout(就像您希望它在屏幕上显示一样)。然后第二个程序应从stdin读取字符串,就像用户从键盘输入一样。然后运行:

$ program_1 | program_2

0
如果你想要实现两个进程之间的通信,你必须确定你是在分叉这些进程还是不分叉。如果你选择分叉它们,那么你可以使用匿名管道。否则,你可以使用命名管道。
匿名管道
假设你在fork()之前声明了管道,那么子进程将继承父进程的管道,两者都指向同一个管道的两端。你可以利用这个事实在进程之间发送数据。
例如,在下面的程序中,父进程向子进程发送字符串"Hello World!",然后子进程将其显示出来。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char* argv[]) {
    int pipefd[2];
    pipe(pipefd);

    int pid =  fork();
    if (pid < 0) {
        fprintf(stderr, "failed to fork process");
    }
    else if (pid == 0) {
        // the child process doesn't use the pipe's write and thus it closes it.
        close(pipefd[1]);

        // read data from the pipe's read end and store into buffer.
        char buffer[80];
        read(pipefd[0], buffer, sizeof(buffer));
        printf("Received string: %s", buffer);

        exit(0);
    }
    else {
        // the parent process doesn't use the pipe's read end and thus it closes it.
        close(pipefd[0]);

        // write string to pipe's write end.
        char string[] = "Hello, world!\n";
        write(pipefd[1], string, (strlen(string)+1));

        exit(0);
    }

    return(0);
}

命名管道

使用命名管道时,您可以使用文件在不相关的进程之间传输数据。

您可以在Shell中使用mkfifo命令创建一个命名管道。或者您可以在C程序中使用库函数mkfifo()来创建它;您必须先包含<sys/types.h><sys/stat.h>库。

交互式创建命名管道

我们在Shell中创建命名管道pipefd

$ mkfifo pipefd

我们按照以下方式实施发件人流程:
// sender.c
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char* argv[]) {
    int pipefd = open("mypipe", O_WRONLY);

    // write string to pipe.
    char string[] = "Hello, world!\n";
    write(pipefd, string, (strlen(string)+1));

    close(pipefd);

    return(0);
}

我们按照以下方式实施接收器流程:
// receiver.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char* argv[]) {
    int pipefd = open("mypipe", O_RDONLY);

    char buffer[80];
    read(pipefd, buffer, sizeof(buffer));
    printf("Received string: %s", buffer);

    close(pipefd);

    return(0);
}

我们编译两个程序:
$ gcc sender.c -o sender
$ gcc receiver.c -o receiver

运行它们:
$ ./sender &
[1] 97687
$ ./receiver
Received string: Hello, world!

根据输出结果显示,父进程将字符串发送给子进程,然后子进程将其显示出来。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接