using thread-safe library code without -lpthreads

using thread-safe library code without -lpthreads

Post by Thomas Haeberl » Tue, 17 Oct 2000 04:00:00



Hi there




> gethostbyname_r() should be useable from both threaded and non-threaded
> programs.

do have any experinces whith the use of gethotbyname_r ?

Maybe i'm too dumb, but i don't get any good results, which
i was having with the use of gethostbyname().

This is my current usage:

from the function parameters is defined char *hostname.

struct hostent *host;
struct hostent **hstres;
char *buffer;
int *myerrno;
int rc2;

rc2=gethostbyname_r(hostname,host,buffer,sizeof(buffer),hstres,myerrno);

The only parameter which is set is the hostname. And i'm not getting
any good results whith it.

it works "fine" when i'm using it with the old func.
But this giving me headache when i have more then one thread using
the function.

when i'm trying to access host->h_addr i'm getting a memory access
failure.

what have i to do that i have the correct values, which i normaly
have from host->h_addr and host->h_lenght ?  

host = gethostbyname(hostname)
there i can then access the hostent fields normal.
to copy the adress of the host.

see ya
Thomas

 
 
 

using thread-safe library code without -lpthreads

Post by c.. » Wed, 18 Oct 2000 11:16:20



+ This is my current usage:
+
+ from the function parameters is defined char *hostname.
+
+ struct hostent *host;
+ struct hostent **hstres;
+ char *buffer;
+ int *myerrno;
+ int rc2;
+
+ rc2=gethostbyname_r(hostname,host,buffer,sizeof(buffer),hstres,myerrno);

the problem is that you havent allocated the memory for your pointers
host, buffer and myerrno.  you probably also need to allocate the memory
for hstres but my man page for gethostbyname_r (on solaris) doesnt
make any mention of it so i cant say for certain but its fairly likely,
hth,
c

 
 
 

using thread-safe library code without -lpthreads

Post by Kenneth Ch » Wed, 18 Oct 2000 12:46:32




>+ This is my current usage:
>+
>+ from the function parameters is defined char *hostname.
>+
>+ struct hostent *host;
>+ struct hostent **hstres;
>+ char *buffer;
>+ int *myerrno;
>+ int rc2;
>+
>+ rc2=gethostbyname_r(hostname,host,buffer,sizeof(buffer),hstres,myerrno);

>the problem is that you havent allocated the memory for your pointers
>host, buffer and myerrno.  you probably also need to allocate the memory
>for hstres but my man page for gethostbyname_r (on solaris) doesnt
>make any mention of it so i cant say for certain but its fairly likely,

Yes, unfortunately, the parameters are different for Linux.
Furthermore, I can't seem to find the man page for it on Linux.  And
even furthermore, gethostbyname_r() has given me problems before on
Linux when called concurrently.  I ended up wrapping it with a mutex,
since performance was not an issue for me.  See:

    http://www.deja.com/[ST_rn=ps]/qs.xp?ST=PS&svcclass=dnyr&firstsearch=yes&preserve=1&QRY=&defaultOp=AND&DBS=1&OP=dnquery.xp&LNG=english&subjects=gethostbyname+%26+threads&groups=*linux*&authors=&fromdate=&todate=&showsort=subject&maxhits=100

As to the parameters, this is what I used, which seems to work:

    #include <pthread.h>
    #include <netdb.h>
    #include <assert.h>
    #include <stdio.h>

    int
    main(int argc, char *argv[]) {

        char buf[1024];
        struct hostent he, *hep2;
        int hen, ec;

        static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

        assert(argc == 2);

        pthread_mutex_lock(&lock);
        ec = gethostbyname_r(argv[1], &he, buf, sizeof(buf), &hep2, &hen);
        pthread_mutex_unlock(&lock);

        assert(ec == 0 && hen == 0);
        /* Sanity check, since I couldn't find the man page for it. */
        assert(hep2 == &he);
        printf("%s\n", he.h_name);

        return 0;
    }

 
 
 

using thread-safe library code without -lpthreads

Post by c.. » Wed, 18 Oct 2000 14:12:37



+ Yes, unfortunately, the parameters are different for Linux.
+ Furthermore, I can't seem to find the man page for it on Linux.  And

me either :\

+ even furthermore, gethostbyname_r() has given me problems before on
+ Linux when called concurrently.  I ended up wrapping it with a mutex,
+ since performance was not an issue for me.  See:
+
+     http://www.deja.com/[ST_rn=ps]/qs.xp?ST=PS&svcclass=dnyr&firstsearch=yes&preserve=1&QRY=&defaultOp=AND&DBS=1&OP=dnquery.xp&LNG=english&subjects=gethostbyname+%26+threads&groups=*linux*&authors=&fromdate=&todate=&showsort=subject&maxhits=100

eek! thanks for the ref, i didnt know all that,
cheers,
c

 
 
 

using thread-safe library code without -lpthreads

Post by Thomas Haeberl » Fri, 20 Oct 2000 04:00:00




> + Yes, unfortunately, the parameters are different for Linux.
> + Furthermore, I can't seem to find the man page for it on Linux.  And
> me either :\

I don't think there's any man page about it.

Quote:> + even furthermore, gethostbyname_r() has given me problems before on
> + Linux when called concurrently.  I ended up wrapping it with a mutex,
> + since performance was not an issue for me.  See:

Thanks for the help, but now the program crashes when i'm
calling the routine with a wrong DNS string, the good one
works perfect, but the bad one crashed the program when i'm
calling gethostbyname_r. Don't know why. Well i'm using
the old structure, but i'm now just waiting whith the other
threads until the host is passed.

see ya
Thomas

 
 
 

1. Using thread-safe C library functions.

The very first statement should be a

    #include <pthread.h>

As this overwrits a lot of routines

We had a lot of problems with pthreads under 10.20 and a fair part
went way after only using this include EVERYWHERE

there is a very nice HP manual "Programming with threads", which
gives a lot of useful hints and do/donts (e.g. functions to avoid ...)

Jose

  josefiller.vcf
< 1K Download

2. sparc solaris7 binaries

3. Thread-safe code under Solaris JDK 1.1+

4. Futexes III : performance numbers

5. Making libraries thread-safe

6. Scribus 1.2.1 available for testing purposes

7. Solaris 8 threads: If a routine is Async-Signal-Safe is it also thread Safe?

8. Can't catch stderror with this cmd...

9. mmap, thread-safe, and signal-safe

10. Making a library thread-safe

11. Thread-safe C libraries in Solaris?

12. Thread-safe and Signal-safe

13. Using Apache resource pools for thread-safe operations