这是我使用的解决方案 shmget ,它过时了,但它的确有效
shmget
主办:
#include <sys/ipc.h> #include <sys/shm.h> int main() { char output[] = "test"; int shmid=shmget(IPC_PRIVATE, sizeof(output), 0666); FILE *fp; fp = fopen("/tmp/shmid.txt", "w+"); fprintf(fp, "%d", shmid); fclose(fp); int *mem_arr; while(true) { mem_arr = (int *)shmat(shmid, NULL, 0); memcpy(mem_arr, output, sizeof(output)); shmdt(mem_arr); } return 1; }
客户:
#include <sys/ipc.h> #include <sys/shm.h> int main() { char output[] = "test"; FILE *fp; fp = fopen("/tmp/shmid.txt", "r"); int shmid; fscanf (fp, "%d", &shmid); fprintf(fp, "%d", shmid); fclose(fp); while(true) { int* shared_mem = (int *)shmat(shmid, NULL, 0); printf("data: %s\n", shared_mem); } return 1; }