- 论坛徽章:
- 0
|
本帖最后由 parrot18 于 2011-11-02 16:52 编辑
最近遇到一个很奇怪的问题,尝试父子进程之间共享内存进行进程间通信,使用的是SYSTEM V API,shmget系列
部分源代码如下:- //下面是顺序存储队列和队列一些操作的定义,准备使用这个队列来进行进程间的信息存取
- typedef struct queue
- {
- unsigned char buffer[64][128];
- int front, rear;
- int buf_size;
- }QUEUE;
- //初始化队列
- void initQueue(QUEUE* cache)
- {
- cache->front = 0;
- cache->rear = 0;
- cache->buf_size = 64;
- int i;
- for(i=0;i<cache->buf_size;i++)
- {
- memset(cache->buffer[i], 0, cache->buf_size);
- }
- }
- //返回队首元素不改变队列状态
- int outQueue(QUEUE* cache, unsigned char* buf)
- {
- sem_wait(sem_cache);
- if(cache->front == cache->rear)
- {
- printf("the queue is empty!\n");
- return -1;
- }
- memcpy(buf, cache->buffer[(cache->front + 1) % cache->buf_size], cache->buf_size);
- sem_post(sem_cache);
- return 0;
- }
- //删除队首元素
- int delFront(QUEUE* cache)
- {
- sem_wait(sem_cache);
- if(cache->front == cache->rear)
- {
- printf("the queue is empty!\n");
- return -1;
- }
- cache->front = (cache->front + 1) % cache->buf_size ;
- memset(cache->buffer[cache->front], 0, cache->buf_size);
- sem_post(sem_cache);
- return 0;
- }
- //插入元素
- int enQueue(QUEUE* cache, unsigned char* buf)
- {
- sem_wait(sem_cache);
- if(((cache->rear + 1) % cache->buf_size) == cache->front)
- {
- printf("the queue is full!\ndelete first node\n");
- cache->front = (cache->front + 1) % cache->buf_size ;
- memset(cache->buffer[cache->front], 0, cache->buf_size);
- }
- cache->rear = (cache->rear + 1) % cache->buf_size;
- memcpy(cache->buffer[cache->rear], buf, cache->buf_size);
- sem_post(sem_cache);
- return 0;
- }
- //主程序利用信号灯进行共享内存读写的同步
- int main()
- {
-
- /*创建信号灯*/
- sem_cache=sem_open(SEM_NAME,O_CREAT,0666,1);
- sem_close(sem_cache);
- sem_unlink(SEM_NAME);
- sem_cache=sem_open(SEM_NAME,O_CREAT,0666,1);//信号灯初始值为1
-
- int flag = 0;
- sem_getvalue(sem_cache, &flag);//问题1:如果不执行这步操作,信号灯初值居然为0...这段信号灯代码用了N年了,就今天这次出问题,不知是为什么。。。
- printf("sem:%d\n", flag);
- unsigned char send_buf[128], read_buf[128];
- memset(send_buf, 0, 128);
- memset(read_buf, 0, 128);
- QUEUE* app_cache;
- int shmid;
- shmid = shmget((key_t)118, 8192, IPC_CREAT|0666);
- if(shmid == -1)
- {
- perror("shmget");
- return -1;
- }
- app_cache = (QUEUE* )shmat(shmid, NULL, 0);
- if(app_cache == NULL)
- {
- perror("shmat");
- return -1;
- }
- initQueue(app_cache);
- send_buf[0] = 252;
- send_buf[1] = 51;
- send_buf[2] = 53;
- send_buf[3] = 49;
- send_buf[4] = 54;
- send_buf[5] = 57;
- send_buf[6] = 51;
- send_buf[7] = 48;
- send_buf[8] = 51;
- send_buf[9] = 49;
- send_buf[10] = 49;
- send_buf[11] = 49;
- send_buf[12] = 48;
- send_buf[13] = 55;
- send_buf[14] = 51;
- send_buf[15] = 52;
- send_buf[16] = 0;
- send_buf[17] = 21;
- send_buf[18] = 6;
- send_buf[19] = 0;
- send_buf[20] = 214;
- send_buf[21] = 13;
- send_buf[22] = 0;
- send_buf[23] = 158;
- send_buf[24] = 52;
- send_buf[25] = 7;
- send_buf[26] = 0;
- send_buf[27] = 160;
-
- enQueue(app_cache, send_buf);//插入信息
- enQueue(app_cache, send_buf);//插入信息
- outQueue(app_cache, read_buf);//读取信息但是队列状态不变
- delFront(app_cache);//删除头结点
- printf("%d\n",app_cache->rear - app_cache->front);//打印队首队尾信息,不论这句话出现在哪儿..只要一访问app_cache->front,它的值就变了!!...正常此处应该是2-1 = 1 访问完之后 front 应该是1 rear是2 但是...gdb调试打印出来的front是2069..有时候甚至是几万几千...然后之后的enQueue操作就有可能溢出、段错误...很郁闷,不论在哪里,只要一访问完front,front的值立马就变了...哪怕只是打印一下...
- shmctl(shmid, IPC_RMID, 0);
- }
复制代码 请大家帮忙指点指点这是为什么。。。谢谢。。 |
|