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进程通信(基于共享内存)的更多相关文章

  1. linux 进程通信之 共享内存

    共享内存是被多个进程共享的一部分物理内存.共享内存是进程间共享数据的一种最快的方法.一个进程向共享内存区域写入了数据,共享这个内存区域的全部进程就能够立马看到当中的内容. 关于共享内存使用的API k ...

  2. Linux 进程通信(共享内存区)

    共享内存是由内核出于在多个进程间交换信息的目的而留出的一块内存区(段). 如果段的权限设置恰当,每个要访问该段内存的进程都可以把它映像到自己的私有地址空间中. 如果一个进程更新了段中的数据,其他进程也 ...

  3. linux进程通信之共享内存

    共享内存同意两个或多个进程共享一给定的存储区,由于数据不须要来回复制,所以是最快的一种进程间通信机制.共享内存能够通过mmap()映射普通文件(特殊情况下还能够採用匿名映射)机制实现,也能够通过系统V ...

  4. Linux进程通信之共享内存实现生产者/消费者模式

    共享内存 共享内存是内核为进程创建的一个特殊内存段,它将出现在进程自己的地址空间中,其它进程可以将同一段共享内存连接(attach)到自己的地址空间.这是最快的进程间通信方式,但是不提供任何同步功能( ...

  5. linux 进程学习笔记-共享内存

    如果能划定一块物理内存,让多个进程都能将该内存映射到其自身虚拟内存空间的话,那么进程可以通过向这块内存空间读写数据而达到通信的目的.另外,和消息队列不同的是,共享的内存在用户空间而不是核空间,那么就不 ...

  6. Linux 进程通信之:内存共享(Shared Memory)(转,好文章)

    https://blog.csdn.net/afei__/article/details/84188548

  7. Linux进程通信之System V共享内存

    前面已经介绍过了POSIX共享内存区,System V共享内存区在概念上类似POSIX共享内存区,POSIX共享内存区的使用是调用shm_open创建共享内存区后调用mmap进行内存区的映射,而Sys ...

  8. Linux 基于IPC机制实现进程间的共享内存处理

    今天学习了相关于IPC(InterProcess Communication ,进程间通信)的相关知识.就做个笔记,一来让大家检查一下我的理解方面是不是有错误,二来也为了能让更多的博友们了解到相关的知 ...

  9. linux进程间的通信之 共享内存

    一.共享内存介绍 共享内存是三个IPC(Inter-Process Communication)机制中的一个. 它允许两个不相关的进程访问同一个逻辑内存. 共享内存是在两个正在进行的进程之间传递数据的 ...

随机推荐

  1. [LOJ#2329]「清华集训 2017」我的生命已如风中残烛

    [LOJ#2329]「清华集训 2017」我的生命已如风中残烛 试题描述 九条可怜是一个贪玩的女孩子. 这天她在一堵墙钉了 \(n\) 个钉子,第 \(i\) 个钉子的坐标是 \((x_i,y_i)\ ...

  2. jquery 获取被点击元素的id属性值

    有时候可能需要获取被点击元素的一些信息,此处就以id属性为例子,进行演示一下. $(document).click(function (e){ var v_id=e.target.id; consol ...

  3. 洛谷 [P3388] 割点模版

    tarjan 求无向图的割点 割点,即割去此点后原图可变为两个或多个独立的联通块 一个点 x 是割点,当且仅当存在一个x 的子节点 y ,使得 low[y] >= dfn[x] 对于根节点来说, ...

  4. linux服务器端口netstat

    netstat命令各个参数说明如下: -t : 指明显示TCP端口 -u : 指明显示UDP端口 -l : 仅显示监听套接字(所谓套接字就是使应用程序能够读写与收发通讯协议(protocol)与资料的 ...

  5. [CODEVS2035]机票打折问题

    题目描述 Description .输入机票原价(3到4位的正整数,单位:元),再输入机票打折率(小数点后最多一位数字).编程计算打折后机票的实际价格(单位:元.计算结果要将个位数四舍五入到十位数“元 ...

  6. 【Visual Studio】简单内存泄漏检测方法 解决 Detected memory leaks! 问题(转)

    原文转自 http://blog.csdn.net/u011430225/article/details/47840647 我的环境是: XP SP2.VS2003 最近在一个项目中, 程序退出后都出 ...

  7. android中提示&对话框----ProgressDialog&DatePickerDialog &TimePickerDialog&PopupWindow

    ProgressDialog(精度条对话框): 1.直接调用ProgressDialog提供的静态方法show()显示 2.创建ProgressDialog,再设置对话框的参数,最后show()出来 ...

  8. webpack 2.6.1配置笔记

    2017-09-11更新:更新到webpack 2.6.1所对应的配置,完善部分代码注释. 由于最近在vue-cli生成的webpack模板项目的基础上写一个小东西,开发过程中需要改动到build和c ...

  9. JdbcTemplate模板使用

    1.添加模板的配置文件 在spring的配置文件中加入如下代码 <bean class="org.springframework.jdbc.core.JdbcTemplate" ...

  10. 修正MYSQL错误数据的一个存储过程

    -- 添加索引 CREATE INDEX idx_STRUCTURE_ID ON t_resource_info(STRUCTURE_ID); DROP PROCEDURE IF EXISTS `P_ ...