G
grocery_stocker
The following code is taken from
http://www.cim.mcgill.ca/~franco/OpSys-304-427/messages/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 <sys/ipc.h> and <sys/sem.h>
#
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
http://www.cim.mcgill.ca/~franco/OpSys-304-427/messages/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 <sys/ipc.h> and <sys/sem.h>
#
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