Deadlock in _pipe() vs fopen() CRT calls

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
i am using Visual studio 6.0 SP6 in windows XP SP3.
mine is console application and multithreaded. when one thread calls fopen() and at the same time if the other thread calls _pipe() then my programs hangs randomly. this hang is not observed if i use msvcrt.dll 7.0.2600.5512.
thread1 -> calls _pipe(filedes,4096,0);<br/>
thread 2 -> calls fopen with NULL device. fopen("NUL:","w");
msvcrt.dll 7.0.2600.5512 -> No deadlock<br/>
msvcrtd.dll 6.0.9782.0 -> Deadlock<br/>
libcmt.lib -> dead lock<br/>
libcmtd.lib -> Dead lock
below is the sample code to explain the issue.
is this problem known and solved in latest CRT ? i tried the same code in VS2008. there is no deadlock.
<div style="color:Black;background-color:White; <pre>
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>


<span style="color:Blue; char *WrapmyName = <span style="color:#A31515; "PipeDeadLockTestAPP";


#ifdef WRAPPRINT

#define printf Wrapprintf
#define fprintf Wrapfprintf

<span style="color:Blue; void Wrapprintf(<span style="color:Blue; char *format, ...);
<span style="color:Blue; void Wrapfprintf(FILE *file, <span style="color:Blue; const <span style="color:Blue; char * format, ...);

#endif


<span style="color:Blue; unsigned __stdcall threadProc(<span style="color:Blue; void *threadStructval);
BOOL bFlag = FALSE;

main(<span style="color:Blue; int argc,<span style="color:Blue; char* argv,<span style="color:Blue; char* env)
{
FILE *fp = NULL;
HANDLE* hThread = NULL;
<span style="color:Blue; int i = 0;

fprintf(stdout,<span style="color:#A31515; "Main Thread Running Infinitely using fprintfn");
printf(<span style="color:#A31515; "Main Thread Running Infinitely using printfn");

hThread = (HANDLE)_beginthreadex(NULL,0,threadProc,NULL,0,NULL);

<span style="color:Blue; while(!bFlag)
{
fp = fopen(<span style="color:#A31515; "MyFile.txt", <span style="color:#A31515; "a");
<span style="color:Blue; if( fp)
{
fprintf(fp, <span style="color:#A31515; "%s %d %s %d %d %d %d %sn",
fp->_ptr, fp->_cnt, fp->_base, fp->_flag, fp->_file, fp->_charbuf, fp->_bufsiz, fp->_tmpfname);
fclose(fp);
}
}
}

<span style="color:Blue; unsigned __stdcall threadProc(<span style="color:Blue; void *threadStructval)
{
<span style="color:Blue; int hpipe[2];
<span style="color:Blue; int i, ret;

FILE *tempFp1 = NULL;

tempFp1 = fopen(<span style="color:#A31515; "MyTempFile.txt", <span style="color:#A31515; "w");

printf(<span style="color:#A31515; "threadProc started using printfn");
fprintf(stdout,<span style="color:#A31515; "threadProc started using fprintfn");

<span style="color:Blue; for(i=0; i<2000; i++)
{
ret = _pipe( hpipe, 10, O_BINARY );
<span style="color:Blue; if( ret == 0 )
{
fprintf(tempFp1, <span style="color:#A31515; "read handle = %u, write handle = %un", hpipe[0], hpipe[1]);
close(hpipe[0]);
close(hpipe[1]);
}
}

<span style="color:Blue; if(tempFp1)
{
fclose(tempFp1);
}

printf(<span style="color:#A31515; "threadProc started End using printfn");
fprintf(stdout,<span style="color:#A31515; "threadProc End using fprintfn");

bFlag = TRUE;
}

<span style="color:Blue; void Wrapprintf(<span style="color:Blue; char *format, ...)
{
va_list ap;
<span style="color:Blue; char *fmt;
FILE *fp;
<span style="color:Green; /* int len;*/
<span style="color:Blue; char temp[512];
<span style="color:Blue; char ttyFile[32];

fprintf(stdout,<span style="color:#A31515; "%s : %d. Enter Wrapprintf(myName:%s)n",__FILE__,__LINE__,WrapmyName);

memset(ttyFile,0, <span style="color:Blue; sizeof(ttyFile));
sprintf(ttyFile,<span style="color:#A31515; "/tmp/%s.tty",WrapmyName);

<span style="color:Blue; if ((fp = fopen(ttyFile, <span style="color:#A31515; "r")) == (FILE*)NULL)
{
<span style="color:Blue; if ((fp = fopen(<span style="color:#A31515; "NUL:",<span style="color:#A31515; "w")) == (FILE*)NULL)
<span style="color:Blue; return;

fprintf(stdout,<span style="color:#A31515; "opened (\/dev\/null)n");
}
<span style="color:Blue; else
{
<span style="color:Blue; if (fscanf(fp, <span style="color:#A31515; "%s", temp) != 1)
{
<span style="color:Blue; if ((fp = fopen(<span style="color:#A31515; "NUL:",<span style="color:#A31515; "w")) == (FILE*)NULL) <span style="color:Blue; return;

fprintf(stdout,<span style="color:#A31515; "opened (/dev/null)n");
}
<span style="color:Blue; else
{
fclose(fp);

<span style="color:Green; /*-----------------------------------------------*
* if ((fp = fopen(temp, "w")) == (FILE*)NULL) {
*-----------------------------------------------*/
<span style="color:Blue; if ((fp = fopen(temp, <span style="color:#A31515; "a")) == (FILE*)NULL)
{
<span style="color:Blue; if ((fp = fopen(<span style="color:#A31515; "NUL:",<span style="color:#A31515; "w")) == (FILE*)NULL)
<span style="color:Blue; return ;
fprintf(stdout,<span style="color:#A31515; "opened (/dev/null)n");
}

fprintf(stdout,<span style="color:#A31515; "opened (%s)n",temp);
}
}

va_start(ap, format);
fmt = format;

<span style="color:Green; /***************************
* vfprint(stderr, fmt, ap);
**************************/
vfprintf(fp, fmt, ap);

va_end(ap);

fclose(fp);

}


<span style="color:Blue; void Wrapfprintf(FILE *file, <span style="color:Blue; const <span style="color:Blue; char * format, ...)
{
va_list ap;
<span style="color:Blue; char *fmt;
FILE *fp;
<span style="color:Green; /* FILE *dummy;*/
<span style="color:Green; /* int len;*/
<span style="color:Blue; char temp[512];
<span style="color:Blue; char ttyFile[32];


sprintf(ttyFile,<span style="color:#A31515; "/tmp/%s.tty",WrapmyName);

<span style="color:Blue; if ((fp = fopen(ttyFile, <span style="color:#A31515; "r")) == (FILE*)NULL) {
<span style="color:Blue; if ((fp = fopen(<span style="color:#A31515; "NUL:",<span style="color:#A31515; "w")) == (FILE*)NULL) <span style="color:Blue; return;
}
<span style="color:Blue; else {

<span style="color:Blue; if (fscanf(fp, <span style="color:#A31515; "%s", temp) != 1) {
<span style="color:Blue; if ((fp = fopen(<span style="color:#A31515; "NUL:",<span style="color:#A31515; "w")) == (FILE*)NULL) <span style="color:Blue; return;
}
<span style="color:Blue; else {
fclose(fp);

<span style="color:Green; /*--------------------------------------------*
* if ((fp = fopen(temp, "w")) == (FILE*)NULL)
*--------------------------------------------*/
<span style="color:Blue; if ((fp = fopen(temp, <span style="color:#A31515; "a")) == (FILE*)NULL)
<span style="color:Blue; if ((fp = fopen(<span style="color:#A31515; "NUL:",<span style="color:#A31515; "w")) == (FILE*)NULL) <span style="color:Blue; return ;
}
}

va_start(ap, file);

fmt = va_arg(ap, <span style="color:Blue; char *);
vfprintf(fp, fmt, ap);

va_end(ap);

fclose(fp);
}



[/code]


View the full article
 
Back
Top