1 /*
2 * =============================================================================3 *4 * Filename: sharememory_read.c5 *6 * Description:7 *8 * Version: 1.09 * Created: 2014年11月04日 19时52分28秒10 * Revision: none11 * Compiler: gcc12 *13 * Author: lwq (28120), scue@vip.qq.com14 * Organization:15 *16 * =============================================================================17 */
18 #include
19
20 /**********************************************************21 *实验要求: 创建两个进程,通过共享内存进行通讯。22 *功能描述: 本程序申请和分配共享内存,然后轮训并读取共享中的数据,直至23 * 读到“end”。24 *日 期: 2010-9-1725 *作 者: 国嵌26 **********************************************************/
27 #include
28 #include
29 #include
30 #include
31 #include
32 #include
33 #include
34 #include
35 #include "sharememory.h"
36
37 void read_shm(struct shared_use_st *shared_stuff);38 void write_shm(struct shared_use_st *shared_stuff);39 voiddel_shm();40
41 voidusage(){42 fprintf(stderr, "usage: %s -r|-w"
43 "-r read mode"
44 "-w write mode"
45 "", "shared_memory");46 exit(1);47 }48
49 #define READ (1)
50 #define WRITE (2)
51 #define OPTNONE (0)
52
53 //全局变量
54 void *shared_memory=(void *)0;55
56 /*
57 * 程序入口58 **/
59 int main(int argc, char **argv)60 {61 int running=RUNNING;62 struct shared_use_st *shared_stuff;63 intshmid;64 int operation=OPTNONE; /*读/写操作*/
65
66 /*-----------------------------------------------------------------------------67 * getopt start68 *----------------------------------------------------------------------------*/
69 intchoice;70 while (1)71 {72 static struct option long_options[] =
73 {74 /*Use flags like so:75 {"verbose", no_argument, &verbose_flag, 'V'}*/
76 /*Argument styles: no_argument, required_argument, optional_argument*/
77 {"version", no_argument, 0, 'v'},78 {"help", no_argument, 0, 'h'},79 {"read", no_argument, 0, 'r'},80 {"write", no_argument, 0, 'w'},81 {0,0,0,0}82 };83
84 int option_index = 0;85
86 /*Argument parameters:87 no_argument: " "88 required_argument: ":"89 optional_argument: "::"*/
90
91 choice = getopt_long( argc, argv, "vhrw",92 long_options, &option_index);93
94 if (choice == -1)95 break;96
97 switch( choice )98 {99 case 'v':100
101 break;102
103 case 'h':104 usage();105 break;106
107 case 'r':108 operation=READ;109 break;110
111 case 'w':112 operation=WRITE;113 break;114
115 case '?':116 /*getopt_long will have already printed an error*/
117 usage();118 break;119
120 default:121 /*Not sure how to get here...*/
122 returnEXIT_FAILURE;123 }124 }125 if (operation ==OPTNONE) {126 usage();127 }128 /*-----------------------------------------------------------------------------129 * getopt end130 *----------------------------------------------------------------------------*/
131
132 /*创建共享内存*/
133 shmid=shmget((key_t)1234,sizeof(struct shared_use_st),0666|IPC_CREAT);134 if(shmid==-1) {135 fprintf(stderr,"shmget failed");136 exit(EXIT_FAILURE);137 }138
139 /*映射共享内存*/
140 shared_memory=shmat(shmid,(void *)0,0);141 if(shared_memory==(void *)-1) {142 fprintf(stderr,"shmat failed");143 exit(EXIT_FAILURE);144 }145
146 printf("Memory attached at 0%08x",(int)((intptr_t)shared_memory));147
148 /*让结构体指针指向这块共享内存*/
149 shared_stuff=(struct shared_use_st *)shared_memory;150
151 /*控制读写顺序*/
152 //lwq: 使之能读取上一条消息
153 if (operation == READ && shared_stuff->written_by_you !=HADWROTE)154 shared_stuff->written_by_you=HADREAD;155
156 switch(operation) {157 caseREAD:158 read_shm(shared_stuff);159 break;160
161 caseWRITE:162 write_shm(shared_stuff);163 break;164
165 default:166 usage();167 break;168 }169
170 del_shm();171 exit(EXIT_SUCCESS);172 }173
174 //读取共享内存
175 void read_shm(struct shared_use_st *shared_stuff){176 while(1) {177 if(shared_stuff->written_by_you ==HADWROTE) {178 printf("You wrote:%s",shared_stuff->some_text);179 shared_stuff->written_by_you=HADREAD;180 if(strncmp(shared_stuff->some_text,"end",3)==0) {181 break;182 }183 }184 else{185 usleep(100000);186 }187 }188 }189
190 //写入共享内存
191 void write_shm(struct shared_use_st *shared_stuff){192 char buffer[BUFSIZ] = {0};193 while(1) {194 while(shared_stuff->written_by_you!=HADREAD); /*等待读写完成*/
195 printf("Enter some text:");196 fgets(buffer,BUFSIZ,stdin);197 strncpy(shared_stuff->some_text,buffer,TEXT_SZ); /*复制进去*/
198 shared_stuff->written_by_you=HADWROTE;199 if(strncmp(buffer,"end",3)==0) {200 break;201 }202 }203 }204
205 //删除共享内存
206 voiddel_shm(){207 /*删除共享内存*/
208 if(shmdt(shared_memory)==-1) {209 fprintf(stderr,"shmdt failed");210 exit(EXIT_FAILURE);211 }212 else{213 fprintf(stderr, "delete shared_memory: 0x%08x", (int)((intptr_t)shared_memory));214 }215 }