I don't understand this semaphore code - Unix

This is a discussion on I don't understand this semaphore code - Unix ; The following code is taken from http://www.cim.mcgill.ca/~franco/OpS...es/node81.html #!/usr/bin/perl -w # Get a semaphore my $sem=semget(&IPC_PRIVATE, 1, &IPC_CREAT | 0666) || die "semget: $! \n"; warn "semid= $sem\n"; # Initialize via sem signal: this is ugly, but couldn't get # semctl(..SETVAL) ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: I don't understand this semaphore code

  1. I don't understand this semaphore code

    The following code is taken from
    http://www.cim.mcgill.ca/~franco/OpS...es/node81.html

    #!/usr/bin/perl -w

    # Get a semaphore

    my $sem=semget(&IPC_PRIVATE, 1, &IPC_CREAT | 0666) || die "semget: $!
    \n";
    warn "semid= $sem\n";

    # Initialize via sem signal: this is ugly, but couldn't get
    # semctl(..SETVAL) to # work :-( maybe a bug in perl?

    semsign($sem);

    if(fork()) {
    # Parent
    my $i;
    for $i (1..10) {
    warn "Parent waits\n";
    semwait($sem);
    warn "Parent's in and sings\n";
    warn "..Freude schoner gotterfunken..\n";
    semsign($sem);
    sleep(1);

    semsign($sem);
    sleep(1);
    }
    }

    else {
    # Child
    my $i;
    for $i (1..10) {
    warn "Child waits\n";
    semwait($sem);
    warn "Child's in and sings\n";
    warn "..Va pensiero su l'ali dorate..\n";
    semsign($sem) || die "semop 2: $!\n";
    sleep(1);
    }
    exit(0);

    }

    # Reap child, remove sem, exit
    wait;
    semctl($sem, 0, &IPC_RMID, 0) || die "semctl: $!\n";
    exit;

    #
    # Constant definitions, etc, from and
    #

    sub IPC_PRIVATE {0};
    sub IPC_RMID {10};
    sub IPC_CREAT {0001000};
    sub GETVAL {5};

    sub semwait {
    my $sem=shift;
    semop($sem, pack("s3", 0, -1, 0)) || die "semw: $!\n";

    }

    sub semsign {
    my $sem=shift;
    semop($sem, pack("s3", 0, +1, 0)) || die "sems: $!\n";

    }

    And here is the output.

    [cdalten@localhost perl]$ ./sem.pl
    semid= 458756
    Child waits
    Child's in and sings
    ...Va pensiero su l'ali dorate..
    Parent waits
    Parent's in and sings
    ...Freude schoner gotterfunken..
    Child waits
    Child's in and sings
    ...Va pensiero su l'ali dorate..
    Parent waits
    Parent's in and sings
    ...Freude schoner gotterfunken..
    Child waits
    Child's in and sings
    ...Va pensiero su l'ali dorate..
    Child waits
    Child's in and sings
    ...Va pensiero su l'ali dorate..
    Parent waits
    Parent's in and sings
    ...Freude schoner gotterfunken..
    Child waits
    Child's in and sings
    ...Va pensiero su l'ali dorate..
    Child waits
    Child's in and sings
    ...Va pensiero su l'ali dorate..
    Parent waits
    Parent's in and sings
    ...Freude schoner gotterfunken..
    Child waits
    Child's in and sings
    ...Va pensiero su l'ali dorate..
    Child waits
    Child's in and sings
    ...Va pensiero su l'ali dorate..
    Parent waits
    Parent's in and sings
    ...Freude schoner gotterfunken..
    Child waits
    Child's in and sings
    ...Va pensiero su l'ali dorate..
    Child waits
    Child's in and sings
    ...Va pensiero su l'ali dorate..
    Parent waits
    Parent's in and sings
    ...Freude schoner gotterfunken..
    Parent waits
    Parent's in and sings
    ...Freude schoner gotterfunken..
    Parent waits
    Parent's in and sings
    ...Freude schoner gotterfunken..
    Parent waits
    Parent's in and sings
    ...Freude schoner gotterfunken..
    Parent waits
    Parent's in and sings
    ...Freude schoner gotterfunken..
    semctl: Invalid argument
    [cdalten@localhost perl]$

    I'm really not seeing what this code is doing. Can someone please
    clarify.

    Chad


  2. Re: I don't understand this semaphore code

    Basically, it just calls fork() to create two processes - a parent
    process and a child process. Then it uses semaphores to serialize
    output between the two processes.

    Semaphores are a form of interprocess serialization - in other words,
    if you have two processes and you want one thing to happen before
    another thing, you can use semaphores to make one process wait until
    it can get access to the semaphore. If you rewrote the same program
    above without using semaphores, the output you see would be in a more
    or less random order. The parent might "sing" before the child, and
    you'd have no serialization between the two processes.


  3. Re: I don't understand this semaphore code

    On Apr 7, 6:49 pm, chsal...@gmail.com wrote:
    > Basically, it just calls fork() to create two processes - a parent
    > process and a child process. Then it uses semaphores to serialize
    > output between the two processes.
    >
    > Semaphores are a form of interprocess serialization - in other words,
    > if you have two processes and you want one thing to happen before
    > another thing, you can use semaphores to make one process wait until
    > it can get access to the semaphore. If you rewrote the same program
    > above without using semaphores, the output you see would be in a more
    > or less random order. The parent might "sing" before the child, and
    > you'd have no serialization between the two processes.



    Maybe I'm not thinking this through. However, the output I see from
    the original code seems to be in random order.


  4. Re: I don't understand this semaphore code

    I'm not sure what you mean. A parent can never sing until the child
    is done, and vice versa. For example, because of the semaphores, you
    will never find this combination of output text:

    Child waits
    Parent waits
    Child's in and sings
    ...Va pensiero su l'ali dorate..
    Parent's in and sings
    ...Freude schoner gotterfunken..


  5. Re: I don't understand this semaphore code

    On Apr 7, 10:28 pm, chsal...@gmail.com wrote:
    > I'm not sure what you mean. A parent can never sing until the child
    > is done, and vice versa. For example, because of the semaphores, you
    > will never find this combination of output text:
    >
    > Child waits
    > Parent waits
    > Child's in and sings
    > ..Va pensiero su l'ali dorate..
    > Parent's in and sings
    > ..Freude schoner gotterfunken..


    Child waits
    Parent waits
    Child's in and sings
    ...Va pensiero su l'ali dorate..
    Parent's in and sings
    ...Freude schoner gotterfunken..

    When you ran fork() without something like wait().

    Anyhow, couldn't I do something like:
    #!/usr/bin/perl -w

    if(fork()) {
    # Parent
    my $i;
    for $i (1..10) {
    warn "Parent waits\n";
    warn "Parent's in and sings\n";
    warn "..Freude schoner gotterfunken..\n";
    sleep(1);
    }

    }

    else {
    # Child
    my $i;
    for $i (1..10) {
    warn "Child waits\n";
    warn "Child's in and sings\n";
    warn "..Va pensiero su l'ali dorate..\n";
    sleep(1);
    }
    exit(0);

    }


    wait;
    exit;

    Without having to use semaphores?


  6. Re: I don't understand this semaphore code

    You could do that, but then there would be no synchronization. Each
    process would run in parallel, and the output would not be in any
    sequence.


+ Reply to Thread