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