Hi...Need Help


 
Thread Tools Search this Thread
Top Forums Programming Hi...Need Help
# 1  
Old 03-21-2002
Hi...Need Help

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
# 2  
Old 03-21-2002
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);
}
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question