linux进程间通信
来源:互联网 发布:人工智能讲座 编辑:程序博客网 时间:2024/06/07 12:17
管道:特点是只能在有父子关系的两个进程之间单向通行,使用数据流方式。
有名管道:可以在不同的进程间通信,但是仍然是单向通信
FIFO文件:进程间读写某个文件通信,FIFO文件不占用磁盘空间,仅为通信提供方便
共享内存:通过在操作系统之间映射一块内存,不同的进程访问共享的内存进行通信,优点是可以共享大量数据,缺点是需要同步机制保证数据的正确性
下面是一个使用管道的例子
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
pid_t pid;
int fd[2];
char buf[128];
pid = getpid();
printf("parentpid = %d\n",pid);
if(pipe(fd)<0){
printf("pipe creat error");
return 0;
}
pid = fork();
if(pid<0){
printf("fork error\n");
return 0;
}
if(0==pid){
printf("this is child, pid = %d\n",getpid());
close(fd[0]);
write(fd[1],"hahahaha",8);
}else{
printf("this is parent, pid = %d,child pid=%d \n",getpid(),pid);
close(fd[1]);
int readnum = read(fd[0],buf,10);
buf[readnum]='\0';
printf("%s",buf);
// wait(NULL);
}
return 0;
}
该例子有个奇怪的问题:写必须用f[1]才行,读必须用f[0]才行,反之则不行
- Linux进程间通信
- Linux进程间通信
- Linux进程间通信
- Linux进程间通信
- linux 进程间通信
- linux 进程间通信
- Linux进程间通信
- Linux进程间通信
- 【Linux】 进程间通信
- linux进程间通信
- Linux 进程间通信
- Linux进程间通信
- Linux进程间通信
- Linux 进程间通信
- linux间进程通信
- Linux进程间通信
- linux 进程间通信
- Linux进程间通信
- C#中的using.system的类
- 证书管理工具
- Internet Delay Asymmetry
- nagios niubility 漏洞
- showModalDialog方法、open方法及parentt对象、opener对象
- linux进程间通信
- understanding linux usb ehci device driver(2)
- Quartz.NET--JOB作业
- understanding linux usb ehci device driver(3)
- android条形码编解码 横向
- .NET 中SQL Server 2000、SQL Server 2005 连接字符串
- FusionCharts与jQuery-显示Chart的几种方法
- 2013年开发者需要了解的开发趋势和技能
- SQL SERVER 2005如何建立自动备份的维护计划