zoukankan      html  css  js  c++  java
  • 撸代码--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
    "};
            char s[123];
            if(fork()!=0)
            {//父进程完毕对共享内存的写
               char *f;
               f=(char *)shmat(flag,NULL,0);//连接了父进程和共享内存 返回指针 指向
               //内存的第一个字节
               memset(f,'',4096);//这时候能够操作f
               strncpy(f,"I am you father",16);//写入内容
    
               printf("parent %d Write buf is %s
    ",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
    ",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
    ",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
    ",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;
    
    }
    





  • 相关阅读:
    win7(windows 7)系统下安装SQL2005(SQL Server 2005)图文教程( Win7 SQL Server2005 安装教程)
    PL/SQL -->隐式游标(SQL%FOUND)
    sql%found sql%notfound sql%rowcount sql%isopen
    C# WINFORM 窗体执行ORACLE存储过程 进行增删改查 自己编写借助网络(二)
    C# WINFORM 窗体执行ORACLE存储过程 进行增删改查 自己编写借助网络
    .net 接收存储过程的返回值 。。。。
    oracle-扫盲贴:存储过程实现增删改查
    sql加一个%号是什么意思
    关于ExecuteNonQuery执行存储过程的返回值 、、实例讲解存储过程的返回值与传出参数、、、C#获取存储过程的 Return返回值和Output输出参数值
    iOS网络请求之数据解析
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6818712.html
Copyright © 2011-2022 走看看