管道由pipe函數創建,是進程間通信的一種方式。
#include <unistd.h>
int pipe(int fd[2]); 返回:成功時爲0,出錯時爲-1
該函數數返回兩個文件描述符:fd[0]和fd[1]。前者用於讀,後者用於寫。
管道的典型用途是提供兩個不同進程(一個是父進程,一個是子進程)間的通信手段。 一個管道只能提供單向的數據流,當需要一個雙向的數據流時,我們必須創建兩個管道,每個方向一個。實際步驟如下:
1.創建管道1(fd1[0]和fd1[1])和管道2(fd2[0]和fd2[1])
2.fork
3.父進程關閉管道1的讀端(fd1[0])
4.父進程關閉管道2的寫端(fd2[1])
5.子進程關閉管道1的寫端(fd1[1)
6.子進程關閉管道2的讀端(fd2[0)
實現代碼如下:
/*
* main.cpp
*
* Created on: 2013-10-26
* Author: Richard
*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
void client(int readfd, int writefd)
{
size_t len;
size_t n;
char buff[1024];
fgets(buff, 1024, stdin);
len = strlen(buff);
if (buff[len - 1] == '\n')
{
len--;
}
write(writefd, buff, len);
while ((n = read(readfd, buff, 1024)) > 0)
{
write(STDOUT_FILENO, buff, n);
}
}
void server(int readfd, int writefd)
{
int fd;
ssize_t n;
char buff[1025];
if ((n = read(readfd, buff, 1024)) == 0)
{
printf("End of file while reading pathname");
exit(0);
}
buff[n] = '\0';
if ((fd = open(buff, O_RDONLY)) < 0)
{
snprintf(buff + n, sizeof(buff) - n, "Cannot open, %s\n",
strerror(errno));
n = strlen(buff);
write(writefd, buff, n);
}
else
{
while ((n = read(fd, buff, 1024)) > 0)
{
write(writefd, buff, n);
}
close(fd);
}
}
int main(int argc, char **argv)
{
int pipe1[2];
int pipe2[2];
pid_t childpid;
pipe(pipe1);
pipe(pipe2);
if ((childpid = fork()) == 0)
{
close(pipe1[1]);
close(pipe2[0]);
server(pipe1[0], pipe2[1]);
exit(0);
}
close(pipe1[0]);
close(pipe2[1]);
client(pipe2[0], pipe1[1]);
waitpid(childpid, NULL, 0);
return 0;
}
main函數創建兩個管道並fork一個子進程。客戶作爲父進程運行,服務器作爲子進程運行。第一個管道用於客戶向服務器發送路徑名,第二個管道用於服務器向客戶發送該文件的內容。