擼代碼--linux進程通訊(基於共用記憶體)

來源:互聯網
上載者:User

標籤:include   sizeof   code   space   article   記憶體   shmget   for   get   

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進程通訊(基於共用記憶體)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.