#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
void client(int, int), server(int, int);
int
main(int argc, char **argv)
{
int pipe1[2], pipe2[2];
pid_t childpid;
pipe(pipe1); /* create two pipes */
pipe(pipe2);
if ( (childpid = fork()) == 0) { /* child */
close(pipe1[1]);
close(pipe2[0]);
server(pipe1[0], pipe2[1]);
exit(0);
}
/* 4parent */
close(pipe1[0]);
close(pipe2[1]);
client(pipe2[0], pipe1[1]);
waitpid(childpid, NULL, 0); /* wait for child to terminate */
exit(0);
}
mainpipe.c
// ubuntu16.04 pipe 默認是半雙工管道
man pipe
pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication.
client.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXLINE 128
void
client(int readfd, int writefd)
{
size_t len;
ssize_t n;
char buff[MAXLINE];
/* 4read pathname */
fgets(buff, MAXLINE, stdin);
len = strlen(buff); /* fgets() guarantees null byte at end */
if (buff[len-1] == '\n')
len--; /* delete newline from fgets() */
/* 4write pathname to IPC channel */
write(writefd, buff, len);
/* 4read from IPC, write to standard output */
while ( (n = read(readfd, buff, MAXLINE)) > 0)
write(STDOUT_FILENO, buff, n);
}
server.c
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "my_err.h"
#define MAXLINE 128
void
server(int readfd, int writefd)
{
int fd;
ssize_t n;
char buff[MAXLINE+1];
/* 4read pathname from IPC channel */
if ( (n = read(readfd, buff, MAXLINE)) == 0)
err_quit("end-of-file while reading pathname");
buff[n] = '\0'; /* null terminate pathname */
if ( (fd = open(buff, O_RDONLY)) < 0) {
/* 4error: must tell client */
snprintf(buff + n, sizeof(buff) - n, ": can't open, %s\n",
strerror(errno));
n = strlen(buff);
write(writefd, buff, n);
} else {
/* 4open succeeded: copy file to IPC channel */
while ( (n = read(fd, buff, MAXLINE)) > 0)
write(writefd, buff, n);
close(fd);
}
}
gcc -o mainpipe mainpipe.c client.c server.c
./mainpipe
/etc/issue
Ubuntu 16.04.5 LTS \n \l
./mainpipe
/no/such/file
/no/such/file: can't open, No such file or directory
popen 與 pclose
popen 創建一個管道並啓動一個進程,改進程要麼從管道讀出標準輸入,要麼往管道寫入標準輸出。
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
type is "r", calling process read the stdout of command
type is "w", calling process write to the stdin of the command
客戶-服務器程序
#include "unistd.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define MAXLINE 128
int
main(int argc, char **argv)
{
size_t n;
char buff[MAXLINE], command[MAXLINE];
FILE *fp;
/* 4read pathname */
fgets(buff, MAXLINE, stdin);
n = strlen(buff); /* fgets() guarantees null byte at end */
if (buff[n-1] == '\n')
n--; /* delete newline from fgets() */
snprintf(command, sizeof(command), "cat %s", buff);
fp = popen(command, "r");
/* 4copy from pipe to standard output */
while (fgets(buff, MAXLINE, fp) != NULL)
fputs(buff, stdout);
pclose(fp);
exit(0);
}
gcc -g -o mainpopen mainpopen.c
./mainpopen
/etc/issue