Shared memory and POSIX semaphores

时间:2015-05-08 10:04:19

标签: c concurrency posix ipc

I wrote simple consumer-producer program in C. It is working fine while I have 1 producer and 1 consumer. But it is acting strange when I increase number of consumers.

  1. I start the producer process
  2. Producer is producing
  3. I start the consumer process
  4. Consumer is consuming and producer is producing
  5. I start the consumer process no 2
  6. Consumer process no2 never gets an element
  7. When I start consumer no3, no4... and so on, the same happens

Second problem:

  1. Producer produced maximum of elements
  2. Consumer consumes all elements, but producer does not continue to produce anymore

I have no idea why it is happening.

Code:

Producer:

Error: 
[$injector:unpr] http://errors.angularjs.org/1.3.15/$injector/unpr?p0=eProvider%20%3C-%20e%20%3C-%20oldPwdCheckDirective

Consumer:

<div class="form-group">
   <label for="sel1">Gender:</label>
   <select class="form-control" id="gender">
      <option>Male</option>
      <option>Female</option>
   </select>
</div>

  (function (global) {
 document.getElementById("save").addEventListener("click", function () {
 global.localStorage.setItem("mySharedData", document.getElementById("gender").value);
 }, false);
}(window));

common.h:

#include <semaphore.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <fcntl.h>
#include "common.h"

int memoryID;
struct wrapper *memory;
int rc;

void atexit_function() {
    rc = shmctl(memoryID, IPC_RMID, NULL);
    rc = shmdt(memory);
    sem_destroy(&memory->pmutex);
    sem_destroy(&memory->cmutex);
    sem_destroy(&memory->empty);
    sem_destroy(&memory->full);
}

int main(int argc, char **argv) {
    atexit(atexit_function);
    //creating key for shared memory
    srand(time(NULL));
    key_t sharedMemoryKey = ftok(".", MEMORY_KEY);
    if (sharedMemoryKey == -1) {
        perror("ftok():");
        exit(1);
    }

    memoryID = shmget(sharedMemoryKey, sizeof(struct wrapper), IPC_CREAT | 0600);
    if (memoryID == -1) {
        perror("shmget():");
        exit(1);
    }

    memory = shmat(memoryID, NULL, 0);
    if (memory == (void *) -1) {
        perror("shmat():");
        exit(1);
    }

    //initialization

    printf("Initializtaion !\n");
    memset(&memory->array, 0, sizeof(memory->array));
    sem_init(&memory->pmutex, 0, 1);
    sem_init(&memory->cmutex, 0, 1);
    sem_init(&memory->empty, 0, SIZE_OF_ARRAY);
    sem_init(&memory->full, 0, 0);
    memory->n = -1;

    if (memoryID == -1) {
        perror("shmget(): ");
        exit(1);
    }


    while(1)
    {
        int r = rand();
        sem_wait(&memory->empty);
        sem_wait(&memory->pmutex);
        memory->n++;
        (memory->array)[memory->n]=r;
        printf("Adding task\t Value:%d\tNumber of tasks waiting:%d \n",r,memory->n);
        usleep(10000);
        sem_post(&memory->pmutex);
        sem_post(&memory->full);
    }

}

0 个答案:

没有答案