sem_init分段错误错误

时间:2014-05-06 17:56:19

标签: c unix semaphore

任务是创建仓库工作的模拟。生产者(以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:44 44 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);
}

1 个答案:

答案 0 :(得分:0)

您的mmap来电似乎失败并返回-1。你没有检查这种情况。

我做了一个快速添加,看起来mutex的偏移量为56,或距离0x38的{​​{1}} sharedtype。根据崩溃报告sem = 0x37,如果shared->mutex,则为shared == -1的地址。

我找不到任何关于为什么会用mmap拨打fd == -1的文档,但我认为这可能是问题的根源,并且没有验证结果。