⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 列表6.5.txt

📁 klinux书籍的配套光盘。可以学习学习。
💻 TXT
字号:
【列表6.5】 摘录自KillZombiesMain.pas的程序代码。
 const
  LF = ^J; { ASCII linefeed/newline }
var
   KillAllZombiesForm: TKillAllZombiesForm;
   SigActionRec : TSigAction;
   ChildDone : Boolean;
   ChildPID : pid_t;
procedure Handler(Sig : Integer); cdecl;
begin
 case Sig of
  SIGCHLD : begin
    if waitpid(ChildPID, nil, WNOHANG) = ChildPID
                     then ChildDone := True;
                  end;
 end; { case }

end;
procedure InstallHandlers;
begin
 with SigActionRec do
   begin
    __sigaction_handler := Handler;
    sigemptyset(sa_mask);
    sa_flags := 0;
    sigaction(SIGCHLD, @SigActionRec, nil);
   end; { with }
end;
procedure DefaultHandlers;
begin
 with SigActionRec do
   begin
    __sigaction_handler := TSigActionHandler(SIG_DFL);
    sigemptyset(sa_mask);
    sa_flags :=0;
    sigaction(SIGCHLD, @SigActionRec, nil);
   end; { with }
end;
procedure TKillAllZombiesForm. RefreshForm;
begin
  case BehaviorRBGroup. ItemIndex of
   0 : InstallHandlers;
   1 : DefaultHandlers;
  end; { case }
end;

procedure TKillAllZombiesForm. LaunchBtnClick(Sender: TObject):
var
  i : Integer;
  FName : String;
  argv : array[0..1] of PChar;
  open_max : Integer;
begin
  FName := 'CrashTestDummy';
argv[0] := PChar(FName);
   argv[1] := nil;
   if not FileExists(FName)
    then begin
             MessageDlg('Error', 'Cannot locate the file ' + FName + ' ' + LF
              + 'Make sure a copy is present in your' + LF
              + 'home directory.',
              mtError, [mbOK], 0);
             Exit;
           end;
  ChildPID := fork;
  case ChildPID of
    -1 : { This is still the parent }
           MessageDlg('Error'. 'Could not launch child process.',
             mtError, [mbOK], 0);
     0 : begin { This is the child }
             { Close the open files }
             open_max := sysconf(_SC_OPEN_MAX);
             for i := stderr + 1 to open_max - 1 do
              fcntl(i, F_SETFD, FD_CLOEXEC);
             FName := 'CrashTestDummy';
             execlp(PChar(FName), @argv, nil);
           
            { If execlp failed then bail the child }
            _exit(-1);
           end;
   else { This is the parent, so disable the button }
     LaunchBtn. Enabled := False;
    ChildDone := False;
    MonitorTimer. Enabled := True;
 end { case }
end;
procedure TKillAllZombiesForm. MonitorTimerTimer(Sender: TObject);
begin
 if ChildDone
   then begin
            MonitorTimer. Enabled := False;
            ShowMessage('Child process completed.');
            LaunchBtn. Enabled:= True;
          end;
end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -