Linux笔记_进程通讯——管道
来源:互联网 发布:mac无法删除文件 编辑:程序博客网 时间:2024/06/10 07:37
管道通信
1.定义:
1)管道式单向的,先进先出的,把一个进程的输出和另一个进程的输入连接在一起。
2)一个进程(写进程)在管道的尾部写入数据,另一个进程(读进程)从管道的头部读出数据。
3)数据被一个进程读出后,将被从管道中删除,其它读进程将不能再读到这些数据。(管道提供了简单的流控制机制,进程试图读空管道时,进程将阻塞。同样,管道已经满时,进程再试图管道写入数据,进程将阻塞)
2.管道创建
1)无名管道:用于父进程和子进程的通信
创建:pipe() int pipe(int filedis[2])
当一个管道建立时,它会创建两个文件描述符:filedis[0]用于读管道,filedis[1]用于写管道
管道关闭:关闭管道只需将这两个文件描述符关闭即可,可以使用普通的close函数逐个关闭
管道读写
管道用于不同进程间的通信。通常先创建一个管道,再通过fork函数创建一个子进程,该子进程会继承父进 程 所创建的管道。
*必须在系统调用fork()前调用pipe(),否则子进程将不会继承文件描述符
2)命名管道
命名管道和无名管道基本相同,不同点在于:无名管道只能由父子进程使用,但通过命名管道,不相关的进程也能交换数据。
创建:
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname,mode_t mode)
pathname:FIFO文件名
mode:属性:
一旦创建了一个fifo,就可以用open打开它,一般的文件访问函数(close,read,write等)都可以用于fifo
操作:
打开FIFO时,非阻塞标志(O_NONBLOCK)将对以后的读写产生以下影响:
1)没有使用O_NONBLOCK:访问要求无法满足时进程将阻塞。如试图读取空的FIFO,将导致进程阻塞。
2)使用O_NONBLOCK:访问要求无法满足时不阻塞,立即出错返回,errno是ENXIO。
代码演示:
/////////////////////fifo_read////////////////////
#include <sys/types.h>
#include <sys/stat.h>#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO "/tmp/myfifo"
main(int argc,char** argv)
{
char buf_r[100];
int fd;
int nread;
/* 创建管道 */
if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
printf("cannot create fifoserver\n");
printf("Preparing for reading bytes...\n");
memset(buf_r,0,sizeof(buf_r));
/* 打开管道 */
fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);
if(fd==-1)
{
perror("open");
exit(1);
}
while(1)
{
memset(buf_r,0,sizeof(buf_r));
if((nread=read(fd,buf_r,100))==-1)
{
if(errno==EAGAIN)
printf("no data yet\n");
}
printf("read %s from FIFO\n",buf_r);
sleep(1);
}
pause(); /*暂停,等待信号*/
unlink(FIFO); //删除文件
}
/////////////fifo_write//////////////////////
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO_SERVER "/tmp/myfifo"
main(int argc,char** argv)
{
int fd;
char w_buf[100];
int nwrite;
/*打开管道*/
fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
if(argc==1)
{
printf("Please send something\n");
exit(-1);
}
strcpy(w_buf,argv[1]);
/* 向管道写入数据 */
if((nwrite=write(fd,w_buf,100))==-1)
{
if(errno==EAGAIN)
printf("The FIFO has not been read yet.Please try later\n");
}
else
printf("write %s to the FIFO\n",w_buf);
}
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FIFO_SERVER "/tmp/myfifo"
main(int argc,char** argv)
{
int fd;
char w_buf[100];
int nwrite;
/*打开管道*/
fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
if(argc==1)
{
printf("Please send something\n");
exit(-1);
}
strcpy(w_buf,argv[1]);
/* 向管道写入数据 */
if((nwrite=write(fd,w_buf,100))==-1)
{
if(errno==EAGAIN)
printf("The FIFO has not been read yet.Please try later\n");
}
else
printf("write %s to the FIFO\n",w_buf);
}
0 0
- Linux笔记_进程通讯——管道
- Linux笔记_进程通讯——信号通讯
- Linux笔记_进程通讯——概述
- Linux笔记_进程通讯——共享内存
- Linux笔记_进程通讯——消息队列
- Linux笔记_进程通讯——信号量
- 进程间通讯—管道
- Linux—进程间通讯—无名管道
- linux——进程间通讯(管道)
- linux进程间通讯--管道
- Linux-进程间通讯-管道
- Linux进程间通讯--管道(有名管道
- 进程间通讯——有名管道
- 进程间通讯-——管道
- 进程间通讯——无名管道
- 进程间通讯——管道(有名管道)
- 进程间的通讯方式_管道(有名管道)
- 进程间的通讯方式_管道(无名管道)
- Cocos2D教程:使用SpriteBuilder和Cocos2D 3.x开发横版动作游戏——Part 2
- Linux笔记_进程
- 关于js的CreateTextFile和OpenTextFile
- Linux笔记_进程通讯——概述
- Android ADB server didn't ACK * failed to start daemon * 简单有效的解决方案
- Linux笔记_进程通讯——管道
- uva 1030 Image Is Everything(迭代更新)
- Linux笔记_进程通讯——信号通讯
- Linux笔记_进程通讯——共享内存
- Hashcode的作用
- uva1330
- 卡尔曼滤波MATLAB源代码,from智慧石
- Linux笔记_进程通讯——消息队列
- 比较两个数组的大小