delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1997/12/04/18:20:13

From: ert AT cit DOT org DOT by (Andrew Lipnitsky)
Subject: Re: gethostby... broken when forked
4 Dec 1997 18:20:13 -0800 :
Message-ID: <3486D7E4.2EB64B2A.cygnus.gnu-win32@cit.org.by>
References: <34862B09 DOT 3FFE AT dtv DOT net>
Mime-Version: 1.0
To: Jeff Smart <jeff AT dtv DOT net>
Cc: gnu-win32 AT cygnus DOT com

Hi Jeff!!!
Your program work fine for me [WinNT4.0 SP3 + gnuwin b18]
but it does nothing because the parent
process exit before child can do something.
The following source determines host properly (at leas for me).

#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main(argc, argv)
int argc;
char *argv[];
{
  struct hostent *hostptr; /* Internet host struct from netdb.h */
  char hostname[256];
  int childpid;

  strcpy(hostname, argv[1]);
  printf("PARENT STARTED PID=%d\n", getpid());

  if ((childpid = fork()) < 0) {
    printf("fork failed\n");
    exit(0);

  } else if (childpid == 0) {           /* start child process */
    printf("CHILD STARTED PID=%d\n", getpid());
    hostptr = gethostbyname(hostname);  /* BOMBS HERE */
    printf("hostptr = %p\n hostname=%s\n", hostptr,hostptr->h_name);
    exit(0);

  } else sleep(100);   //  !!! parent process wait for child
}

-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request AT cygnus DOT com" with one line of text: "help".

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019