I have to program Readers-Writers Problem in C without using threads. I have used this to initialize shared semaphores:
/* initialize semaphores for shared processes */
resource = sem_open ("/rSem", 0644, O_CREAT | O_EXCL, 1);
sem_unlink ("/rSem");
mutex = sem_open ("/mSem", 0644, O_CREAT | O_EXCL, 1);
sem_unlink ("/mSem");
but while running child process
sem_wait(resource);
terminates the program.
here is my complete code:
/* Includes */
#include <unistd.h>     /* Symbolic Constants */
#include <sys/types.h>  /* Primitive System Data Types */
#include <errno.h>      /* Errors */
#include <stdio.h>      /* Input/Output */
#include <stdlib.h>     /* General Utilities */
#include <pthread.h>    /* POSIX Threads */
#include <string.h>     /* String handling */
#include <semaphore.h>  /* Semaphore */
#include <sys/mman.h>
#include <fcntl.h>          /* O_CREAT, O_EXEC          */
/* prototype for thread routine */
void writer ( int , int *);
void reader ( int , int *, int *);
void multipleFork ( int );
/* global variables */
sem_t  *mutex;
sem_t  *resource;
int main(int argc, char **argv)
{
    int * Buffer = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
    int * readcount = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
    /* initialize semaphores for shared processes */
    resource = sem_open ("/rSem", 0644, O_CREAT | O_EXCL, 1);
    sem_unlink ("/rSem");
    mutex = sem_open ("/mSem", 0644, O_CREAT | O_EXCL, 1);
    sem_unlink ("/mSem");
    pid_t pid = fork();
    if (pid == 0)
        writer(1, Buffer);
    else
        reader(3, Buffer, readcount);
    sem_destroy(resource);
    sem_destroy(mutex);
    exit(0);
} /* main() */
void writer ( int n , int * Buffer)
{
    multipleFork(n);
    sleep(1);
    while(1){
        usleep(rand()%3000);
        sem_wait(resource);
        //<CRITICAL Section>
        printf("The writer (%d) acquires the lock.\n", getpid());
        (*Buffer)++;
        printf("The writer (%d) writes the value %d.\n", getpid(), *Buffer);
        sleep(1);
        printf("The writer (%d) releases the lock.\n", getpid());
        //<CRITICAL Section>
        sem_post(resource);
        sleep(1);
    }
}
void reader( int n , int * Buffer, int * readcount) {
    multipleFork(n);
    while(1){
        usleep(rand()%3000);
        sem_wait(mutex);
        usleep(rand()%3000);
        if ((*readcount) == 0)
        {
            sem_wait(resource);
            printf("The first reader (%d) acquires the lock.%d\n", getpid(), *readcount);
            (*readcount)++;
        }
        else if ((*readcount) > 0)
        {
            printf("The reader (%d) acquires the lock.%d\n", getpid(), *readcount);
            (*readcount)++;
        }
        sem_post(mutex);
        //<CRITICAL Section>
        printf("Reader (%d) reads the value %d.%d\n", getpid(), *Buffer, *readcount);
        sleep(1);
        //<CRITICAL Section>
        sem_wait(mutex);
        (*readcount)--;
        if ((*readcount) == 0)
        {
            printf("The last reader (%d) releases the lock.\n", getpid());
            sem_post(resource);
        }
        else
            printf("The reader (%d) releases the lock.\n", getpid());
        sem_post(mutex);
        sleep(1);
    }
}
void multipleFork (int n)
{
    while(n-1 > 0){
        pid_t pid = fork();
        if (pid == 0){
            return;
        }
        else
            n--;
    }
    return;
}
 
    