APM: Perl and C Semaphores
Brian Michalk
michalk at awpi.com
Wed Nov 27 14:13:09 CST 2002
I posted this to comp.lang.perl Maybe someone here has a suggestion?
From: michalk at awpi.com (Brian K. Michalk)
Newsgroups: comp.lang.perl
Subject: Semaphore from Perl, used in C (semget problems)
NNTP-Posting-Host: 66.134.114.52
Message-ID: <c30723e3.0211271209.e723ea5 at posting.google.com>
I have a subroutine in a perl module:
543 ################################################################
544 sub new_semaphore() {
545 my $self = shift; if($self->sd(@_)) {return undef;}
546
547 if (@_) {
548 my $s = new IPC::Semaphore(IPC_PRIVATE, 1, S_IRWXU | S_IRWXG |
S_IROTH | IPC_CREAT);
549 $self->{semaphore} = $s;
550 }
551 if (defined $self->{semaphore}) {
552 return $self->{semaphore}->id();
553 }
554 }
555 ################################################################
That is called from the perl code:
45 my $sid = $gpr->new_semaphore(1);
46 $gpr->set_executable($ARGV[0]." -m $id -s $sid -q");
Where ARGV[0] is the name of the C program to run, and the semaphore
id is passed via "-s" on the command line. My C program looks like
this:
... declarations ...
61 key_t sem_key = 0; // semaphore key
... down in the main program ...
188 main(int argc, char *argv[])
189 {
...snip...
194 union semun {
195 int val; /* value for SETVAL */
196 struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */
197 unsigned short int *array; /* array for GETALL, SETALL */
198 struct seminfo *__buf; /* buffer for IPC_INFO */
199 } arg;
...process arguments...
215 while ((argc > 1) && (argv[1][0] == '-')) {
216 switch (argv[1][1]) {
...snip...
235 case 's':
236 argv++;
237 --argc;
238 printf("processing S\n");
239 instring = argv[1]; // memory ID
240 sem_key = atoi(instring);
241 printf("sem_key: %d \n", sem_key);
242 break;
243 }
244 argv++;
245 --argc;
246 }
... finally to the semget...
253 #define PERM S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH |
S_IWOTH
254 sem_id = semget(sem_key, , PERM );
256 if (sem_id = -1) perror("semget failed--");
When I run the whole mess, I get the following:
semget failed--: No such file or directory
which is the message for ENOENT which according to the semget(2) manpage:
"No semaphore set exists for key and semflg wasn't asserting IPC_CREAT."
I am at a loss. I've tried many permutations, and trying to attach to
a semaphore listed by ipcs doesn't work either. I've even gotten a
semget() = -1 result with errno set to success, whatever that means.
More information about the Austin
mailing list