1 无名管道,工作方式:半双工,即单项传输,无名管道局限性较大,只实现了有亲缘关系的进程进程间的通信,如下是简单的实现例子
涉及到的函数只有pipe(),其它的read(),write()为通用文件操作的系统调用.原型如下:
int pipe(int pipefd[2]);
简单测试代码如下:
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char**argv)
{
int pipe_fd[2];
pid_t child_pid;
char buf[100];
memset(buf, 0, sizeof(buf));
if (pipe(pipe_fd) < 0) {//创建管道,之后就读写就ok了
perror("pipe creat");
return -1;
}
if ( (child_pid = fork()) == -1) {
perror("fork()");
}
if (child_pid == 0) {//child
int w_ret = 0;
close(pipe_fd[0]);//管道只能一端读,一端写来实现自身功能,否则没意义0为读 1为写
while (1) {
strcpy(buf, "hello i am child");
w_ret = write(pipe_fd[1], buf, strlen(buf));//写操作
if (w_ret != -1) {
printf("write\n");
} else {
printf("write error....\n");
}
usleep(100000);
}
close(pipe_fd[1]);//理论上 不用时要关掉,这里拿来play
} else {//parent
int read_len = 0;
close(pipe_fd[1]);//关掉写端
while (1) {
read_len = read(pipe_fd[0], buf, 100);//读数据,读到怎么做,读不到怎么做等
printf("p->child pid %d read len %d: %s\n", child_pid, read_len, r_buf);
usleep(500000);
}
close(pipe_fd[0]);//同上
}
return 0;
}
2有名管道:有名管道克服了无名管道需要亲缘关系的缺点,所以个人觉得有名管道还是比较有用处的,它和管道一样,也是半双工,先进先出的方式。操作同样和文件类似,但是不支持想lseek等文件操作,严格的先进先出。
有名管道用到的函数:
int mkfifo(char *pathname, mode_t mode)
其中mode和open一样,而且有名管道创建之后也需要open,操作类似文件。
简单测试代码如下:
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#define FIFO_FILE "/tmp/testfifo"
int getFifoFd(mode_t mode)//减少两个进程调用的公共代码, 创建管道和以mode的方式打开管道
{
int fd;
if ((mkfifo(FIFO_FILE, O_CREAT | O_EXCL | 0600) < 0) && (errno != EEXIST)) {//创建管道
printf("creat: mkfifo error\n");
}
fd = open(FIFO_FILE, mode);//打开管道
if (fd < 0) {//
perror("open()");
}
return fd;
}
int main(int argc, char **argv)
{
pid_t pid;
char buf[100];
memset(buf, 0, sizeof(buf));
if ((pid = fork()) < 0) {
perror("fork()");
} else if (pid == 0) {//child
int fd = getFifoFd(O_WRONLY);
strcpy(buf, "hello write in child");
while (1) {
write(fd, buf, strlen("hello write in child"));
printf("write in child\n");
usleep(500000);
}
} else {//parent
int fd = getFifoFd(O_RDONLY);
int num;
usleep(1000000);
while (1) {
num = read(fd, buf, 100);
if (num > 0) {
printf("%d read from buffer: %s\n", num, buf);
}
usleep(1000000);
}
}
return 0;
}
涉及到的函数只有pipe(),其它的read(),write()为通用文件操作的系统调用.原型如下:
int pipe(int pipefd[2]);
简单测试代码如下:
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char**argv)
{
int pipe_fd[2];
pid_t child_pid;
char buf[100];
memset(buf, 0, sizeof(buf));
if (pipe(pipe_fd) < 0) {//创建管道,之后就读写就ok了
perror("pipe creat");
return -1;
}
if ( (child_pid = fork()) == -1) {
perror("fork()");
}
if (child_pid == 0) {//child
int w_ret = 0;
close(pipe_fd[0]);//管道只能一端读,一端写来实现自身功能,否则没意义0为读 1为写
while (1) {
strcpy(buf, "hello i am child");
w_ret = write(pipe_fd[1], buf, strlen(buf));//写操作
if (w_ret != -1) {
printf("write\n");
} else {
printf("write error....\n");
}
usleep(100000);
}
close(pipe_fd[1]);//理论上 不用时要关掉,这里拿来play
} else {//parent
int read_len = 0;
close(pipe_fd[1]);//关掉写端
while (1) {
read_len = read(pipe_fd[0], buf, 100);//读数据,读到怎么做,读不到怎么做等
printf("p->child pid %d read len %d: %s\n", child_pid, read_len, r_buf);
usleep(500000);
}
close(pipe_fd[0]);//同上
}
return 0;
}
2有名管道:有名管道克服了无名管道需要亲缘关系的缺点,所以个人觉得有名管道还是比较有用处的,它和管道一样,也是半双工,先进先出的方式。操作同样和文件类似,但是不支持想lseek等文件操作,严格的先进先出。
有名管道用到的函数:
int mkfifo(char *pathname, mode_t mode)
其中mode和open一样,而且有名管道创建之后也需要open,操作类似文件。
简单测试代码如下:
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#define FIFO_FILE "/tmp/testfifo"
int getFifoFd(mode_t mode)//减少两个进程调用的公共代码, 创建管道和以mode的方式打开管道
{
int fd;
if ((mkfifo(FIFO_FILE, O_CREAT | O_EXCL | 0600) < 0) && (errno != EEXIST)) {//创建管道
printf("creat: mkfifo error\n");
}
fd = open(FIFO_FILE, mode);//打开管道
if (fd < 0) {//
perror("open()");
}
return fd;
}
int main(int argc, char **argv)
{
pid_t pid;
char buf[100];
memset(buf, 0, sizeof(buf));
if ((pid = fork()) < 0) {
perror("fork()");
} else if (pid == 0) {//child
int fd = getFifoFd(O_WRONLY);
strcpy(buf, "hello write in child");
while (1) {
write(fd, buf, strlen("hello write in child"));
printf("write in child\n");
usleep(500000);
}
} else {//parent
int fd = getFifoFd(O_RDONLY);
int num;
usleep(1000000);
while (1) {
num = read(fd, buf, 100);
if (num > 0) {
printf("%d read from buffer: %s\n", num, buf);
}
usleep(1000000);
}
}
return 0;
}