Hi,
I was trying out (reading the man pages only) a simple
program on Sys V semaphores.
The program just gets a semaphore (creates if necessary)
and sets the value to 1.
"setting" the value gives a core dump (Bus Error) on a
SPARC SunOS (5.5.1 and 5.6).
That is,
semctl(sem, 0, SETVAL, 1) gives a Bus Error (see below)
The same code, however, works on a HP-UX machine.
What could be the reason?
Totally confused :-(
- sameer.
====
$ gcc -o sem sem.c
$ sem 67
Semaphore ID: 65540
Setting Semaphore 65540 value to 1...Bus Error (core dumped)
$ ipcs -s
IPC status from <running system> as of Thu Sep 7 22:58:52 2000
T ID KEY MODE OWNER GROUP
Semaphores:
s 65540 0x00000043 --ra------- schabung eng
$ cat sem.c
/* sem.c
*
* Illustrates the use of semaphores.
*
* Usage:
* sem <key>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/ipc.h>
int
main(int argc, char **argv)
{
key_t key;
int sem;
/* error guard */
if (argc != 2) {
fprintf(stderr, "Usage: %s <key>\n", argv[0]);
return 1;
}
/* initialize the key */
key = (key_t)atoi(argv[1]); /* ok ok. I know */
/* get the semaphore */
sem = semget(key, 1, IPC_CREAT | 0600);
if (sem == -1) {
perror("Error creating semaphore");
return 1;
}
printf("Semaphore ID: %d\n", sem);
/* set the value of the semaphore */
printf("Setting Semaphore %d value to 1...", sem); fflush(stdout);
if (semctl(sem, 0, SETVAL, 1) < 0) { /* IS THIS WRONG??? */
perror("Error");
return 1;
}
printf("done\n"); fflush(stdout);
return 0;
Quote:}