管道
匿名管道:半双工,数据单向流动,只能用与有亲缘关系的进程间。
pipe_read_buf_small.c
#include <unistd.h>
#include <sys/types.h>
int main(){
int pipe_fd[2];
pid_t pid;
// char* r_buf;
// char* w_buf;
char r_buf[4096];
char w_buf[4096*2];
int r_num;
int w_num;
int count=0;
if(pipe(pipe_fd)==-1){
printf("Crate pipe error\n");
}
if((pid=fork())<0){
printf("Fork error\n");
}else if(pid==0){
printf("Child:\n");
close(pipe_fd[1]);
while(1){
sleep(1);
if((r_num=read(pipe_fd[0],r_buf,1000))==-1){
printf("Child read error\n");
}else {
printf("Child read successfully read:%d\n",r_num);
}
count++;
if(count>10){
break;
}
}
close(pipe_fd[0]);
printf("c dead\n");
exit(0);
}else{
printf("Parent:\n");
close(pipe_fd[0]);
if((w_num=write(pipe_fd[1],w_buf,1024))<0){
printf("p write error\n");
}else{
printf("p write successfully :%d\n",w_num);
}
w_num=write(pipe_fd[1],w_buf,4096);
printf("p write suc 2 :%d\n",w_num);
printf("p dead\n");
exit(0);
}
return 0;
}
pipe_close_child_read.c
#include <sys/types.h>
#include <errno.h>
#include <sys/types.h>
int main(){
int pipe_fd[2];
pid_t pid;
// char* r_buf;
// char* w_buf;
char r_buf[4096];
char w_buf[4096*2];
int r_num;
int w_num;
int count=0;
if(pipe(pipe_fd)==-1){
printf("Crate pipe error\n");
}
if((pid=fork())<0){
printf("Fork error\n");
}else if(pid==0){
printf("Child:\n");
close(pipe_fd[1]);
while(1){
sleep(1);
if((r_num=read(pipe_fd[0],r_buf,1000))==-1){
printf("Child read error\n");
}else {
printf("Child read successfully read:%d\n",r_num);
}
count++;
if(count>10){
break;
}
}
close(pipe_fd[0]);
printf("c dead\n");
exit(0);
}else{
printf("Parent:\n");
close(pipe_fd[0]);
if((w_num=write(pipe_fd[1],w_buf,1024))<0){
printf("p write error\n");
}else{
printf("p write successfully :%d\n",w_num);
}
w_num=write(pipe_fd[1],w_buf,4096);
printf("p write suc 2 :%d\n",w_num);
printf("p dead\n");
exit(0);
}
return 0;
}
pipe_close_parent_write.c
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
int main(){
int pipe_fd[2];
pid_t pid;
char r_buf[10];
char w_buf[4];
int r_num;
if(pipe(pipe_fd)==-1){
printf("Open pipe error\n");
return -1;
}
if((pid=fork())<0){
printf("Fork error\n");
}else if(pid==0){
printf("Child process\n");
close(pipe_fd[1]);//close write
sleep(3);//make sure the parent process close write
r_num=read(pipe_fd[0],r_buf,10);
//printf("Read size:%d,%d\n",r_num,atoi(r_buf));
printf("Read size:%d,%s\n",r_num,r_buf);
close(pipe_fd[0]);
printf("Child dead!\n");
exit(0);
}else if(pid>0){
printf("Parent process\n");
close(pipe_fd[0]);//close read
strcpy(w_buf,"123");
if(write(pipe_fd[1],w_buf,5)!=-1){
printf("Write successfully\n");
}
close(pipe_fd[1]);
printf("Parent process write has been closed!\n");
// sleep(6);
printf("Parent dead!\n");
exit(0);
}
exit(0);
}
命名管道:可以在不具有亲缘关系的进程间通信。
FIFO_w.c
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#define FIFO_SERVER "/tmp/fifoserver"
int main(){
int fd;
char w_buf[4096*2];
int w_num;
if(mkfifo(FIFO_SERVER,O_CREAT|O_EXCL)<0){
printf("Create fifo error\n");
}
// fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
fd=open(FIFO_SERVER,O_WRONLY,0);
if(fd==-1){
if(errno==ENXIO){
printf("open error;no reading process\n");
}
}
memset(w_buf,0,4096*2);
w_num=write(fd,w_buf,2048);
if(w_num==-1){
if(errno==EAGAIN){
printf("1write to fifo error, try later\n");
}
}else {
printf("1write successfully:%d\n",w_num);
}
w_num=write(fd,w_buf,5000);
if(w_num==-1){
if(errno==EAGAIN){
printf("2write to fifo error, try later\n");
}
}else {
printf("2write successfully:%d\n",w_num);
}
return 0;
}
FIFO_r.c
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#define FIFO_SERVER "/tmp/fifoserver"
#define SIZE 4096
int main(int argc,char **argv){
char r_buf[SIZE];
int fd;
int r_num=atoi(argv[1]);
printf("Read wants to read:%d\n",r_num);
//fd=open(FIFO_SERVER,O_RDONLY|O_NONBLOCK,0);
fd=open(FIFO_SERVER,O_RDONLY,0);
if(fd==-1){
printf("open %s for read error\n",FIFO_SERVER);
exit(0);
}
int count=0;
while(1){
r_num=read(fd,r_buf,SIZE);
if(r_num==-1){
if(errno==EAGAIN)
printf("no data available\n");
}else {
printf("read OK :%d\n",r_num);
}
sleep(1);
count++;
if(count>20)break;
}
unlink(FIFO_SERVER);
return 0;
}
相关推荐
命名管道 pipe_write.c为发送方 pipe_read.c为接收方
linux进程间通信管道的实现,ipc,进程间通信
Linux进程间的通信实现方式,比如管道、消息队列等
linux进程间通信方式,以有名管道和匿名管道的方式通信
linux 进程间通信相关教程,管道 共享内存 共享队列 信号量 socket 锁 方面的讲解,值得下载
操作系统实验四:Linux下进程管道通信 任务1:使用Pipe创建管道,创建一个子进程,子进程向父进程发送消息“I am your son!”,父进程接收到子进程的消息后,显示在屏幕上,并向子进程发送“I am your father!”。子...
Linux进程间通信之管道.适用于亲缘关系间的两个进程间,本程序为基本模型,实现了父子进程间的管道通信.
linux进程间通信详解,包括管道及有名管道,消息队列,共享内存等
Linux进程间管道通信的研究Linux进程间管道通信的研究
IPC(InterProcess Communication)是各种进程通信方式的...前五种IPC只能用于一台主机内的进程间通信,套接口既可以用于一台主机内的进程间通信,也可以用于多台主机间的进程间通信。关于套接口将在网络编程中介绍。
linux进程间通信,对管道、命名管道、信号、信号量、消息队列、共享内存等机制进行了分析与讲解,详细讲解了与这些进程间通信有关的C函数,并给出了示例程序,是讲解linux进程间通信不可多得的佳作。
中嵌教育的PPT linux进程间通信 1. 进程间通信概述 2. 管道通信 3. 信号 4. 共享内存 5. 消息队列
Linux进程间的管道通信.pdf
Linux 进程间通信的几种主要手段。其中管道和有名管道是最早的进程间通信机制之一,管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因 此,除具有管道所具有的功能外,它还允许无亲缘关系...
管道 信号 共享内存
Linux进程间通信-命名管道实例.pdf 学习资料 复习资料 教学资源
概述了 linux 进程间通信的几种主要手段。其中管道和有名管道是最早的进程间通信机制之一,管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许无...
Linux进程间通信4——使用命名管道.doc
Linux进程间管道通信的研究.pdf