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. AsyncSocket

    如果需要在项目中像QQ微信一样做到即时通讯,必须使用socket通讯,本人也是刚学习,分享一下,有什么不对的地方希望大家指正 ios原生的socket用起来不是很直观,所以我用的是AsyncSocke ...

  2. [51nod 1022] 石子归并v2 [dp+四边形不等式优化]

    题面: 传送门 思路: 加强版的石子归并,现在朴素的区间dp无法解决问题了 首先我们破环成链,复制一条一样的链并粘贴到原来的链后面,变成一个2n长度的序列,在它上面dp,效率O(8n^3) 显然是过不 ...

  3. 使用caffe测试自己的图片

    第一种方法是测试批量图片,使用caffe.bin即可,首先要做的是把你的jpg图片转换为LMDB的格式,如何转换呢?用/build/tools/convert_image --resize_width ...

  4. js面向对象实现切换面板

    js面向对象的特点: 继承(inheritance):对象方法和属性的继承 多态(polymorphism):组件开发 抽象(abstract):抓住核心问题 封装(encapsulation):把功 ...

  5. [LeetCode] Sudoku Solver 解数独,递归,回溯

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  6. LeetCode OJ--Word Break II ***@

    https://oj.leetcode.com/problems/word-break-ii/ class Solution { public: unordered_set<string> ...

  7. TopCoder SRM 660 Div2 Problem 1000 Powerit (积性函数)

    令$f(x) = x^{2^{k}-1}$,我们可以在$O(k)$的时间内求出$f(x)$. 如果对$1$到$n$都跑一遍这个求解过程,时间复杂度$O(kn)$,在规定时间内无法通过. 所以需要优化. ...

  8. WEB接口测试之Jmeter接口测试自动化 (一)(初次接触)

    软件测试自动化从不同的测试阶段分类,可从下层到上层依次分为单元测试-->接口测试-->界面自动化测试. 单元测试一般有开发人员自行完成,而界面自动化测试合适的测试条件又很难达到,测试人员在 ...

  9. CentOS 6.9编译安装Python-2.7.14(python升级)

    参考 Python官网:https://www.python.org/ 阿里云 https://www.aliyun.com/jiaocheng/517192.html 一.查看CentOS版本和系统 ...

  10. java.lang.NoSuchMethodError: main Exception in thread "main" ===Exception

    java.lang.NoSuchMethodError: mainException in thread "main" 出现该异常是因为在之前我的项目中自定义了一个String类, ...