xref: /DragonOS/user/apps/test_shm/receiver.c (revision 9621ab16ef27bc94f223e6254fafb9bb07d46d57)
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 
10 #define SHM_SIZE 9999
11 
12 int main()
13 {
14     int shmid;
15     char *shmaddr;
16     key_t key = 6666;
17 
18     // 测试shmget
19     shmid = shmget(key, SHM_SIZE, 0666 | IPC_CREAT);
20     if (shmid < 0)
21     {
22         perror("shmget failed");
23         exit(EXIT_FAILURE);
24     }
25 
26     // 测试shmat
27     shmaddr = shmat(shmid, 0, 0);
28 
29     char read_buf[20];
30     memcpy(read_buf, shmaddr, 14);
31 
32     printf("Receiver receive: %s\n", read_buf);
33 
34     memset(shmaddr, 0, SHM_SIZE);
35     memcpy(shmaddr, "Reveiver Hello!", 16);
36 
37     shmdt(shmaddr);
38 
39     return 0;
40 }