非親緣程序間的共享記憶體與訊號通訊

2021-05-28 07:57:12 字數 1756 閱讀 1086

view plain

print?

/*****************************

寫入資料到共享記憶體,後再得到另乙個程序pid

向其傳送sigusr1訊號,另乙個程序

收到後退出

***************************/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define size 1024

int main(void)  

addr = shmat(shmid,null,0);  

if( addr == (void *)(-1))  

perror("shmat");  

else  

strcpy(addr+4,buf);  

pid_t pid;  

strncpy(&pid,addr,4);     

kill(pid,sigusr1);  

if(shmdt(addr) < 0)  

perror("shmdt");  

else

printf("detached share memory\n");  

return 0;  

}  

view plain

print?

/************************

先寫入自己的pid 對方寫完資料後

向 自己 傳送一sigusr1訊號,收到

訊號後,讀資料後退出.

**********************/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define size 1024

void get(int sig);  

int main(void)  

addr = shmat(shmid,null,0);  

if(addr == (void *)(-1))  

perror("shmat");  

else  

pid_t pid = getpid();  

strncpy(addr,&pid,4);  

pause();  

strcpy(buf,addr+4);  

fprintf(stdout,"get message from shm:\n%s\n",buf);  

if(shmdt(addr)<0)  

perror("shmdt");  

else

printf("detached share memory success\n");  

if (shmctl(shmid, ipc_rmid, null) == -1)  

perror("child: shmctl(ipc_rmid)\n");  

else

printf("delete shared-memory\n");  

return 0;  

}  void get(int sig)   

Linux 程序間通訊 共享記憶體 訊號量

shm write.c include include include include include include define max len 512 struct shm def union semun int init sem int sem id,int val int del sem ...

程序間的通訊 共享記憶體

在記憶體中開闢一段記憶體空間儲存資料,每次儲存的內容都會覆蓋上次的內容。由於沒有對記憶體進行格式化的修飾所以訪問速度塊效率高。from multiprocessing import value,array from multiprocessing import value,process impor...

Linux程序間的共享記憶體

什麼是共享記憶體 顧名思義,共享記憶體就是允許兩個不相關的程序訪問同乙個邏輯記憶體。共享記憶體是在兩個正在執行的程序之間共享和傳遞資料的一種非常有效的方式。不同程序之間共享的記憶體通常安排為同一段物理記憶體。程序可以將同一段共享記憶體連線到它們自己的位址空間中,所有程序都可以訪問共享記憶體中的位址,...