1. 程式人生 > >擼代碼--linux進程通信(基於共享內存)

擼代碼--linux進程通信(基於共享內存)

-- log pac 字符指針 clas fcn eno csdn printf

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進程通信(基於共享內存)