child process initialization as part of fork() -- STC

Mark Geisert mark@maxrnd.com
Tue Mar 8 08:20:00 GMT 2016


-------- note.h
#include <stdio.h>
#include <unistd.h>
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>

static void
note (const char *fmt, ...)
{
     va_list     ap;
     char        buf[160];
     int         count;
     DWORD       done;
     HANDLE      errh = GetStdHandle (STD_ERROR_HANDLE);

     va_start (ap, fmt);
     count = sprintf (buf, "%d: ", getpid ());
     count += vsprintf (&buf[count], fmt, ap);
     va_end (ap);

     WriteFile (errh, buf, count, &done, NULL);
     FlushFileBuffers (errh);
}

-------- stc.c
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>
#include "note.h"

void constructor (void) __attribute__((__constructor__));
void destructor (void) __attribute__((__destructor__));

void
constructor (void)
{
     note ("I'm a constructor\n");
}

void
destructor (void)
{
     note ("I'm a destructor\n");
}

int
main (int argc, char *argv[])
{
     int         isparent = 0;
     int         status;
     int         winfo = 0;

     note ("I'm the parent\n");
     status = fork ();
     if (status == -1)
         return errno;

     if (status)
         isparent++;
     else
         note ("I'm the child\n");

     sleep (3 + isparent);

     if (isparent) {
         status = wait (&winfo);
         if (status == -1)
             return errno;
     }

     return 0;
}

-------- compile, link, and run
~ gcc -nostdlib -pg -g -o stc stc.c build/gcrt0.o build/crt0.o 
build/libgmon.a build/libcygwin.a /c/Windows/system32/kernel32.dll -lgcc

~ strace -o stc.out --mask=debug+sigp+syscall ./stc 
5592: _monstartup called
5592: monstartup called
5592: moncontrol called
5592: profile_off called
5592: profile_on called
5592: _monstartup called  <-- this is the odd message I'm asking about
5592: I'm a constructor
5592: I'm the parent
5504: I'm the child
5504: I'm a destructor
5504: _mcleanup called
5504: moncontrol called
5504: profile_off called
5592: I'm a destructor
5592: _mcleanup called
5592: moncontrol called
5592: profile_off called

~

In the above compilation command, "build" is a symlink to 
/oss/build/x86_64-unknown-cygwin/winsup/cygwin/

I've patched several routines in Cygwin DLL and libgmon.a to call my 
note() function just to announce they've been reached and to say what pid 
they're running in.  It looks like libgmon's _monstartup() is called 
twice, which is expected, but both times in the parent's process, which is 
not expected.

..mark



More information about the Cygwin-developers mailing list