NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: PR/53998 CVS commit: src/sys/kern



The original program was incorrect; here's a fixed version that works as expected.

christos

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

static void
sem_create(sem_t **semaphore)
{
        char    name[15];

        sprintf(name, "/TEST-%d", getpid());
        printf("> %s\n", name);
        fflush(NULL);

        if ((*semaphore = sem_open(name, O_RDWR | O_CREAT | O_EXCL,
                        S_IRUSR | S_IWUSR, 0)) == SEM_FAILED)
        {
                perror("sem_open()");
                fflush(NULL);
        }

        return;
}

static void
sem_delete(sem_t **semaphore)
{
        char    name[15];

        sprintf(name, "/TEST-%d", getpid());
        printf("< %s\n", name);
        fflush(NULL);

        if (sem_close(*semaphore) != 0)
        {
                perror("sem_close");
                fflush(NULL);
        }

        printf("After sem_close()\n");
        sem_unlink(name);

        return;
}

int
main()
{
        int             pid;
        sem_t   *semaphore;

        sem_create(&semaphore);

        pid = fork();

        if (pid > 0)
        {
                printf("Parent process\n");
                sem_delete(&semaphore);
                printf("End of parent process\n");
                fflush(NULL);
                _exit(EXIT_SUCCESS);
        }

        sleep(1);

        setsid();
        pid = fork();

        if (pid > 0)
        {
                printf("End of second process\n");
                fflush(NULL);
                _exit(EXIT_SUCCESS);
        }

        printf("Daemonized process\n");
        fflush(NULL);
        sem_create(&semaphore);
        printf("Closing semaphore\n");
        fflush(NULL);
        sem_delete(&semaphore);
        printf("End\n");
        fflush(NULL);

        sleep(1);

        exit(EXIT_SUCCESS);
}



Home | Main Index | Thread Index | Old Index