0

Hi,

I was programming with POSIX threads and I was using semaphores. All of a sudden the program didn't work anymore, and I started searching for the problem. It turns out that my first semaphore command "sem_open()" causes the problem. It returns value "0", or "SEM_FAILED", and sets errno to a random value, which gives me "Unknown error".

I tried to make a stub program with just the sem_open(), and it fails too. I'm helpless!

Here's my stub code:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

sem_t* sem;

int main(int argc, char** argv) {
    
    sem = sem_open("mysem", 1);
    if(sem == SEM_FAILED) {
        printf("Error! %d, %s\n", sem, gai_strerror(sem));
    }

    return 0;
2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by bleedi
0

The error probably appears random because you are calling gai_strerror when you should be calling strerror.

0

Actually, I solved this problem just a minute ago, it was that I missed #include <fcntl.h> from the include list.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.