- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Problem with Semaphores
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2007 01:35 AM
06-19-2007 01:35 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2007 03:42 PM
06-19-2007 03:42 PM
Re: Problem with Semaphores
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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2007 03:56 PM
06-19-2007 03:56 PM
Re: Problem with Semaphores
http://forums1.itrc.hp.com/service/forums/helptips.do?#33
http://forums1.itrc.hp.com/service/forums/pageList.do?userId=CA1482177&listType=unassigned&forumId=1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2007 04:22 PM
06-19-2007 04:22 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2007 04:19 PM
06-20-2007 04:19 PM
Re: Problem with Semaphores
I have now tried to pass a union but still the semaphore is not available for use.
please help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2007 04:38 PM
06-20-2007 04:38 PM
Re: Problem with Semaphores
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2007 04:43 PM
06-20-2007 04:43 PM
Re: Problem with Semaphores
#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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2007 04:59 PM
06-20-2007 04:59 PM
SolutionWell 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;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2007 05:20 PM
06-20-2007 05:20 PM
Re: Problem with Semaphores
You are terrific!!!
It has solved our problem..
Thanks,
nan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2007 09:15 PM
06-20-2007 09:15 PM
Re: Problem with Semaphores
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2007 10:24 PM
06-20-2007 10:24 PM