C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 29th, 2005, 04:05 AM
nucleo nucleo is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 1 nucleo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 24 sec
Reputation Power: 0
WriteFile() function in printf() freezes

I am writing to console from several threads using printf(). The process is synchronized and it works fine.
The problem is that sometimes some of the threads freezes (never returns ) at WriteFile() function used to implement printf() and the really strange here is that it returns and everything continues when i click with the right button (NOT THE LEFT) on the console.
Can someone help me to figure out what's going on?
Thank you very much in advance!

Code:
void Log::write( const char* msg )
{
printf( msg );
}

...

and in ...\VC98\CRT\SRC\WRITE.C

Code:
/* now define version that doesn't lock/unlock, validate fh */
int __cdecl _write_lk (
		int fh,
		const void *buf,
		unsigned cnt
		)
{
		int lfcount;			/* count of line feeds */
		int charcount;		 /* count of chars written so far */
		int written;			/* count of chars written on this write */
		ULONG dosretval;		/* o.s. return value */
		char ch;				/* current character */
		char *p, *q;			/* pointers into buf and lfbuf resp. */
		char lfbuf[BUF_SIZE]; /* lf translation buffer */
 
#else /* _MT */
 
/* now define normal version */
int __cdecl _write (
		int fh,
		const void *buf,
		unsigned cnt
		)
{
		int lfcount;			/* count of line feeds */
		int charcount;		 /* count of chars written so far */
		int written;			/* count of chars written on this write */
		ULONG dosretval;		/* o.s. return value */
		char ch;				/* current character */
		char *p, *q;			/* pointers into buf and lfbuf resp. */
		char lfbuf[BUF_SIZE]; /* lf translation buffer */
 
		/* validate handle */
		if ( ((unsigned)fh >= (unsigned)_nhandle) ||
			 !(_osfile(fh) & FOPEN) )
		{
				/* out of range -- return error */
				errno = EBADF;
				_doserrno = 0; /* not o.s. error */
				return -1;
		}
 
#endif /* _MT */
 
		lfcount = charcount = 0;		/* nothing written yet */
 
		if (cnt == 0)
				return 0;			 /* nothing to do */
 
 
		if (_osfile(fh) & FAPPEND) {
				/* appending - seek to end of file; ignore error, because maybe
				 file doesn't allow seeking */
				(void)_lseek_lk(fh, 0, FILE_END);
		}
 
		/* check for text mode with LF's in the buffer */
 
		if ( _osfile(fh) & FTEXT ) {
				/* text mode, translate LF's to CR/LF's on output */
 
				p = (char *)buf;		/* start at beginning of buffer */
				dosretval = 0;		 /* no OS error yet */
 
				while ( (unsigned)(p - (char *)buf) < cnt ) {
						q = lfbuf;	 /* start at beginning of lfbuf */
 
						/* fill the lf buf, except maybe last char */
						while ( q - lfbuf < BUF_SIZE - 1 &&
							(unsigned)(p - (char *)buf) < cnt ) {
								ch = *p++;
								if ( ch == LF ) {
										++lfcount;
										*q++ = CR;
								}
								*q++ = ch;
						}
 
						/* write the lf buf and update total */
 
HERE IS THE PROBLEM : 
 
					 if ( WriteFile( (HANDLE)_osfhnd(fh), 
										lfbuf,
										q - lfbuf,
										(LPDWORD)&written,
										NULL) )						{
								charcount += written;
								if (written < q - lfbuf)
										break;
						}
						else {
								dosretval = GetLastError();
								break;
						}
				}
		}
		else {
				/* binary mode, no translation */
				if ( WriteFile( (HANDLE)_osfhnd(fh), 
								(LPVOID)buf,
								cnt,
							 (LPDWORD)&written,
								NULL) )				{
						dosretval = 0;
						charcount = written;
				}
				else
						dosretval = GetLastError();
		}
 
		if (charcount == 0) {
				/* If nothing was written, first check if an o.s. error,
				 otherwise we return -1 and set errno to ENOSPC,
				 unless a device and first char was CTRL-Z */
				if (dosretval != 0) {
						/* o.s. error happened, map error */
						if (dosretval == ERROR_ACCESS_DENIED) {
							/* wrong read/write mode should return EBADF, not
							 EACCES */
								errno = EBADF;
								_doserrno = dosretval;
						}
						else
								_dosmaperr(dosretval);
						return -1;
				}
				else if ((_osfile(fh) & FDEV) && *(char *)buf == CTRLZ)
						return 0;
				else {
						errno = ENOSPC;
						_doserrno = 0; /* no o.s. error */
						return -1;
				}
		}
		else
				/* return adjusted bytes written */
				return charcount - lfcount;
}


... and so on

Reply With Quote
  #2  
Old April 30th, 2005, 02:12 AM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
just a random stab in the dark here, but is it possible your thread ouputs are overloading the console, thus freezing it?

also, you call WriteFile() after that point too, does it ever freeze there?
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > WriteFile() function in printf() freezes


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT