-1

I am attempting to create a program that forks child processes that sleep for a few seconds and then releases the semaphore for the parent that is waiting. I've spent days reading about semaphores and researching online but it's all very confusing. Also, every example is a large amount of code for something that seems simple, and each one is completely different.

EDIT: DERP! So, the code doesn't work. The child(ren) forks just fine but the parent is forever stuck on sem_wait. So there appears to be something wrong with how I'm implementing the semaphore.

Here's what I have so far:

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

int main(int argc, char *argv[])
{
    int j, dummy, intCast;
    sem_t s;

    if (argc < 2 || strcmp(argv[1], "--help") == 0){
        printf("Usage: sem_sync X (X is one or more ints)");
        exit(EXIT_FAILURE);
    }

    setbuf(stdout, NULL); /* Make stdout unbuffered, since we
                                           terminate child with _exit() */
    printf(" Parent started\n");

    sem_init(&s, 0, 0);
    for (j = 1; j < argc; j++) {
        switch (fork()) {
        case -1:
            printf("Error on fork %d", j);
            exit(EXIT_FAILURE);

        case 0: /* Child */
            intCast = atoi(argv[j]); // Casts the argument into an integer
            printf("Child sleeping for %d seconds\n",intCast);
            sleep(intCast);
            sem_post(&s);
            _exit(EXIT_SUCCESS);

        default: /* Parent loops to create next child */
            break;
        }
    }
    printf(" Parent is waiting\n");
    sem_wait(&s);
    printf(" Parent ready to go\n");
    exit(EXIT_SUCCESS);
}

Here's the working code with sem_open() and sem_close()

int main(int argc, char *argv[])
{
    int j, intCast;
    sem_t *s;

    printf(" Parent started\n");
    s = sem_open("file1", O_CREAT, 0600, 0);
    if (s == SEM_FAILED){
        printf("sem_open() failed. errno:\n");
    }

    for (j = 1; j < argc; j++) {
        switch (fork()) {
        case -1:
            printf("Error on fork %d", j);
            exit(EXIT_FAILURE);
        case 0: /* Child */
            intCast = atoi(argv[j]);
            printf("Child sleeping for %d seconds\n",intCast);
            sleep(intCast);
            sem_post(s);
            _exit(EXIT_SUCCESS);
        default: 
            break;
        }
    }
    printf(" Parent is waiting\n");
    for (j = 1; j < argc; j++){
        sem_wait(s);
    }
    sem_close(s);
    exit(EXIT_SUCCESS);
}
3
  • So what is the problem with this code? is this code seems long to you? Dec 8, 2014 at 6:49
  • It would be more useful if you provide a problem as the length of code does not seems an issue of concern.
    – OshoParth
    Dec 8, 2014 at 6:54
  • Sorry! I edited the post to point out the issue with the code.
    – Sidreal
    Dec 8, 2014 at 6:58

1 Answer 1

1

Use sem_open() instead of sem_init(). The semaphores you create with sem_init() are not shared across processes unless you create them in shared memory. It is much easier to just use sem_open() for what you are trying to do.

Edit: Remember that sem_open() will create a persistent semaphore that you need to remove with sem_unlink(). If there may be a stray file lying around from a previous invocation, you may want to unlink the file before you open it like this:

sem_unlink(filename);
sem_open(filename, O_CREAT, etc...);

so that you delete the named semaphore before you open it. Otherwise, if it already exists, the sem_open will open the existing semaphore and inherit whatever count is already in the semaphore.

3
  • Ok, so I changed my code to use sem_open() and now I'm having the opposite problem. The parent wakes up before the child process has a chance to sem_post().
    – Sidreal
    Dec 8, 2014 at 7:52
  • Not enough space in comments, I'll edit my original post. After switching to sem_open() it was behaving oddly because I didn't have sem_close() and the semaphore would remain through multiple executions.
    – Sidreal
    Dec 9, 2014 at 21:56
  • Use sem_unlink() to get rid of the semaphore once you are done with it. Think of it like a file that stays around even after you close your program. You may just want to put a sem_unlink() right before your sem_open() to make sure the file is deleted.
    – JS1
    Dec 9, 2014 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.