compiling with libpthread.a would block system()

compiling with libpthread.a would block system()

Post by Ming-Ching Tie » Tue, 15 Apr 2003 23:28:21



I need to compile my program with libpthread.a but
after that, the call to system() would block. However
compiling using shared library will not !

ie, compiling with

         gcc a.c -lpthread

run fine !

compiling with

         gcc a.c /usr/lib/pthread.a

or
         gcc a.c -static -lpthread

would blocked at the call to system().

Why ?

Sample code included. Please help !!!

------------------------cut-here---------------------------------
include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include "pthread.h"

int global = 1;

void * process(void * arg)
{
  int i;
  fprintf(stderr, "Starting process %s\n", (char *) arg);
  while( global )
  {
    for (i = 0; i < 10000; i++)
     write(1, (char *) arg, 1);
  }
  return NULL;

Quote:}

int main(void)
{
  int retcode;
  pthread_t th_a, th_b;
  void * retval;

  retcode = pthread_create(&th_a, NULL, process, (void *) "a");
  if (retcode != 0) fprintf(stderr, "create a failed %d\n", retcode);
  else fprintf(stderr, "create a succeeded %d\n", retcode);

  system( "echo 'hello world'");

  global = 0;
  retcode = pthread_join(th_a, &retval);
  if (retcode != 0) fprintf(stderr, "join a failed %d\n", retcode);
  else fprintf(stderr, "join a succeeded %d\n", retcode);
  return 0;

Quote:}

 
 
 

compiling with libpthread.a would block system()

Post by ross » Thu, 17 Apr 2003 17:11:46



> I need to compile my program with libpthread.a but
> after that, the call to system() would block. However
> compiling using shared library will not !

> ie, compiling with

>          gcc a.c -lpthread

> run fine !

> compiling with

>          gcc a.c /usr/lib/pthread.a

> or
>          gcc a.c -static -lpthread

> would blocked at the call to system().

> Why ?

> Sample code included. Please help !!!

> ------------------------cut-here---------------------------------
> include <stddef.h>
> #include <stdio.h>
> #include <unistd.h>
> #include "pthread.h"

> int global = 1;

> void * process(void * arg)
> {
>   int i;
>   fprintf(stderr, "Starting process %s\n", (char *) arg);
>   while( global )
>   {
>     for (i = 0; i < 10000; i++)
>      write(1, (char *) arg, 1);
>   }
>  return NULL;
> }

> int main(void)
> {
>   int retcode;
>   pthread_t th_a, th_b;
>   void * retval;

>   retcode = pthread_create(&th_a, NULL, process, (void *) "a");
>   if (retcode != 0) fprintf(stderr, "create a failed %d\n", retcode);
>   else fprintf(stderr, "create a succeeded %d\n", retcode);

>   system( "echo 'hello world'");

>   global = 0;
>   retcode = pthread_join(th_a, &retval);
>   if (retcode != 0) fprintf(stderr, "join a failed %d\n", retcode);
>   else fprintf(stderr, "join a succeeded %d\n", retcode);
>   return 0;
> }

First a question - why is it #include "pthread.h" and not #include
<pthread.h>?

The sort of behaviour that you describe can happen if the library in
question is being loaded twice. If it has some static data the
initialisation will get fouled up. By loading it statically, you are
forcing at least one instance. A second may be coming in another way.
What does ldd say when its statically linked - does it continue to
show a dependency on libpthread?

HTH
Brenton

 
 
 

compiling with libpthread.a would block system()

Post by M?ns Rullg? » Thu, 17 Apr 2003 17:45:12



> First a question - why is it #include "pthread.h" and not #include
> <pthread.h>?

> The sort of behaviour that you describe can happen if the library in
> question is being loaded twice. If it has some static data the
> initialisation will get fouled up. By loading it statically, you are
> forcing at least one instance. A second may be coming in another way.
> What does ldd say when its statically linked - does it continue to
> show a dependency on libpthread?

No.

--
M?ns Rullg?rd

 
 
 

compiling with libpthread.a would block system()

Post by Ming-Ching Tie » Thu, 17 Apr 2003 23:07:11



Quote:> The sort of behaviour that you describe can happen if the library in
> question is being loaded twice. If it has some static data the
> initialisation will get fouled up. By loading it statically, you are
> forcing at least one instance. A second may be coming in another way.
> What does ldd say when its statically linked - does it continue to
> show a dependency on libpthread?

> HTH
> Brenton

Haha you are answering my question with more questions. Can
I expect you to bring the discussion back to my question: -

Is there any explanation to the problem?
Is there a solution to the problem ?

 
 
 

compiling with libpthread.a would block system()

Post by ross » Fri, 18 Apr 2003 17:13:14





> > The sort of behaviour that you describe can happen if the library in
> > question is being loaded twice. If it has some static data the
> > initialisation will get fouled up. By loading it statically, you are
> > forcing at least one instance. A second may be coming in another way.
> > What does ldd say when its statically linked - does it continue to
> > show a dependency on libpthread?

> > HTH
> > Brenton

> Haha you are answering my question with more questions. Can
> I expect you to bring the discussion back to my question: -

> Is there any explanation to the problem?
> Is there a solution to the problem ?

The reason I asked the questions was that I am not able to set up the
exact same environment that you have. If the statically linked version
still shows a dependency on libpthread then the answers are as
follows:

The explanation is that libpthread has static data which doesn't get
initialised properly if its loaded twice.

The solution is don't load it twice - ie don't link pthread
statically.

Of course there may be another explanation, but i have seen the one i
described in practice.

Brenton

 
 
 

1. ld.so.cache: ../lib/libpthread.so.1 => libpthread.so.1.0.0

I installed libpthreads.
My ldconfig says :
.....
.....
/usr/local/lib:
        ../lib/libpthread.so.1 => libpthread.so.1.0.0
        libqt.so.1 => libqt.so.1.2
        libz.so.1 => libz.so.1.0.4
        libtiff.so.3 => libtiff.so.3.4.036
        libpng.so.0 => libpng.so.0.90
        libmpeg.so.1 => libmpeg.so.1.2
        libjpeg.so.6 => libjpeg.so.6.0.1
....
....
My binaries don't like that ../lib/ before libpthread.
When I create link ../lib/libpthread.so to
/usr/local/lib/libpthread.so... it works.

I don't understand this.

Could someone help me ?

I need pthreads for ObjC and GNUStep.
I use Slackware3.2beta but this happens on 3.1 also.

2. oneko

3. compiling C w/ libpthread

4. pcmcia scsi linux > 2.1.118

5. ccc compiled binary coredumps having loaded libpthread

6. SCSI cdrom "swap file out of sync...

7. kswapd blocks system when compiling koffice

8. boot probs

9. FreeBSD File System 512 byte-blocks --> 1024 byte-blocks

10. Non blocking socket blocks; says 'read would block' ?

11. compiling XFree (TinyX) for another system (cross compiling)

12. compiling libg++ (also compiling ACE) on an HPUX 10.20 system

13. system stop at "initializing system services" after compiling Kernel