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. 实现段落文字两端对齐的css样式

    有时候网站中的文字比较多,虽然为父元素设置了宽度,但是总是会出现两端参差不齐的情况,看起来不整齐.其实实现段落的两端对齐,只需要设置两个css 样式即可. .demo{ text-align: jus ...

  2. 求中位数为K的区间的数目

    给定一个长为 $n$ 的序列和常数 $k$,求此序列的中位数为 $k$ 的区间的数量.一个长为 $m$ 的序列的中位数定义为将此序列从小到大排序后第 $\lceil m / 2 \rceil$ 个数. ...

  3. 0-Broadcast机制原理简要介绍

    Broadcast机制简要介绍 来源: http://blog.csdn.net/luoshengyang/article/details/6730748 导语 广播机制在Android系统中,也不算 ...

  4. Mybatis Plugin插件安装破解及使用

    2018年2月更新 2018年2月份,提供一个网上比较多的一个版本V3.21版本,下载资源里面有个已整合版直接解压放入C:\Users\你的用户名\.IntelliJIdea2017.3\config ...

  5. python面试模拟真题讲解

    一.选择题(32分) 1.python不支持的数据类型有:(A) A.char B.int C.float D.list 2.x = “foo” y = 2 print(x+y)           ...

  6. 编译安装的php 安装pdo_mysql扩展(php版本5.6.29)

    1.进入扩展目录 cd /etc/php-/ext/pdo_mysql/ 注:根据自己情况做适当改变 2.执行phpize /etc/php-/scripts/phpize 3.编译 ./config ...

  7. 利用linux信号机制调试段错误(Segment fault)【转】

    转自:http://blog.csdn.net/ab198604/article/details/6164517 版权声明:本文为博主原创文章,未经博主允许不得转载. 在实际开发过程中,大家可能会遇到 ...

  8. Ubuntu中vim添加lua支持

    系统:Ubuntu 15.10/16.04 因为Ubuntu15.10系统自带vim不支持lua,所以得自己编译安装. $ sudo apt install vim-nox vim-nox可以让vim ...

  9. Virtualbox 设置虚拟机上网并和主机互通(如ping等)

    我的主机是Ubuntu12.04, 安装virtualbox虚拟了一个xp系统.把xp作为一个开发用的机器,需要上网,并且和主机以及虚拟机之间互相访问. 1. 在virtual设置界面,将xp系统的网 ...

  10. AutoResetEvent 和 ManualResetEvent 多线程应用

    AutoResetEvent 1.用于在多线程,对线程进行阻塞放行 static AutoResetEvent auth0 = new AutoResetEvent(false); static Au ...