3

I use win8 Consumer preview build 8250 for executing a program, which works OK on win7 The program uses the following macros/functions:

#if defined(_WIN32)
#include <io.h>
#define streamDup(fd1) _dup(fd1)
#define streamDup2(fd1,fd2) _dup2(fd1,fd2)
#endif

static int acquireOutputStream()

{   int fd = streamDup(fileno(stdout));
    FILE* f = freopen("tmp","w",stdout); 
    return fd; }


static void releaseOutputStream(int fd)

{   fflush(stdout);
    streamDup2(fd,fileno(stdout));
    close(fd);
}

The program performs the following:

for (int i = 0; i < 1000;++i) {
   int fd = acquireOutputStream();
   printf("redirect %d\n",i);
   releaseOutputStream(fd);
   printf("test %d\n",i);
}

Every time I run it ,it prints to file tmp random number of correct "redirect j" printings : After it ,the file is empty for the remaining executions.(f pointer is never NULL in the acquireOutputStream)"test j" is always printed correctly. What could be a problem? Is it a known issue on win 8?

YAKOVM
  • 9,805
  • 31
  • 116
  • 217
  • Is Windows 8 publically available? – trojanfoe Apr 02 '12 at 08:26
  • 1
    Can you clarify what you're expecting vs what you're actually getting? Your question is a bit confusing. Are you seeing "redirect 999" in the tmp file, or "redirect 0", nothing at all, or something else? – Derek Park Apr 04 '12 at 21:01
  • 2
    This looks worryingly like [the issue reproduced here](http://qa.social.msdn.microsoft.com/Forums/en/vcgeneral/thread/9c6ecc5f-2294-479d-8827-49068c3b1802). There's a gotcha in how `fd` is being coupled and unwound by your two macros, and sounds to me like a race condition in how Windows 8 handles the code. You may want to consider an explicit pipe into your file, instead of trying to use `streamDup` to write out your log information. – MrGomez Apr 10 '12 at 05:06
  • 1
    @ MrGomez - can you give me example for "explicit pipe into your file" - I need cross-platform solution for that – YAKOVM Apr 10 '12 at 12:32
  • What makes you think that `freopen` and `_dup2` are meant to work together? – Ben Voigt Apr 10 '12 at 21:30

1 Answers1

0

There is one small issue i see with your code.

static void releaseOutputStream(int fd)

{   fflush(stdout);
    streamDup2(fd,fileno(stdout));
    close(fd);
}

In this function you do not close stdout prior to the dup2 call (fclose(stdout)).

Please add more detail to the question on exactly what you are seeing when running this code. It would help in diagnosing the issue.

NothingMore
  • 1,211
  • 9
  • 19