1

I am working on some code which uses the pthread and semaphore libraries. Here is my code but it dose not work and I think its because of sem_init function. I am new in C and really I don't know how to use sem_init, sem_open, sem_wait and sem_post. can someone give me some advice??

#include <sys/mman.h>
#include <math.h>
#include <wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <unistd.h>
#include<fcntl.h>
sem_t sem1,sem2;

float Calculate(int a,int b)
{
    float d = ((a *a) + (b*b))/2;
    return d;
}

int main()
{
    int q;
    int i,j,x;
    float *Maddress, m, sum;

    Maddress=mmap(NULL,sizeof(float),PROT_READ|PROT_WRITE,MAP_SHARED,-1,0);

    sem_init(&sem2,0,1);
    sem_init(&sem1,0,0);
    sem_open("sem2",O_CREAT);
    sem_open("sem1",O_CREAT);


    if((q = sem_init(&sem1,1,0))!=0)
        printf("error in create\n");

    for(i=1;i<=10;i++)
    {
        j=fork();
        if(j==0)
        {

            printf("The child %d is executing\n",i);
            m=Calculate(i-1,i);
            printf("child %d calculated value: %f\n",i,m);
            sem_wait(&sem2);
            Maddress=&m;
            sem_post(&sem1);
            exit(EXIT_SUCCESS);
        }
    }           
    for(j=1;j<=10;j++)
    {
        wait(&x);
        if(WIFEXITED(x))
        {
            sem_wait(&sem1);
            sum=sum+ (*Maddress);
            sem_post(&sem2);


        }
    }
    printf("The final result is: %f \n",sum);

    sem_close(&sem1);
    sem_close(&sem2);
    return 0;
}

2 Answers 2

1

You should define your semaphores like this:

sem_t sem1, sem2;

As far as sem_init() is concerned, according to man 3 sem_init, prototype is:

int sem_init(sem_t *sem, int pshared, unsigned int value);

So you have to pass the semaphores in like this:

sem_init(&sem1, 0, 0);

or whatever your initial values need to be.

Same applies for sem_wait() and sem_post().

Sign up to request clarification or add additional context in comments.

1 Comment

I changed the code just as you said and I edited my question. After changing the code it almost works but now the second 'for' doesn't run and really I don't know why!!
0

(1) There are two kinds of POSIX semaphores - named and unnamed - and you are mixing both. Unnamed semaphores use sem_init and sem_destroy. Named use sem_open,sem_close and sem_unlink.

(2) Assuming you want unnamed semaphores, which are fine for related (i.e. forked) processes as you are doing, then you must put the semaphores into shared memory before you start forking. Currently you are putting the semaphore into process memory that won't be shared between children and parent because every child will get it own copy of memory and therefore, in effect, its own set of semaphore that do not related to any other process.

Your program will hang forever in the parent at sem_wait(&sem1) because the children will be signaling an entirely different copy of sem1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.