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;

Recommended Answers

All 2 Replies

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.