Hi,
I am getting a bus error when i run the following code.
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>
main()
{
int semid,retval;
semid=semget(0x20,1,IPC_CREAT|0666);
retval= semctl(semid,0,GETVAL,0);
printf("The value returned by the Getval is %d\n",retval);
semctl(semid,0,SETVAL,1);
printf("HI2\n");
retval= semctl(semid,0,GETVAL,0);
printf("The value returned by the Getval is %d\n",retval);
}
The bus error is due to this statement semctl(semid,0,SETVAL,1);
Can any one help to solve the problem.
Ur help will be appreciated.
Thanks..Jack
I believe that when you are "setting" a semaphore,
the argument (4th parameter in semctl() ) needs to be
a union...
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
} arg;
...check out sem.h for complete union def. for your system.
In which case you do...
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>
main()
{
int semid,retval;
union semun args;
semid=semget(0x20,1,IPC_CREAT|0666);
retval= semctl(semid,0,GETVAL,0);
printf("The value returned by the Getval is %d\n",retval);
args.val = 1;
semctl(semid,0,SETVAL,args);
printf("HI2\n");
retval= semctl(semid,0,GETVAL,0);
printf("The value returned by the Getval is %d\n",retval);
}