1831656 Members
2237 Online
110029 Solutions
New Discussion

Problem with Semaphores

 
SOLVED
Go to solution
rajanandhini
Advisor

Problem with Semaphores

Hi,

We are trying to access a Semaphore memory for a particular process.Below is the code that we are using. We are able to access the Semaphore and set the values successfully. But value 0 is returned when we try to use the getval function of semctl. The actual value that has to be returned is, 1. This semaphore is not used by any other program. We are not aware why it is returning this value. Any help in this regard will be greatly appreciated.

#include
#include
#include

int main()
{
int semid;
if (SemAccess(&semid, 0x321))
{
printf("Into SemCreate");
SemCreate(&semid, 0x321);
}

if (!SemAvailable(semid))
{
printf("No");
}
else
printf("Yes");

return(0);
}
int SemAccess(int *semid, int key)
{
int sem;

sem = semget(key, 1, 0666);
if (sem != -1)
{
*semid = sem;
printf("\nCan Access Sem %d\n",*semid);
}
return(sem == -1);
}


int SemCreate(int *semid, int key)
{
int sem;

sem = semget(key, 1, 0666 | IPC_CREAT | IPC_EXCL);
printf("\nSem Created%d\n", *semid);
if (sem != -1)
{
*semid = sem;
semctl(sem, 0, SETVAL, 1); /* initially released */
printf("\nSem Created%d\n", semctl(sem, 0, SETVAL, 1));
}
return(sem == -1);
}
int SemAvailable(int semid)
{
printf("\n%d\n",semctl(semid, 0, GETVAL));
return(semctl(semid, 0, GETVAL));
}

Thanks,

nan
10 REPLIES 10
Dennis Handly
Acclaimed Contributor

Re: Problem with Semaphores

You code doesn't compile with aC++, you need to fully prototype everything.

You are illegally passing the wrong type to parm 4 of semctl. You are required to pass union semun but you are passing an int.

You neglected to mention you were compiling on IPF or using PA64.

(In SemCreate you are printing the value of *semid before you initialize it.)
Dennis Handly
Acclaimed Contributor

Re: Problem with Semaphores

rajanandhini
Advisor

Re: Problem with Semaphores

<
We are compiling using IPF

<
I am really sorry that i did not assign points for all the threads. I have now assigned for the same.
rajanandhini
Advisor

Re: Problem with Semaphores

<

I have now tried to pass a union but still the semaphore is not available for use.

please help
Dennis Handly
Acclaimed Contributor

Re: Problem with Semaphores

>I have now tried to pass a union but still the semaphore is not available for use.

This worked fine for me. I did have to use "ipcrm -s" to start all over.

If this doesn't solve it, please attach your latest source.
rajanandhini
Advisor

Re: Problem with Semaphores

Please find the below code and kindly let me know where I have gone wrong.

#include
#include
#include
union samp
{
int p;
}
q;

int main()
{
int semid;
if (SemAccess(&semid, 0x321))
{
printf("Into SemCreate");
SemCreate(&semid, 0x321);
}

if (!SemAvailable(semid))
{
printf("No");
}
else
printf("Yes");

return(0);
}
int SemAccess(int *semid, int key)
{
int sem;

sem = semget(key, 1, 0666);
if (sem != -1)
{
*semid = sem;
printf("\nCan Access Sem %d\n",*semid);
}
return(sem == -1);
}


int SemCreate(int *semid, int key)
{
int sem;

sem = semget(key, 1, 0666 | IPC_CREAT | IPC_EXCL);
printf("\nSem Created%d\n", *semid);
if (sem != -1)
{
*semid = sem;
semctl(sem, 0, SETVAL, q.p); /* initially released */
printf("\nSem Created%d\n", *semid);
}
return(sem == -1);
}
int SemAvailable(int semid)
{
printf("\n%d\n",semctl(semid, 0, GETVAL));
return(semctl(semid, 0, GETVAL));
}

Thanks,
nan
Dennis Handly
Acclaimed Contributor
Solution

Re: Problem with Semaphores

>kindly let me know where I have gone wrong.

Well that's one way to get it wrong. ;-)
You were still passing an int, not the union and didn't set it to 1. And you should specify all the union members.

union semun {
int val;
struct semid_ds *buf;
ushort *array;
};

int SemCreate(int *semid, int key) {
union semun arg;
int tmp;
int sem = semget(key, 1, 0666 | IPC_CREAT | IPC_EXCL);
if (sem != -1) {
*semid = sem;
printf("Sem Created: %d\n", *semid);
#ifdef FIX
arg.val = 1;
tmp = semctl(sem, 0, SETVAL, arg); /* initially released */
#else
tmp = semctl(sem, 0, SETVAL, 1); /* initially released */
#endif
printf("Sem SETVAL: %d\n", tmp);
}
return sem == -1;
}
rajanandhini
Advisor

Re: Problem with Semaphores

Thanks a lot!!!!

You are terrific!!!

It has solved our problem..

Thanks,

nan
Dennis Handly
Acclaimed Contributor

Re: Problem with Semaphores

If you are happy with your answers, you should close the thread so people know you don't need any more help.
rajanandhini
Advisor

Re: Problem with Semaphores

I have got a solution. Thanks for the assistance