任务是模拟仓库的工作。生产者(数量为argv[2])创建“商品”的随机数(总数为argv[1]),消费者(数量为argv[3])获取这些商品的随机数。完整代码如下。
我得到分割错误,并试图调试它,得到以下结果:
Program received signal SIGSEGV, Segmentation fault.__new_sem_init (sem=0x37, pshared=0, value=1) at sem_init.c:4444 sem_init.c: No such file or directory
我想问题出在这里接线员的地址上
sem_init(&(shared->mutex), 0, 1);
我应该如何更改代码以使其工作?
谢谢!

#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/mman.h>
#define NBUFF 10
#define MAXNTHREADS 100
#define min( a, b ) ( ( a < b) ? a : b )
int nitems, nproducers, nconsumers;

typedef struct shared_s
{
    int buff[NBUFF];
    int nput;
    int nputval;
    int nget;
    int ngetval;
    sem_t mutex, nempty, nstored;
} sharedtype;




int main(int argc, char **argv)
{
    sharedtype * shared;
    key_t key;
    int shmid, semid;
    int i, j, prodcount[MAXNTHREADS], conscount[MAXNTHREADS];

    shared = mmap(NULL, sizeof(sharedtype),
                PROT_READ | PROT_WRITE, MAP_SHARED, -1, 0);

    /* Wrong argv */
    if (argc != 4)
    {
        printf("usage: newconsumer <#items> <#producers> <#consumers>\n");
        exit(1);
    }
    nitems = atoi(argv[1]);
    nproducers = min(atoi(argv[2]), MAXNTHREADS);
    nconsumers = min(atoi(argv[3]), MAXNTHREADS);
    pid_t chpidpr [nproducers];
    pid_t chpidcon [nconsumers];
    /* initilising semaphores */
    sem_init(&(shared->mutex), 0, 1);
    sem_init(&(shared->nempty), 0, NBUFF);
    sem_init(&(shared->nstored), 0, 0);


    for (i = 0; i < nproducers; i++) {
        prodcount[i] = 0;
        switch (chpidpr[i] = fork())
        {
            case -1:
                printf("fork error");
                return 1;
            case 0:
                for (;;)
                {
                    sem_wait(&shared->nempty);
                    sem_wait(&shared->mutex);
                    if (shared->nput >= nitems)
                    {
                        sem_post(&(shared->nstored));
                        sem_post(&(shared->nempty));
                        sem_post(&(shared->mutex));
                        return 0;
                    }
                    shared->buff[(shared->nput) %NBUFF] = shared->nputval;
                    (shared->nput)++;
                    (shared->nputval)++;
                    sem_post(&(shared->mutex));
                    sem_post(&(shared->nstored));
                    prodcount[i] += 1;
                }
        }
    }
    for (i = 0; i < nproducers; i++)
    {
        printf("producer count[%d] = %d\n", i, prodcount[i]);
    }
    for (i = 0; i < nconsumers; i++) {
        conscount[i] = 0;
        switch (chpidcon[i] = fork())
        {
            case -1:
                printf("error");
                return 1;
            case 0:
                for (;;)
                {
                    sem_wait(&(shared->nstored));
                    sem_wait(&(shared->mutex));
                    if (shared->nget >= nitems)
                    {
                        sem_post(&(shared->nstored));
                        sem_post(&(shared->mutex));
                        return 0;
                    }
                    j = shared->nget % NBUFF;
                    if (shared->buff[j] != shared->ngetval)
                    {
                        printf("error: buff[%d] = %d\n", j, shared->buff[j]);
                    }
                    shared->nget++;
                    shared->ngetval++;
                    sem_post(&(shared->mutex));
                    sem_post(&(shared->nempty));
                    conscount[i] += 1;
                }
        }
    }


    for (i = 0; i < nconsumers; i++)
    {
        printf("consumer count[%d] = %d\n", i, conscount[i]);
    }
    /* destroying semaphores */
    sem_destroy(&(shared->mutex));
    sem_destroy(&(shared->nempty));
    sem_destroy(&(shared->nstored));
    exit(0);
}

最佳答案

似乎您的mmap调用失败并返回-1。你不是在检查这种情况。
我做了一个快速的加法,看起来mutex的偏移量是56,或者是0x38sharedtype的基部的偏移量。根据事故报告,sem = 0x37,如果shared->mutex,则为shared == -1的地址。
我找不到任何文档说明为什么要用mmap调用fd == -1,但我认为这可能是问题的根源,加上没有验证结果。

关于c - sem_init段错误错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23501667/

10-13 01:05