1-》管道(pipe)和有名管道(fifo).
2-》消息队列
3-》共享内存
4-》信号量
5-》信号(signal)
6-》套接字(sicket)
在Linux系统中,管道通信可以通过使用系统调用来实现。
使用格式为:
表头文件 #include<unistd.h>
定义函数 int pipe(int filedes[2]);
函数说明
pipe()会建立管道,并将文件描述词由参数 filedes 数组返回。
filedes[0]为管道里的读取端,所以pipe用read调用的
filedes[1]则为管道的写入端。
返回值: 若成功则返回零,否则返回-1,错误原因存于 errno 中。
错误代码:
EMFILE 进程已用完文件描述词最大量
ENFILE 系统已无文件描述词可用。
EFAULT 参数 filedes 数组地址不合法。
但值得我们注意的是:管道它有自身的特点。
(1)管道通信是单向的,并且遵守先进先出的原则,即先写入的数据先读出。
(2)管道是一个无结构,无固定大小的字节流。
(3) 管道把一个进程的标准输出和另一个进程的标准输入连接在一起。数据读出后就意味着从管道中移走了,消失了。其它的进程都不能
再读到这些数据。就像我们平常见到的管子水流走了就没有了。 这点很重要!!
(4) pipe这种管道用于两个有亲缘关系的进程之间。eg:父子进程......
#include <unistd.h>
#include <stdio.h>
int main( void )
{
int filedes[2];
char buf[80];
pid_t pid;
pipe( filedes );
if ( (pid=fork()) > 0 )
{
printf( "This is in the father process,here write a string to the pipe.\n" );
char s[] = "Hello world , this is write by pipe.\n";
write( filedes[1], s, sizeof(s) );
close( filedes[0] );
close( filedes[1] );
}
else
{
printf( "This is in the child process,here read a string from the pipe.\n" );
read( filedes[0], buf, sizeof(buf) );
printf( "%s\n", buf );
close( filedes[0] );
close( filedes[1] );
}
waitpid( pid, NULL, 0 );
return 0;
}