By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,179 Members | 2,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,179 IT Pros & Developers. It's quick & easy.

why link to DB2 'libdb2' library cause the semaphore misbehavior?

P: n/a
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.

Dec 14 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Dec 18 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.