Semaphore Help

Semaphore Help

Post by Brad » Tue, 14 Nov 2000 04:00:00

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...

        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){
        register int id,
       /*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;


        if((id=semget(semKey,1,PERMS|IPC_CREAT))<0) {
                printf("\nsemget() error\n");
                return -1;

                printf("\nCannot lock\n");
                printf("\nSemaphone locked");

                /*systemError("\nCannot GETVAL\n");*/
                printf("\nCannot GETVAL\n");

        if(semval==0) {
                printf("\nSemval = = 0");
                semCtlArg.val=value ;
                        systemError("Cannot SETVAL[0]");
                systemError("Cannot SETVAL");
        return id;


The operations called by semop() are as follows (I feel that I may be
having a problem with these)...

static struct sembuf operation_lock[1]={


static struct sembuf operation_unlock[1]={

static struct sembuf operation_close[1]={

static struct sembuf operation_open[1]={


Is it correct to say that "0,1,SEM_UNDO" in the above code means, 0 -
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){
                systemError("Signal - Semop Error");
                printf("\nSignal operation processed\n");


/*This is used to unlock the sem and in turn notice any other process
wanting to use the sem that the memory is now available.*/
void sem_wait(int semVal){
        int response;

                systemError("\nOperation value cannot be 0\n");
                printf("\nWait processed, semop retunred %d\n",\


Am I doing this correctly. It is not working like I think it should,
notifying the other process that the memory is available.

Thanks again,

Sent via
Before you buy.