Semaphore Segmentation Fault

When I execute the first 4 lines of code , it works fine. But the output gives a segmentation fault on executing the sem_getvalue() function. I looke up everywhere for the syntax and other mistakes but I am not being able to find out whats wrong with the code. Can anyone please help me on that...??

char sem_fn[] = "my_sem";
sem_t * sem_des;
int* value;
sem_des = sem_open(sem_fn, O_CREAT | O_EXCL, 0666, 30);

sem_getvalue(sem_des,value);

Is your sem_open call returning a valid semaphore value? Are you checking for that? For all you know the sem_open is returning SEM_FAILED.

hi I even tried out using

x=sem_getvalue(sem_des,value);

and tried printing the value of x
but the execution does not reach there..It gives a segmentation fault immediately after the sem_open function.

I also put in the check

if(sem_des == (void*)-1){
perror("sem_open failure");
printf("ERROR\n");
exit(1);
}

after sem_open() and printed
printf("%d\n",(int*)sem_des);

which prints the value as zero...but it fails as soon as it reaches the sem_getvalue()

Notice that I didn't ask you to check the return value of the sem_getvalue call, but of sem_open. How about having your program printing that?

Something like this:

sem_des=sem_open(blah,blah);
fprintf(stdout,"sem_des: %d\n",sem_des);

Now does that seem too hard?

I realised that you had asked for the return value of sem_open().Please read my next post after that in which I have mentioned that I got the return value as zero. I also got the return value as zero on using your snippet to print the value.

Since it is returning a zero, I think sem_open() seems to be working fine. But then after that 'segmentation fault' occurs. Notice that this fault doesn't occur if I comment out the sem_getvalue() line from the code.

sem_open() should ideally return the address of the semaphore whereas in this case it is returning 0 ..What can be the probable reasons for the open() call not working?

perror() might tell you.