如何同步父/子进程执行?
我想执行一个子进程并同步它(可能与互斥体),而不等待子进程终止:
Parent:
program Project1;
{$APPTYPE CONSOLE}
uses
Windows, ShellApi, SysUtils, Dialogs;
procedure ShellExecEx(Wnd: HWND; const AExeFilename, AParams: string);
const
SEE_MASK_NOZONECHECKS = $00800000;
SEE_MASK_WAITFORINPUTIDLE = $02000000;
SEE_MASK_NOASYNC = $00000100;
var
Info: TShellExecuteInfo;
begin
FillChar(Info, SizeOf(Info), 0);
Info.Wnd := Wnd;
Info.cbSize := SizeOf(Info);
Info.fMask := SEE_MASK_FLAG_NO_UI or SEE_MASK_NOZONECHECKS or
SEE_MASK_NOASYNC
//or SEE_MASK_WAITFORINPUTIDLE (works only with UI app ???)
//or SEE_MASK_NO_CONSOLE
//or SEE_MASK_NOCLOSEPROCESS
;
Info.lpVerb := '';
Info.lpFile := PChar(AExeFilename);
Info.lpParameters := PChar(AParams);
Info.lpDirectory := PChar(ExtractFilePath(AExeFilename));
Info.nShow := SW_SHOWNORMAL;
if not ShellExecuteEx(@Info) then
RaiseLastOSError;
CloseHandle(Info.hProcess);
end;
var
Mutex: THandle = 0;
Error: DWORD;
begin
OutputDebugString('Project1 : 1');
ShellExecEx(0, 'Project2.exe', '');
// synchronize
repeat
// attempt to create a named mutex
Mutex := CreateMutex(nil, False, 'F141518A-E6E4-4BC0-86EB-828B1BC48DD1');
Error := GetLastError;
if Mutex = 0 then RaiseLastOSError;
CloseHandle(Mutex);
until Error = ERROR_ALREADY_EXISTS;
OutputDebugString('Project1 : 3');
end.
Child:
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils, Windows, Dialogs;
var
Mutex: THandle = 0;
begin
OutputDebugString('Project2 : 2');
// attempt to create a named mutex and acquire ownership
Mutex := CreateMutex(nil, True, 'F141518A-E6E4-4BC0-86EB-828B1BC48DD1');
if Mutex = 0 then RaiseLastOSError;
// do something
ReleaseMutex(Mutex);
CloseHandle(Mutex); // <- at this point Program1.exe should exit the repeat loop
ShowMessage('ok from Project2');
end.
我期待看到一个输出:
Project1 : 1
Project2 : 2
Project1 : 3
问题是有时父级 (Project1.exe) 没有退出循环。
我做错了什么?
I would like to execute a child process and synchronize it (possibly with Mutex) without waiting for the child process to terminate:
Parent:
program Project1;
{$APPTYPE CONSOLE}
uses
Windows, ShellApi, SysUtils, Dialogs;
procedure ShellExecEx(Wnd: HWND; const AExeFilename, AParams: string);
const
SEE_MASK_NOZONECHECKS = $00800000;
SEE_MASK_WAITFORINPUTIDLE = $02000000;
SEE_MASK_NOASYNC = $00000100;
var
Info: TShellExecuteInfo;
begin
FillChar(Info, SizeOf(Info), 0);
Info.Wnd := Wnd;
Info.cbSize := SizeOf(Info);
Info.fMask := SEE_MASK_FLAG_NO_UI or SEE_MASK_NOZONECHECKS or
SEE_MASK_NOASYNC
//or SEE_MASK_WAITFORINPUTIDLE (works only with UI app ???)
//or SEE_MASK_NO_CONSOLE
//or SEE_MASK_NOCLOSEPROCESS
;
Info.lpVerb := '';
Info.lpFile := PChar(AExeFilename);
Info.lpParameters := PChar(AParams);
Info.lpDirectory := PChar(ExtractFilePath(AExeFilename));
Info.nShow := SW_SHOWNORMAL;
if not ShellExecuteEx(@Info) then
RaiseLastOSError;
CloseHandle(Info.hProcess);
end;
var
Mutex: THandle = 0;
Error: DWORD;
begin
OutputDebugString('Project1 : 1');
ShellExecEx(0, 'Project2.exe', '');
// synchronize
repeat
// attempt to create a named mutex
Mutex := CreateMutex(nil, False, 'F141518A-E6E4-4BC0-86EB-828B1BC48DD1');
Error := GetLastError;
if Mutex = 0 then RaiseLastOSError;
CloseHandle(Mutex);
until Error = ERROR_ALREADY_EXISTS;
OutputDebugString('Project1 : 3');
end.
Child:
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils, Windows, Dialogs;
var
Mutex: THandle = 0;
begin
OutputDebugString('Project2 : 2');
// attempt to create a named mutex and acquire ownership
Mutex := CreateMutex(nil, True, 'F141518A-E6E4-4BC0-86EB-828B1BC48DD1');
if Mutex = 0 then RaiseLastOSError;
// do something
ReleaseMutex(Mutex);
CloseHandle(Mutex); // <- at this point Program1.exe should exit the repeat loop
ShowMessage('ok from Project2');
end.
I'm expecting to see an output of:
Project1 : 1
Project2 : 2
Project1 : 3
Problem is that sometimes the Parent (Project1.exe) is not exiting the loop.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你在互斥锁上进行了一场竞赛。您希望出现以下顺序:
但可能发生的情况是,
我不太清楚您的最终目标是什么,但我怀疑某个事件实际上就是您正在寻找的内容。
在父级中:
在孩子身上:
在非常高的级别,您需要的代码将如下所示:
Parent
Child
我没有包含任何错误检查。我相信你可以添加一些。您可能还会发现 SyncObjs 中的事件包装类更方便。
最后,您的代码有一个繁忙的循环。这几乎永远不是解决任何问题的方法。如果您发现自己编写了一个繁忙的循环,您应该将其视为设计不正确的信号。重点是,在您的代码中,如果可以使其正常工作,父进程在等待子进程时将消耗 100% 的 CPU 利用率。
You have a race on the mutex. You are hoping for the following sequence:
But what can happen is
I can't quite work out what your ultimate goal is but I have a suspicion that an event is actually what you are looking for.
In the parent:
In the child:
In very high level the code you need will look like this:
Parent
Child
I've not included any error checking. I'm sure you can add some. You may also find that the event wrapper class in
SyncObjs
is more convenient.Finally, your code has a busy loop. That is almost never the solution to any problem. If ever you find yourself writing a busy loop you should take that as a signal that the design is incorrect. The point being that, in your code, if it could be made to work, the parent process would burn 100% CPU utilization whilst waiting on the child process.