Thread #1: Bug in libpthread: sem_wait succeeded on semaphore without prior sem_post

88 Views Asked by At

I'm having an error feedback from Helgrind Thread #1: Bug in libpthread: sem_wait succeeded on semaphore without prior sem_post. This is for an exercise where I'm limited in the functions I can use and I need somehow to send a signal from the child to the parent. Cannot use mutex nor signals. Is that really wrong or somehow problematic to use semaphores like the following example :

# include <unistd.h>
# include <fcntl.h>
# include <sys/stat.h>
# include <sys/wait.h>
# include <semaphore.h>
# include <stdlib.h>
# include <stdio.h>

void    *ft_incr(sem_t *sem)
{
    sleep(2);
    sem_post(sem);
    sem_close(sem);
    exit (EXIT_SUCCESS);
}

int main(void)
{
    sem_t       *sem;
    pid_t       pid;

    sem = sem_open("sem", O_CREAT, S_IRWXO, 0); 
    sem_unlink("sem");
    pid = fork();
    if (pid == 0)
        ft_incr(sem);
    printf("WAITING SEM TO BE INCREMENTED\n");
    sem_wait(sem);
    sem_close(sem);
    waitpid(pid, NULL, 0);
    printf("ALL GOOD\n");
}
0

There are 0 best solutions below