从共享内存段复制数据会导致客户端出现seg错误(信号量和共享内存)

时间:2014-03-20 01:38:50

标签: c synchronization shared-memory semaphore memcpy

我正在尝试编写一个简单的生产者和消费者程序(两个不相关的进程)。 ,共享内存&信号灯。我用信号量空&完全作为条件变量,我将数据存储到生产者的共享内存段中。并且,我尝试将数据存储到消费者的本地变量中,但这会导致seg错误。这很奇怪,我无法弄清楚发生了什么。这是代码。

制片人和制片人的共同部分消费者(信号量和共享记忆创造):

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <string.h>
#include<stdlib.h>
#include <sys/shm.h>

struct a
{
int a;
int b;
}a_s;
void wait(int semid)
{
int err,nsops=1;
struct sembuf *sops = (struct sembuf *) malloc(sizeof(struct sembuf));
sops[0].sem_num = 0;
sops[0].sem_op = -1;
sops[0].sem_flg = 0;
err=semop(semid, sops, nsops);
if(err < 0)
    printf(" unable to do the sop \n");
}

void signal(int semid)
{
int err,nsops=1;
struct sembuf *sops = (struct sembuf *) malloc(sizeof(struct sembuf));      
sops[0].sem_num = 0;
sops[0].sem_op = 1;
sops[0].sem_flg = 0;
err=semop(semid, sops, nsops);
if(err < 0)
    printf(" unable to do the sop \n");
}


int main()
{
int i, err;
int full,empty;
key_t full_key = 1234, empty_key = 5678;
int sem_flg = IPC_CREAT | 0666;
int nsems = 1;
int nsops = 2;
    int shmid;
void *string;
void *s;
int shm_key = 9999;

struct a *a_str;
/*****************************************/

empty = semget(empty_key, nsems, sem_flg);
if(empty < 0)
    printf(" failed to initialize the semaphore \n");

semctl(empty, 0, SETVAL, 1) ;
/****************************************/
full = semget(full_key, nsems, sem_flg);
if(full < 0)
    printf(" failed to initialize the semaphore \n");

semctl(full, 0, SETVAL, 0) ;    

/*****************************************/
shmid = shmget(shm_key, 30, IPC_CREAT|0666); 
if(shmid < 0) 
    printf(" unable to create shmem \n");
else
    printf(" created shm \n");

string = shmat( shmid, NULL, 0); 
if( string == (void * ) (-1))
    printf(" unable to attach the string \n");
else
    printf(" success with shmat \n");
s = string;
/******************************************/

制片人:输入数据

while(1)
{
    wait(empty);
    sleep(1);
    memcpy( string, (void *) a_str, sizeof(struct a));
    printf(" wrote the string \n");
    signal(full);
}

消费者:复制数据并显示

while(1)
{

wait(full);
printf(" after full \n");
memcpy((void *)a_str, (void *)s, sizeof(struct a));
printf(" copied the memory from string \n");
printf(" a %d b %d \n",((struct a *)a_str)->a, ((struct a *)a_str)->b);
sleep(1);
memcpy(s, string, 7);


signal(empty);
}

return 0;
}

任何人都可以让我知道它为什么会发生错误?我只是从一个内存段复制地址。什么可能出错?

1 个答案:

答案 0 :(得分:1)

  

有人可以让我知道为什么会发生错误吗?

您没有初始化a_str,这可以通过

修复
a_str = malloc(sizeof(*a_str));

典型的using -ininitialized-pointer,a.k.a。野指针,问题。


顺便说一句,POSIX IPC API优于System V IPC API。参见

  • mq_overview (7) - POSIX消息队列概述
  • sem_overview (7) - POSIX信号量概述
  • shm_overview (7) - POSIX共享内存概述