receiver.c 702 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/mman.h>
  5. #include <sys/shm.h>
  6. #include <sys/ipc.h>
  7. #include <string.h>
  8. #include <sys/wait.h>
  9. #define SHM_SIZE 9999
  10. int main()
  11. {
  12. int shmid;
  13. char *shmaddr;
  14. key_t key = 6666;
  15. // 测试shmget
  16. shmid = shmget(key, SHM_SIZE, 0666 | IPC_CREAT);
  17. if (shmid < 0)
  18. {
  19. perror("shmget failed");
  20. exit(EXIT_FAILURE);
  21. }
  22. // 测试shmat
  23. shmaddr = shmat(shmid, 0, 0);
  24. char read_buf[20];
  25. memcpy(read_buf, shmaddr, 14);
  26. printf("Receiver receive: %s\n", read_buf);
  27. memset(shmaddr, 0, SHM_SIZE);
  28. memcpy(shmaddr, "Reveiver Hello!", 16);
  29. shmdt(shmaddr);
  30. return 0;
  31. }