f



"file in use by another process" error after process has terminated

I'm running a process with stdout redirected into a file, and after
waiting for it to terminate (WaitForSingleObject on the process
handle), I read the file.  This usually works, but once in a while,
opening the file fails with EPIPE, "The process cannot access the file
because it is being used by another process".  If I comment out the
WaitForSingleObject, it always fails with this error.

It doesn't happen often enough for me to be able to be sure that a fix
has worked.  Also, it never happens (at least not after 1000 tries) on
the development machine, and I'd rather not run stress tests on the
production machine.

Code is compiled in MSVC 6, running on Windows 2000 Pro.

here's how I run the process:

bool run(LPCTSTR command, LPCTSTR outfile, CString& reason) {
  SECURITY_ATTRIBUTES sa;

  sa.nLength = sizeof(sa);
  sa.lpSecurityDescriptor = NULL;
  sa.bInheritHandle = TRUE;

  HANDLE hOut = CreateFile(outfile, GENERIC_WRITE, 0, &sa,
			       CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  if (hOut == INVALID_HANDLE_VALUE)	{
	reason = "CreateFile ";
	reason += outfile;
	reason += " - ";
	reason += ErrorMessage();
	return false;
  }

  STARTUPINFO si;
  GetStartupInfo(&si);
  si.hStdOutput = hOut;
  si.dwFlags = STARTF_USESTDHANDLES;

  PROCESS_INFORMATION pi;
  ZeroMemory(&pi, sizeof pi);

  if (CreateProcess(NULL, (char*) command, NULL, NULL,
				TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
	WaitForSingleObject(pi.hProcess, INFINITE);
	CloseHandle(hOut);
	CloseHandle(pi.hProcess);
	CloseHandle(pi.hThread);
	return true;
  } else {
	reason = "CreateProcess ";
	reason += command;
	reason += " - ";
	reason += ErrorMessage();
	CloseHandle(hOut);
	return false;
  }
}

after which I did (and here's where it sometimes fails:

bool LIResults::ReadText(const CString& fileName, CString& text,
						   CString& reason) {
  ifstream file;
  file.open(fileName, ios::in | ios::nocreate);
  if (file.fail()) {
	reason = "cannot open ";
	reason += fileName;
	reason += " - ";
	reason += ErrorMessage();
	return false;
  }
  ...

here's how I get the error message:

char* ErrorMessage() {
  int error = GetLastError();

  static char errmsg[200];
  if ( !FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error,
		MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
		errmsg, sizeof errmsg, NULL))
	sprintf(errmsg, "windows error %d\n", error);
  return errmsg;
}

Is there any way to find out what process has the file open? 
Something like lsof(8) on linux?  That way I could at least find out
if the child process is open, or maybe someone else has opened the
file (but I can't imagine who else would be doing that).  As it's
likely that the file is only open for a short time, a way to do it
from the command line would be needed - even if I watched it all day
long waiting for the problem, chances are I wouldn't be able to
operate a GUI fast enough to catch it.

Any other suggestions would be welcome.

Thanks.
0
warrenb10
7/23/2003 1:33:51 PM
comp.os.programmer.win32 14523 articles. 0 followers. Post Follow

1 Replies
722 Views

Similar Articles

[PageSpeed] 32

"Warren Burstein" <warrenb10@my-deja.com> wrote in message news:dd99563a.0307230533.18161d12@posting.google.com...
> Is there any way to find out what process has the file open?
> Something like lsof(8) on linux?  That way I could at least find out

Try 'Process Explorer' (procexp.exe) from sysinternal.com.

--
Ritchie


0
Ritchie
7/23/2003 1:50:31 PM
Reply: