撸代码--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;

}





  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值