【问题标题】:How to retrieve the all available PIDs on windows in tcl?如何在 tcl 中检索 windows 上所有可用的 PID?
【发布时间】:2013-01-08 07:18:19
【问题描述】:

我想知道通过 tcl 编码检索 windows 中所有可用 pid 的方法。这是我需要根据它是否可用来杀死进程吗? 感谢您。 代码:

proc stopprogressbar {} {
  variable n_progressWinPid;
  if {[info exists n_progressWinPid]} {
    if {$n_progressWinPid > 0} {
      if {[lsearch [GETALLPIDS] $n_progressWinPid] >= 0} {
        catch {exec [auto_execok taskkill] /PID $n_progressWinPid}
      }
    }
  }
}

【问题讨论】:

    标签: windows process tcl pid


    【解决方案1】:

    twapi::get_process_ids 返回 Windows 系统上的所有 PID。你必须先package require twapi

    如果您使用 ActiveTcl,请在命令提示符下运行 C:\Tcl\bin\teacup.exe install twapi(或安装 Tcl 的任何位置)以获取 TWAPI。它甚至可能默认包含在较新的 ActiveTcl 版本中,我不知道。

    【讨论】:

    • 是的......这就是我要找的......而且我使用了相同的包。
    【解决方案2】:

    您的目标似乎是杀死一个进程(如果存在)。不检查就以任何方式杀死它怎么样?

    package require Tclx;           # For the kill command
    catch {kill $n_progressWinPid}; # Kill the process
    

    Tclx 包中您可能会发现有用的其他命令:

    id process;        # Get PID for this process
    id process parent; # Get PID for the parent of this process
    wait ?many options? pid; # Wait for a process
    

    请查看 Tclx 包的帮助以获取更多信息。

    【讨论】:

    • 这是真的,我想最终杀死进程,但在主程序中我想检查 pid (n_progressWinPid) 是否存在。这是因为如果该 pid 不存在则停止主进程,否则继续。 (主流程不只是单循环)
    • @OliveOne 检查进程是否存在最好通过查找目录/proc/$thepid 或使用正确的选项(例如exec ps -p $thepid)运行系统程序ps 来完成。唉,由于历史原因,这些都是不可移植的。
    猜你喜欢
    • 2013-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多