进程间通信(ipc)
应用场景:
数据传输
资源共享
通知事件
进程控制
system v at&t system v
posix(portable operating system interface)可移植操作系统接口
常用的进程间通信的方式:
管道(pipe)和有名管道(fifo)
信号(signal)
消息队列
共享内存
信号量
套接字(socket)
管道通信
半双工的,数据只能向一个方向流动
只能用于父子进程间或兄弟进程间
先进先出,一个进程在管道的尾部写入数据,另一个进程从管道的头部读出数据
无名管道 用于父子进程间的通信
创建管道
int pipe(int filedis[2])
管道建立时,所创建的两个文件描述符
filedis[0] 管道的读取端
filedis[1] 管道的写入端
关闭管道
将两个文件描述符关闭
示例代码如下:
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
int pipe_fd[2];
if(pipe_fd<0){
printf("pipe create error\n");
return -1;
}else{
printf("pipe create success\n");
}
close(pipe_fd[0]);
close(pipe_fd[1]);
}
管道的读写
选创建一个管道,通过fork()函数创建一个子进程,子进程会继承父进程所创建的管道
示例代码如下:
#include <unistd.h>
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
int pipe_fd[2];
pid_t pid;
char buf_r[100];
char *p_wbuf;
int r_num;
memset(buf_r,0,sizeof(buf_r));
/*创建管道*/
if(pipe(pipe_fd)<0){
printf("create pipe error\n");
return -1;
}
/*创建子进程*/
if((pid=fork())==0){//子进程中运行
printf("\n");
close(pipe_fd[1]);
sleep(2);
if((r_num=read(pipe_fd[0],buf_r,100))>0){
printf("%d number read form the pipe is %s\n",r_num,buf_r);
}
close(pipe_fd[0]);
exit(0);
}
else if(pid>0){//父进程中运行
close(pipe_fd[0]);
if(write(pipe_fd[1],"hello",5)!=-1){
printf("parent write1 hello!\n");
}
if(write(pipe_fd[1]," pipe",5)!=-1){
printf("parent write2 page!\n");
}
close(pipe_fd[1]);
sleep(3);
waitpid(pid,NULL,0);
exit(0);
}
}
运行结果如下:
[retacn@localhost tmp]$ gcc pipe_rw.c -o pipe_rw
[retacn@localhost tmp]$ ./pipe_rw
parent write1 hello!
parent write2 page!
10 number read form the pipe is hello pipe
有名管道 用于运行于同一系统中的任意两个进程间的通信
fifo,与无命管道的区别是不相关的进程也能交换数据
建立命名管道
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname,mode_t mode)
pathname:fifo文件名
mode:属性
当使用open打开fifo文件时,O_NONBLOCK会产生以下影响
使用时,访问要求无法满足时不阻塞,立即返回,errno是enxio
不使用时,访问要求无法满足时进程将阻塞
示例代码如下:
fifo_read.c
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#define FIFO "/home/retacn/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 form fifo\n",buf_r);
sleep(1);
}
/*暂停,等待信号*/
pause();
}
fifo_write.c
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#define FIFO_SERVER "/home/retacn/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 sonething\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 the fifo\n",w_buf);
}
}
- 浏览: 246945 次
- 性别:
- 来自: 济南
文章分类
- 全部博客 (303)
- c (31)
- c++ (16)
- java (18)
- c# (1)
- python (3)
- java web (6)
- oracle (7)
- sqlserver (2)
- mysql (2)
- android (24)
- android系统 (15)
- android多媒体部分 (15)
- android游戏 (12)
- linux (26)
- javaScript (1)
- ajax (1)
- node JS (2)
- html (5)
- apache (3)
- jboss (1)
- weblogic (0)
- 通信协议 (10)
- 云计算 (1)
- 分布式 (5)
- ejb (1)
- webservice (5)
- 设计模式 (16)
- JNI (6)
- swing (13)
- 版本控制 (1)
- UML (1)
- xml (4)
- spring (5)
- hibernate (5)
- struts1 (3)
- struts2 (4)
- ibatis (0)
- tomcat (2)
- 心得体会 (1)
- css (1)
- 嵌入式 (41)
- arm体系结构 (10)
发表评论
-
u-boot Makefile 文件分析
2013-06-01 21:44 2357Makefile文件分析 # #(C)Copyri ... -
uboot start.S文件分析
2013-06-03 22:18 1276U-boot第一个开始文件arch\arm\cpu\arm1 ... -
u-boot mkconfig文件分析
2013-05-31 21:29 1102Mkconfig文件分析 #!/bin/ ... -
链接地址学习笔记
2013-05-05 12:40 1233链接地址 启动过程 示例代码如下: ... -
DDR学习笔记
2013-05-11 14:19 989DDR 15条地址线32k 128M*2(20)=2(2 ... -
nand flash学习笔记一
2013-05-13 21:05 923Nandflash 原理图上有data0-data7 ... -
openJTAG学习笔记一
2013-05-22 21:45 2114安装软件 光盘Windows\install目录下的 01.O ... -
linux进程管理学习笔记
2013-03-28 20:57 1284linux 进程管理 1 linux进程控制 进程的四个要素: ... -
字符设备驱动程序学习笔记一
2013-04-01 21:55 819linux 驱动程序 字符设备驱动程序 网络接口驱动程序 块设 ... -
字符设备驱动程序学习笔记二
2013-04-04 10:29 710字符驱动程序 1 设备号 字符设备通过字符设备文件来存取 ls ... -
字符设备驱动程序学习笔记三
2013-04-04 14:03 719memdev.h文件示例代码如下: #ifndef _MEM ... -
字符设备驱动程序学习笔记四
2013-04-05 11:12 549竟争与互斥 程序调试 1 ... -
GPIO学习笔记
2013-04-14 19:50 766用汇编点亮一个led 1看原理图GPK4=0,led亮G ... -
系统时钟学习笔记
2013-05-04 21:59 79412m晶振----->pll------>cpu ... -
UART学习笔记
2013-05-04 22:00 1081串口(UART) DIV_VAL=(PCLK/(bpsx1 ... -
linux内存管理学习笔记
2013-03-12 20:50 9851 linux内存管理 地址类型 物理地址 出现在cpu地址 ... -
嵌入式linux系统学习笔记
2013-03-06 21:39 879嵌入式linux内核制作 1 清除原有配置文件与中间文件 x8 ... -
原理图学习笔记一
2013-02-17 22:24 364画个草图也挺过瘾 -
进程间通信学习笔记二(信号通信)
2013-02-16 21:39 722信号通信 用户按某些键时,产生信号 硬件异常产生信号 进程用k ... -
进程间通信学习笔记三(共享内存通信)
2013-02-16 21:40 571共享内存通信 被多个进程共享的一部分物理内存,是进程间共享数据 ...
相关推荐
学习笔记信号量和进程间通信
笔记_UNIX环境网络编程卷二进程间通信_中文第二版
编程高手 笔记 通过管道与子进程通信 编程高手 笔记 通过管道与子进程通信 编程高手 笔记 通过管道与子进程通信 编程高手 笔记 通过管道与子进程通信
MFC教程lesson 17-进程间通信.rar。 包含课程内容、视频、PPT、笔记。内容详尽,MFC珍藏。
【linux学习笔记-15】基本进程通信--管道 【linux学习笔记-16】shell管道重定向程序的实现 【linux学习笔记--17】POSIX IPC——消息队列 【linux学习笔记--18】POSIX IPC——信号量 【linux学习笔记--19】POSIX IPC...
这是本人学习期间整理的关于进程间通信的笔记,和一些代码
Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习...
Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记
2023年通信学习笔记.docx
LabVIEW学习笔记LabVIEW学习笔记LabVIEW学习笔记LabVIEW学习笔记LabVIEW学习笔记LabVIEW学习笔记
【linux学习笔记-15】基本进程通信--管道.doc 【linux学习笔记-16】shell管道重定向程序的实现.doc 【linux学习笔记-1】使用GDB调试简单的用户程序.doc 【linux学习笔记-2】父子进程共享文件描述符.doc 【linux学习...
CSS学习笔记CSS学习笔记CSS学习笔记CSS学习笔记
Contiki学习笔记:启动一个进程process_start Contiki学习笔记:系统进程etimer_process Contiki学习笔记:深入理解process_run函数 Contiki 进阶学习: Contiki学习笔记:新事件产生及事件处理 Contiki学习笔记:...
C语言学习笔记C语言学习笔记C语言学习笔记C语言学习笔记
C语言学习笔记 C语言学习笔记 C语言学习笔记 C语言学习笔记
maven学习笔记maven学习笔记maven学习笔记
CCNA学习笔记 CCNA学习笔记 CCNA学习笔记
j2ee学习笔记j2ee学习笔记j2ee学习笔记j2ee学习笔记j2ee学习笔记
《ORANGE’S:一个操作系统的实现》读书笔记(二十五)进程间通信(三)文章代码
《ORANGE’S:一个操作系统的实现》读书笔记(二十四)进程间通信(二)文章代码