I am trying to get a simple semaphore written. It is for a class
assignment and I have put allot of time into understanding it but I
seem to be missing something and I think Its the value of the
operations I perform on the semaphore. Any help is appreciated.
I have already allocated shared memory and a pointer to the shared
memory and it works fine between client and server. The semaphore I am
trying to create contains only one operation element. The following is
called from main...
clieKey=getSem1Key();
if((cliesem=sem_create(clieKey,1)) < 0)
systemError("\nCan't create client semaphore\n");
And the functions are contained in a header file.
key_t getSem1Key(){
return ftok("./",SEM1KEY);
int sem_create(key_t semKey,int value){Quote:}
register int id,
semval;
/*I populate the semCtlArg so that I can set the initial value
of the semaphore, the value variable*/
union senum
{
int val;
struct semid_ds *buf;
ushort *array;
}semCtlArg;;
if((id=semget(semKey,1,PERMS|IPC_CREAT))<0) {
printf("\nsemget() error\n");
return -1;
}
if(semop(id,&operation_lock[0],1)<0)
printf("\nCannot lock\n");
else
printf("\nSemaphone locked");
if((semval=semctl(id,1,GETVAL,0))<0)
/*systemError("\nCannot GETVAL\n");*/
printf("\nCannot GETVAL\n");
if(semval==0) {
printf("\nSemval = = 0");
semCtlArg.val=value ;
if(semctl(id,0,SETVAL,semCtlArg)<0)
systemError("Cannot SETVAL[0]");
}
if(semop(id,&operation_unlock[0],1)<0)
systemError("Cannot SETVAL");
return id;
The operations called by semop() are as follows (I feel that I may beQuote:}
having a problem with these)...
static struct sembuf operation_lock[1]={
0,1,SEM_UNDO
static struct sembuf operation_unlock[1]={Quote:};
0,-1,SEM_UNDO
static struct sembuf operation_close[1]={Quote:};
0,1,0
static struct sembuf operation_open[1]={Quote:};
0,1,SEM_UNDO
Is it correct to say that "0,1,SEM_UNDO" in the above code means, 0 -Quote:};
the index in the array, 1 - change the availability of the shared
memory, e.g. 0 is available, 1 is not. the book I have suggests I can
use -1, but for what? And the third field that can be populated with
IPC_NOWAIT and SEM_UNDO. I fell SEM_UNDO means that the sem will wait
for the shared memory to be come available where IPC_NOWAIT does not
wait and returns an error if the memory is not available.
Also if I had more than one element in this "struct sembuf array[]" and
I declare an operation above to contain more than one operation, e.g.
0,1,SEM_UNDO,1,0,SEM_UNDO what is taking place here. Does this mean
waiting for the sem to become available 0,1,SEM_UNDO and then
immediately setting the memory as unavailable to other processes
1,0,SEM_UNDO. This is all given the fact that the second value in the
settings is 0 for available and 1 for unavailable.
I have other functions...
/*This should unlock the sem allowing the sem_signal() to be called.*/
void sem_signal(int semVal){
if(semop(semVal,&operation_unlock[0],1)<0)
systemError("Signal - Semop Error");
else
printf("\nSignal operation processed\n");
/*This is used to unlock the sem and in turn notice any other processQuote:}
wanting to use the sem that the memory is now available.*/
void sem_wait(int semVal){
int response;
if((response=semop(semVal,&operation_lock[0],1))<0)
systemError("\nOperation value cannot be 0\n");
else
printf("\nWait processed, semop retunred %d\n",\
response);
Am I doing this correctly. It is not working like I think it should,Quote:}
notifying the other process that the memory is available.
Thanks again,
Brad
Sent via Deja.com http://www.deja.com/
Before you buy.