|
Home > Archive > Unix Programming > December 2006 > why link to DB2 'libdb2' library cause the semaphore misbehavior?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
why link to DB2 'libdb2' library cause the semaphore misbehavior?
|
|
| cranfic 2006-12-14, 1:29 pm |
| Please help...
To make it simple, I have a simple test C program which calls my
semphore library functions only. (It doesn't call any DB2 function at
all). And the following is the test program:
#include "hasem.h"
#include <semaphore.h>
#include <stdio.h>
int
main(int argc, char **argv)
{
haSem hasem;
int val;
if (argc != 2)
{
printf("usage: semgetvalue <name>\n");
exit(1);
}
strcpy(hasem.semName, argv[1]);
//Creating binary semaphore
//if (!semCreate(&hasem))
//Creating counting semaphore
if (!semCreateCounting(&hasem,1))
{
printf("Failed to create semaphore\n");
}
if (!semGetValue(&hasem, &val))
{
printf("Failed to get semaphore value.(try 1)\n");
}
else
{
printf("value = %d(try 1)\n", val);
}
/****************/
if (!semWait(&hasem)) // sem_wait() wrapper function
{
printf("Failed to secure semaphore.\n");
}
if (!semGetValue(&hasem, &val))
{
printf("Failed to get semaphore value.(try 2)\n");
}
else
{
printf("value = %d(try 2)\n", val);
}
/****************/
if (!semPost(&hasem)) //sem_post() wrapper function
{
printf("Failed to post semaphore.\n");
}
if (!semGetValue(&hasem, &val))
{
printf("Failed to get semaphore value.(try 3)\n");
}
else
{
printf("value = %d(try 3)\n", val);
}
if (!semDestroy(&hasem))
{
printf("Failed to unlink semDestroy().\n");
}
exit(0);
}
Without link to libdb2 the result is:
(gcc -g -O2 -D_REENTRANT -Wall -D__EXTENSIONS__ -I ~qq/rxgui/inc -c -o
hursemgetvalue.o hursemgetvalue.c
gcc -g -O2 -D_REENTRANT -Wall -D__EXTENSIONS__ -o hursemgetvalue
hursemgetvalue.o ~qq/rxgui/S*/obj/libhasem.a -lrt -lposix4 )
value = 1(try 1)
value = 0(try 2)
value = 1(try 3)
But if I link to libdb2 as well, the executable runs with incorrect
behavior. And the following is the printf output:
(gcc -g -Wall -I ~qq/rxgui/inc -c -o hursemgetvalue.o hursemgetvalue.c
gcc -o hursemgetvalue hursemgetvalue.o ~qq/rxgui/S*/obj/libhasem.a
-lrt -lposix4 -R/export/home/db2eee1a/sqllib/lib -ldb2)
value = 0(try 1)
value = 0(try 2)
value = 0(try 3)
As you can see, the semaphore value is always 0 and doesn't perform
locking function as well. I don't know if it's a DB2 bug or me making
a mistake somewhere. Any help or direction is greatly appreciated.
The DB2 SDK version is 7.2.9 on Solaris 8 and I am using gcc compiler.
| |
| Knut Stolze 2006-12-18, 1:20 pm |
| cranfic wrote:
> Please help...
>
> To make it simple, I have a simple test C program which calls my
> semphore library functions only. (It doesn't call any DB2 function at
> all). And the following is the test program:
>
> #include "hasem.h"
> #include <semaphore.h>
> #include <stdio.h>
>
> int
> main(int argc, char **argv)
> {
> haSem hasem;
> int val;
>
> if (argc != 2)
> {
> printf("usage: semgetvalue <name>\n");
> exit(1);
> }
>
> strcpy(hasem.semName, argv[1]);
> //Creating binary semaphore
> //if (!semCreate(&hasem))
> //Creating counting semaphore
> if (!semCreateCounting(&hasem,1))
> {
> printf("Failed to create semaphore\n");
> }
> if (!semGetValue(&hasem, &val))
> {
> printf("Failed to get semaphore value.(try 1)\n");
> }
> else
> {
> printf("value = %d(try 1)\n", val);
> }
> /****************/
> if (!semWait(&hasem)) // sem_wait() wrapper function
> {
> printf("Failed to secure semaphore.\n");
> }
> if (!semGetValue(&hasem, &val))
> {
> printf("Failed to get semaphore value.(try 2)\n");
> }
> else
> {
> printf("value = %d(try 2)\n", val);
> }
>
> /****************/
> if (!semPost(&hasem)) //sem_post() wrapper function
> {
> printf("Failed to post semaphore.\n");
> }
> if (!semGetValue(&hasem, &val))
> {
> printf("Failed to get semaphore value.(try 3)\n");
> }
> else
> {
> printf("value = %d(try 3)\n", val);
> }
>
> if (!semDestroy(&hasem))
> {
> printf("Failed to unlink semDestroy().\n");
> }
>
> exit(0);
> }
>
> Without link to libdb2 the result is:
> (gcc -g -O2 -D_REENTRANT -Wall -D__EXTENSIONS__ -I ~qq/rxgui/inc -c -o
> hursemgetvalue.o hursemgetvalue.c
> gcc -g -O2 -D_REENTRANT -Wall -D__EXTENSIONS__ -o hursemgetvalue
> hursemgetvalue.o ~qq/rxgui/S*/obj/libhasem.a -lrt -lposix4 )
>
> value = 1(try 1)
> value = 0(try 2)
> value = 1(try 3)
>
> But if I link to libdb2 as well, the executable runs with incorrect
> behavior. And the following is the printf output:
> (gcc -g -Wall -I ~qq/rxgui/inc -c -o hursemgetvalue.o hursemgetvalue.c
>
> gcc -o hursemgetvalue hursemgetvalue.o ~qq/rxgui/S*/obj/libhasem.a
> -lrt -lposix4 -R/export/home/db2eee1a/sqllib/lib -ldb2)
>
> value = 0(try 1)
> value = 0(try 2)
> value = 0(try 3)
>
> As you can see, the semaphore value is always 0 and doesn't perform
> locking function as well. I don't know if it's a DB2 bug or me making
> a mistake somewhere. Any help or direction is greatly appreciated.
> The DB2 SDK version is 7.2.9 on Solaris 8 and I am using gcc compiler.
First question: any chance on moving to a supported DB2 version like V8 or,
better yet, V9.
What would also be interesting to know are the definitions of your various
sem* functions. The POSIX semaphore functions are not mixed case, e.g.
sem_destroy and not semDestroy.
And finally, what's the output of the linkage editor?
--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
|
|
|
|
|