blob: a609e49a70c20b96211717d008ae026a7c2e0159 [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001#include <errno.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <unistd.h>
6#include <signal.h>
7#include <time.h>
8#include <pthread.h>
9#include <sys/prctl.h>
10#include <sys/ipc.h>
11#include <sys/shm.h>
12#include <sys/types.h>
13#include <unistd.h>
14#include <assert.h>
15#include <sys/mman.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18
19/**
20 * ºê¶¨Òå
21 */
22#define SHM_TEXT_SIZE (8192)
23#define SHM_PAGE_SIZE (4096)
24#define SHM_ALLOC_ONE_PAGE_SIZE (1024)
25#define SHM_ALLOC_TWO_PAGE_SIZE (8100)
26#define SHM_ALLOC_FOUR_PAGE_SIZE (16000)
27#define SHM_MIN(x,y) ((x)<(y)?(x):(y))
28
29typedef struct
30{
31 key_t key;
32 int shmid;
33 unsigned int shm_size;
34}shm_node_t;
35
36typedef struct
37{
38 int write;
39 char data[SHM_TEXT_SIZE];
40}shm_use_remote;
41
42shm_node_t shm_node;
43
44int main (int argc, char *argv[])
45{
46 int opt;
47 int ret = 0;
48 int shmid = 0;
49 int copy_size = 0;
50 unsigned int shm_size = 0;
51 key_t key = 0;
52 char *pshm = NULL;
53 char *write_data = NULL;
54 extern char *optarg;
55 shm_use_remote *shared;
56
57 while ((opt = getopt(argc,argv,"k:d:s::")) != EOF)
58 {
59 switch (opt)
60 {
61 case 'k':
62 key = atoi(optarg);
63 break;
64 case 'd':
65 write_data = optarg;
66 break;
67 case 's':
68 shm_size = atoi(optarg);
69 break;
70 default:
71 break;
72 }
73 }
74
75 if (key == 0 || !write_data)
76 {
77 printf("param error: you can input \"shm_write -k -200 -d \"123456\" -s1024 \n");
78 printf("or \"shm_read -k -200 -d \"123456\" \n");
79 printf("-k -200: create a shm using key -200\n");
80 printf("-d \"123456\": the data write to shm: for example, if we write 123456 to shm, then can read 123456 form shm, the test case is pass! \n");
81 printf("-s1024: set the size of shm, then the default size is 1024 \n");
82 return -1;
83 }
84 if (shm_size == 0)
85 shm_size = SHM_ALLOC_ONE_PAGE_SIZE;
86
87 printf("AP write test: key is %d, shm_size is %d\n", key, shm_size);
88
89 shmid = shmget(key, shm_size, 0666|IPC_CREAT);
90 if(shmid < 0)
91 {
92 printf("shmid return error\n");
93 return -1;
94 }
95 printf("shmid %d \n",shmid);
96
97 pshm = (char *)shmat(shmid, 0, 0);
98 if (pshm == NULL)
99 {
100 printf("shmat return error\n");
101 return -1;
102 }
103
104 printf("pshm 0x%x \n",pshm);
105 shared = (shm_use_remote *)pshm;
106
107 shm_node.key = key;
108 shm_node.shm_size = shm_size;
109
110 copy_size = SHM_MIN(strlen(write_data), (SHM_TEXT_SIZE - 1));
111
112 while(1)
113 {
114 if(shared->write == 1)
115 {
116 strncpy(shared->data, write_data, copy_size);
117 printf("AP write %s to shm\n", write_data);
118 shared->write = 0;
119 break;
120 }
121 else{
122 sleep(2);
123 }
124 }
125
126 if(shmdt(pshm) < 0)
127 {
128 printf("shmdt errno %d: %s\n", errno, strerror(errno));
129 return -1;
130 }
131
132 if(shmctl(shmid, IPC_RMID, NULL) == -1)
133 {
134 printf("shmctl errno %d: %s\n", errno, strerror(errno));
135 return -1;
136 }
137
138 printf("shm write end\n");
139
140 return 0;
141}
142
143