如何使用pipe道在两个程序之间发送一个简单的string?

我试图在网上search,但几乎没有任何资源。 一个小例子就足够了。

编辑我的意思是说,两个不同的C程序相互沟通。 一个程序应该发送“嗨”,另一个应该收到它。 就是这样

一个普通的pipe道只能连接两个相关的进程。 它由一个进程创build,并在最后一个进程closures时消失。

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

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

注意:为了简单起见,上面的代码省略了错误检查。

从C中创buildpipe道 ,这将告诉你如何分叉程序来使用pipe道。 如果你不想fork(),你可以使用命名pipe道 。

另外,你可以得到prog1 | prog2的效果 prog1 | prog2prog1的输出发送到stdout,并从prog2 stdin 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); } 
 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可以是更好的解决scheme

 echo "salut" | code 

一个程序写入stdout的内容可以通过stdin读取。 简单地说,使用c,编写prog1使用printf()prog2使用scanf()读取某些内容。 然后就跑吧

 ./prog1 | ./prog2 

这是一个示例 :

 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. build立subprocess和父进程中pipe道的关联的popen()调用。
  2. 使用pipe道作为普通文件的fprintf()调用写入subprocess的标准input或从标准输出读取。
  3. closurespipe道并导致subprocess终止的pclose()调用。

首先,程序1将string写入标准输出(就好像您希望它出现在屏幕中一样)。 那么第二个程序应该从stdin中读取一个string,就像用户正在用键盘input一样。 那么你运行:

program_1 | 节目2

这个答案可能对未来的Google员工有所帮助。

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

您可以在这里find一个高级的双向pipe道调用示例。