撸代码--linux进程通信(基于共享内存)
1.实现亲缘关系进程的通信,父写子读
思路分析:1)首先我们须要创建一个共享内存。
2)父子进程的创建要用到fork函数。fork函数创建后,两个进程分别独立的执行。
3)父进程完毕写的内容。同一时候要保证子进程退出后,在删除共享内存。
4)子进程完毕读的内容。
效果展示:
代码展示:
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h> int main()
{//父子进程 操作共享内存
//先创建共享内存 父进程对共享内存写 子进程对共享内存读
int flag;
flag=shmget(IPC_PRIVATE,4096,0600|IPC_CREAT);
//创建一个共享内存 然后返回标示符 char buf[]={"I am your father\n"};
char s[123];
if(fork()!=0)
{//父进程完毕对共享内存的写
char *f;
f=(char *)shmat(flag,NULL,0);//连接了父进程和共享内存 返回指针 指向
//内存的第一个字节
memset(f,'\0',4096);//这时候能够操作f
strncpy(f,"I am you father",16);//写入内容 printf("parent %d Write buf is %s\n",getpid(),f);
wait(NULL);//等待子进程
shmctl(flag,IPC_RMID,0);//删除共享 内存
exit(0);
}
else
{
char *fp;
sleep(3);//让父进程有时间往里面写
fp=(char *)shmat(flag,NULL,0);//子进程跟其连接 然后返回给字符指针
printf("child pid is %d,Read buf is %s\n",getpid(),fp);
exit(0);
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2.实现非亲缘关系的通信。(两个进程对共享内存的值进行改动)
思路分析:1)首先我们须要创建一个共享内存。
2)两个进程要採取锁的方式訪问共享内存的值。
效果展示:
代码展示:
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h> //写进程不断的写 然后要推断当前内存中的锁是开闭状态
struct t
{
int user_now;//定义一个锁
int val;
}; int main()
{
int flag;
flag=shmget((key_t)1234,4096,0600|IPC_CREAT); struct t *tt;
tt=(struct t*)shmat(flag,NULL,0);//拿到内存
tt->user_now=0;
while(1)
{
if(tt->user_now==0)
{//假设0 锁开了 那么设置值
tt->val=1;
printf("I am write, the value is %d\n",tt->val); tt->user_now=1;//加上锁。 }
sleep(1);
}
shmdt((void *)tt);
return 0; }
写进程2:
<pre name="code" class="objc">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h> //写进程不断的写 然后要推断当前内存中的锁是开闭状态
struct t
{
int user_now;//定义一个锁
int val;
}; int main()
{
int flag;
flag=shmget((key_t)1234,4096,0600|IPC_CREAT); struct t *tt;
tt=(struct t*)shmat(flag,NULL,0);//拿到内存
tt->user_now=0;
while(1)
{
if(tt->user_now==1)
{//假设0 锁开了 那么设置
tt->val=2;
printf("I am write2, the value is %d\n",tt->val); tt->user_now=0;//加上锁。
}
sleep(1);
}
shmdt((void *)tt);
shmctl(flag,IPC_RMID,0);
return 0; }
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.程序间的对话(AB进程能够实现对话 仅仅能够实现A进程不断写 B进程不断读)
思路分析:1)首先我们须要创建一个共享内存。
2)建立两个进程,A,B。
A进程完毕接受键盘的输入,然后放在共享内存中。
3)B进程拿出共享内存的数据。然后显示出来。
效果展示:
代码展示:
A进程
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h> //写进程不断的写 然后要推断当前内存中的锁是开闭状态
struct t
{
int user_now;//定义一个锁
char buf[1024];
}; int main()
{
int flag;
flag=shmget((key_t)1234,4096,0600|IPC_CREAT); struct t *tt;
tt=(struct t*)shmat(flag,NULL,0);//拿到内存
tt->user_now=0;
while(1)
{
if(tt->user_now==0)
{//假设0 锁开了 那么设置值
read(STDIN_FILENO,tt->buf,1024);
//将键盘输入的放在共享内存的buf中
tt->user_now=1;
}
// memset(tt->buf,0,sizeof(tt->buf));
sleep(1);
}
shmdt((void *)tt);
return 0; }<strong>
</strong>
B进程
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h> //写进程不断的写 然后要推断当前内存中的锁是开闭状态
struct t
{
int user_now;//定义一个锁
char buf[1024];
}; int main()
{
int flag;
flag=shmget((key_t)1234,4096,0600|IPC_CREAT); struct t *tt;
tt=(struct t*)shmat(flag,NULL,0);//拿到内存
tt->user_now=0;
while(1)
{
if(tt->user_now==1)
{//假设0 锁开了 那么设置值
write(STDOUT_FILENO,tt->buf,strlen(tt->buf));
memset(tt->buf,0,sizeof(tt->buf));
tt->user_now=0;
}
sleep(1);
}
shmdt((void *)tt);
shmctl(flag,IPC_RMID,0);
return 0; }
撸代码--linux进程通信(基于共享内存)的更多相关文章
- linux 进程通信之 共享内存
共享内存是被多个进程共享的一部分物理内存.共享内存是进程间共享数据的一种最快的方法.一个进程向共享内存区域写入了数据,共享这个内存区域的全部进程就能够立马看到当中的内容. 关于共享内存使用的API k ...
- Linux 进程通信(共享内存区)
共享内存是由内核出于在多个进程间交换信息的目的而留出的一块内存区(段). 如果段的权限设置恰当,每个要访问该段内存的进程都可以把它映像到自己的私有地址空间中. 如果一个进程更新了段中的数据,其他进程也 ...
- linux进程通信之共享内存
共享内存同意两个或多个进程共享一给定的存储区,由于数据不须要来回复制,所以是最快的一种进程间通信机制.共享内存能够通过mmap()映射普通文件(特殊情况下还能够採用匿名映射)机制实现,也能够通过系统V ...
- Linux进程通信之共享内存实现生产者/消费者模式
共享内存 共享内存是内核为进程创建的一个特殊内存段,它将出现在进程自己的地址空间中,其它进程可以将同一段共享内存连接(attach)到自己的地址空间.这是最快的进程间通信方式,但是不提供任何同步功能( ...
- linux 进程学习笔记-共享内存
如果能划定一块物理内存,让多个进程都能将该内存映射到其自身虚拟内存空间的话,那么进程可以通过向这块内存空间读写数据而达到通信的目的.另外,和消息队列不同的是,共享的内存在用户空间而不是核空间,那么就不 ...
- Linux 进程通信之:内存共享(Shared Memory)(转,好文章)
https://blog.csdn.net/afei__/article/details/84188548
- Linux进程通信之System V共享内存
前面已经介绍过了POSIX共享内存区,System V共享内存区在概念上类似POSIX共享内存区,POSIX共享内存区的使用是调用shm_open创建共享内存区后调用mmap进行内存区的映射,而Sys ...
- Linux 基于IPC机制实现进程间的共享内存处理
今天学习了相关于IPC(InterProcess Communication ,进程间通信)的相关知识.就做个笔记,一来让大家检查一下我的理解方面是不是有错误,二来也为了能让更多的博友们了解到相关的知 ...
- linux进程间的通信之 共享内存
一.共享内存介绍 共享内存是三个IPC(Inter-Process Communication)机制中的一个. 它允许两个不相关的进程访问同一个逻辑内存. 共享内存是在两个正在进行的进程之间传递数据的 ...
随机推荐
- 习题:八数码难题(双向BFS)
八数码难题(wikioi1225) [题目描述] 在3×3的棋盘上,摆有八个棋子,每个棋子上标有1至8的某一数字.棋盘中留有一个空格,空格用0来表示.空格周围的棋子可以移到空格中.要求解的问题是:给出 ...
- [LOJ#2329]「清华集训 2017」我的生命已如风中残烛
[LOJ#2329]「清华集训 2017」我的生命已如风中残烛 试题描述 九条可怜是一个贪玩的女孩子. 这天她在一堵墙钉了 \(n\) 个钉子,第 \(i\) 个钉子的坐标是 \((x_i,y_i)\ ...
- shell的简单介绍
一 什么叫shell,shell 是什么 如果考虑到操作系统其实是一组软件,我们可以发现应用程序其实是在最外层,就如同鸡蛋的外壳一样,因此这个也就被称为shell. 其实shell的功能只是提供用户操 ...
- JS 监听绑定和取消事件
1. 原生 JS 语言: 绑定:addEventListener(type, function, false) 取消: removeEventListener(type, function, fals ...
- Java EE 学习(5):IDEA + maven + spring 搭建 web(1)
参考:http://www.cnblogs.com/lonelyxmas/p/5397422.html http://www.ctolib.com/docs-IntelliJ-IDEA-c--1590 ...
- 洛谷 [P3496] BLO
割点 首先 tarjan 求割点, 对于不是割点的点, 答案是 2 * (n-1) 有序,所以要乘 2 对于是割点的点, 答案是删去该点后所有连通块的个数加上 n-1 在乘 2 #include &l ...
- python实现贪吃蛇
贪吃蛇的算法还是比较简单的,蛇的移动我是通过不停添加一个head方块,然后判断应该加到蛇头的哪个方向,加完后删掉蛇尾就行了,如果吃到食物就不删蛇尾. 只是一个贪吃蛇只需要70行代码左右就可以了,后来又 ...
- HTTP 错误 401.2 - Unauthorized
最近开始拾起Asp.net方面的知识,遇到如下这个问题 HTTP 错误 401.2 - Unauthorized 由于身份验证头无效,您无权查看此页. 解决方法: >IIS管理 >功能视图 ...
- requireJs杂项
如果设置了baseUrl,那么baseUrl目录下的模块可以被加载,那么被加载之后的模块Id是什么?文件名吗? 入口函数的写法?是不是要执行,后面加() 中文网中指的优化工具是什么? ...
- [LeetCode] Trapping Rain Water 栈
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...