【问题标题】:Copying Data from Shared memory segment causes seg fault in the client ( semaphores & shared memory)从共享内存段复制数据会导致客户端出现段错误(信号量和共享内存)
【发布时间】:2014-03-20 01:38:50
【问题描述】:

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

生产者和消费者的共同部分(信号量和共享内存创建):

#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;
}

谁能告诉我为什么它的段错误?我只是从内存段复制地址。可能会出什么问题?

【问题讨论】:

    标签: c synchronization shared-memory semaphore memcpy


    【解决方案1】:

    谁能告诉我为什么它的段错误?

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

    a_str = malloc(sizeof(*a_str));
    

    典型的 using-uninitialized-pointer,又名野指针,问题。


    顺便说一句,POSIX IPC API 比 System V IPC API 好。见

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

    【讨论】:

    • @saipallavi 接受或点赞是表达感谢的更好方式 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 2010-11-27
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多