在生产者消费者示例中,信号量同步出错了

时间:2014-03-20 03:36:42

标签: c synchronization semaphore producer-consumer

我正在尝试编写单个生产者 - 消费者问题,如下所示。我将通过共享内存传递i的递增值。但是,我发现生产者发送的初始值丢失,生产者发送0,但消费者无法捕获0,而是从1捕获。我不明白为什么会发生这种情况。服务器将0写入共享内存,并等待为空,并且一旦客户端程序开始运行,即使在读取初始值之前,生产者也会释放一些如何的空,并且它会写入1. 0在这里丢失。我在这里指定两个进程的代码。

Semaphore_Server.c

#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 *sopwait = (struct sembuf *) malloc(sizeof(struct sembuf));
    sopwait[0].sem_num = 0;
    sopwait[0].sem_op = -1;
    sopwait[0].sem_flg = 0;
    err = semop(semid, sopwait, 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 = (struct a*)malloc(sizeof(struct a));
    /*****************************************/

    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;
    /******************************************/
    i = 0;
    while(i < 20)
    {
        wait(empty);
        a_str->a = i;
        memcpy( string, (void *) a_str, sizeof(struct a));
        printf(" wrote the string %d \n", i);
        i++;
        signal(full);
    }
}

Semaphore _client.c

#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 *sopwait = (struct sembuf *) malloc(sizeof(struct sembuf));
    sopwait[0].sem_num = 0;
    sopwait[0].sem_op = -1;
    sopwait[0].sem_flg = 0;
    err = semop(semid, sopwait, 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 = (struct a*)malloc(sizeof(struct a));
    /*****************************************/

    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;
    /******************************************/
    i = 0;
    while(i < 20)
    {
        wait(full);
        memcpy((void *)a_str, (void *)s, sizeof(struct a));
        printf(" a %d \n",((struct a *)a_str)->a);
        i++;
        signal(empty);
    }

    return 0;
}

任何人都可以告诉我为什么服务器在客户端读取一次之前写两次? 这是服务器的输出。     写了字符串0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

客户输出:     a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

1 个答案:

答案 0 :(得分:2)

  

任何人都可以告诉我为什么服务器在客户端读取一次之前写两次?

这很容易解决,请删除semctl()中对Semaphore _client.c的这两次来电。

说明:

  1. 如果服务器先运行,它会将0写入共享内存,并等待wait(empty);;但是一旦你在客户端调用semctl(empty, 0, SETVAL, 1);,服务器中的等待将返回,服务器可能会开始写1,在这种情况下,0将丢失。

  2. 如果客户端先运行,您的代码将正常运行。