Linux笔记_进程通讯——管道
2015-01-24 22:01
330 查看
管道通信
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);
}
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);
}
相关文章推荐
- Linux 进程间通讯之创建无名管道和读写无名管道
- Linux进程间通讯方式之管道pipe
- linux下进程间的通信——有名管道fifo学习笔记
- Linux进程间通讯一--无名管道PIPE
- 【Linux】利用管道在进程通讯拷贝文件
- Linux 进程间通讯之有名管道方式
- Linux笔记_进程通讯——信号量
- Linux 进程间通讯之创建无名管道和读写无名管道
- linux 进程学习笔记-named pipe (FIFO)命名管道
- Linux学习笔记——进程间的通信-管道
- 神奇的linux:父子进程管道通讯
- Linux笔记_进程通讯——共享内存
- linux 进程间管道通讯 popen以及pclose
- Linux进程通讯:管道通讯、信号通讯、共享内存
- Linux下用fork()派生的子进程通过pipe管道通讯的实例详解("生产者-消费者"问题)
- Linux学习笔记24——进程管道
- Linux下进程的创建及其使用管道进行进程之间的通讯[00原创]
- Linux笔记_进程通讯——信号通讯
- Linux 进程间通讯之有名管道方式
- linux进程间通讯-命名管道