IPC: InterProcess Communication
IPC的方式通常有管道(包括无名管道和命名管道)、消息队列、信号量、共享存储、Socket、Streams等。其中 Socket和Streams支持不同主机上的两个进程IPC。
不同进程在物理内存上是相互独立的。
想要实现进程间通信,就要找到公共的资源,主要有三种方式:
-
一个管道有且只有2个端口,最多支持两个进程通信。
-
当缓冲区满时阻塞。
-
像管道一样连接两个进程,一个进程的输入作为另一个进程的输出。
-
本质是字节流,一个端口负责输入字节,一个端口负责输出字节。
-
访问一个管道的两个进程:单工,固定的读端和写端。
-
访问两个管道的两个进程:半双工。
-
只能用于具有亲缘关系的进程之间的通信(父子进程或兄弟进程)。
-
可以看成是一种特殊的文件,但它不属于任何文件系统,存在于内核中。
一、管道
通常指无名管道。

管道是内核空间的一片缓冲区,被封装成文件的形式,因此用户可以使用文件IO接口读写。
1、特点
-
单向,具有固定的读端和写端。
-
像管道一样连接两个进程,一个进程的输入作为另一个进程的输出。
-
只能用于具有亲缘关系的进程之间的通信(父子进程或兄弟进程)。
-
可以看成是一种特殊的文件,但它不属于任何文件系统,只存在于内核中。
2、API
1 #include <unistd.h> // 头文件
2 int pipe(int fd[2]); // 返回值:若成功返回0,失败返回-1
pipe是系统调用,参数int fd[2],作为传出参数,用户可以得到两个描述符:fd[0]为读端,fd[1]为写端。

关闭管道只需将这两个文件描述符关闭。
3、例子
单向通信

示例代码:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#define handle_error(msg) \
{ perror(msg); exit(-1); }
int main(void)
{
int fd_pipe[2] = {0};
int pid = -1;
if(-1 == pipe(fd_pipe))
handle_error("pipe");
if(-1 == (pid = fork()))
handle_error("fork");
if(0 == pid)
{
char buf[20] = {0};
int len;
close(fd_pipe[0]);
puts("child process start");
len = read(1, buf, 20);
write(fd_pipe[1], buf, len + 1);
close(fd_pipe[1]);
puts("child process end");
exit(0);
}
else if(0 < pid)
{
char buf[20] = {0};
int len;
close(fd_pipe[1]);
puts("parent process start");
len = read(fd_pipe[0], buf, 20);
write(2, buf, len);
close(fd_pipe[0]);
puts("parent process end");
exit(0);
}
return 0;
}
运行结果:
book@100ask:~/Desktop/mycode/linux/IPC$ gcc main.c -o app
book@100ask:~/Desktop/mycode/linux/IPC$ ./app
parent process start
child process start
Hello, world!
child process end
Hello, world!
parent process end
双向通信

示例:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#define handle_error(msg) \
{ perror(msg); exit(-1); }
int main(void)
{
int fd_pipe[2] = {0};
int fd_pipe_2[2] = {0};
int pid = -1;
if(-1 == pipe(fd_pipe) || -1 == pipe(fd_pipe_2))
handle_error("pipe");
if(-1 == (pid = fork()))
handle_error("fork");
if(0 == pid)
{
char buf[20] = {"child process"};
close(fd_pipe[0]);
close(fd_pipe_2[1]);
puts("child process start");
printf("input string: ");
scanf("%s", buf);
write(fd_pipe[1], buf, strlen(buf));
buf[read(fd_pipe_2[0], buf, 20)] = '\0';
printf("%s\n", buf);
close(fd_pipe[1]);
close(fd_pipe_2[0]);
puts("child process end");
exit(0);
}
else if(0 < pid)
{
char buf[20] = {0};
int len;
close(fd_pipe[1]);
close(fd_pipe_2[0]);
puts("parent process start");
len = read(fd_pipe[0], buf, 20);
printf("%s\n", buf);
strcat(buf, "Okay");
write(fd_pipe_2[1], buf, 20);
close(fd_pipe[0]);
close(fd_pipe_2[1]);
puts("parent process end");
exit(0);
}
return 0;
}
运行结果:
book@100ask:~/Desktop/mycode/linux/IPC/pipe$ ./app
parent process start
child process start
input string: hello
hello
parent process end
helloOkay
child process end
545

被折叠的 条评论
为什么被折叠?



