简体   繁体   English

如何通过其PID与流程交互

[英]How to interact with a process with its PID

I am using Ubuntu 12.04 . 我正在使用Ubuntu 12.04。 I would like to know how can I do operations on a process through its PID . 我想知道如何通过其PID对进程进行操作。 I don't know whether is it possible or not. 不知道有没有可能 If possible help me on this. 如果可能,请帮助我。 Operations means status, properties of the process and sending commands and data to process through it PID . 操作是指状态,过程的属性以及通过其PID发送命令和数据进行处理。

EDIT: Status means its working statistics, for example how much RAM it's consuming. 编辑:状态表示其工作统计信息,例如它消耗了多少RAM。 Properties means information regrading that process. 属性意味着信息会重新定义该过程。 The main thing I want to know here is about sending commands/data to process through its PID. 我想在这里了解的主要内容是有关发送命令/数据以通过其PID进行处理。

procfs is something you should have a look at. procfs是您应该看看的东西。 It provides a lot of information on the process. 它提供了有关该过程的大量信息。 Here is the manpage for proc(5) . 这是proc(5)联机帮助页 Basically, you cd to /proc/$PID and all those pseudo files contain info you should be able to look at. 基本上,您将cd转到/proc/$PID ,所有这些伪文件都包含您应该能够查看的信息。

Otherwise, a good program to play with is top , or even better, htop . 否则,可以使用的最佳程序是top ,甚至更好的是htop It can send all sorts of signals, and you can observe a lot of info (status, mem usage, cpu usage,...) It's really great. 它可以发送各种信号,并且您可以观察到很多信息(状态,内存使用情况,CPU使用情况...),这真的很棒。

Apart from that it's not clear what you're looking to do, so I'm staying pretty generic. 除此之外,目前尚不清楚您要做什么,因此我保持通用性。 If you want to do IPC (inter process communication) as sarnold said, signals might be the way to go, or mmap. 如果您想像sarnold所说的那样进行IPC(进程间通信),信号可能是解决之道或mmap。 (memory-mapped file) (内存映射文件)

ps, kill and top are commonly used to interact with processes, please read their man pages. ps,kill和top通常用于与进程进行交互,请阅读其手册页。

ps gives snapshot,usually to grab pid from the proc name though: ps提供快照,通常从proc名称中获取pid:

eg ps aux | grep -i sshd 例如ps aux | grep -i sshd ps aux | grep -i sshd

top gives semi-continuous feedback (like a configurable windows task manager...sorta) 顶部给出半连续的反馈(例如可配置的Windows任务管理器... sorta)

In my opinion top will give a good idea of mem usage. 我认为top可以很好地了解内存使用情况。 You'll need to use top's interactive commands to configure for best output though. 不过,您将需要使用top的交互式命令来配置以获得最佳输出。 Press 'o' for sorting and 'f' for fields I think, read the man. 按“ o”进行排序,按“ f”对我认为的字段,请读那个人。

kill sends SIGTERM by default (or something w/e it stops the process) or use kill -9 if proc handler is ignoring SIGTERMs, kill -s SIGNAL can be used to send ANY signal though like SIGSTOP (stops proc) and SIGCONT (continues proc) 默认情况下,kill会发送SIGTERM(或者它会停止进程)或在proc处理程序忽略SIGTERM时使用kill -9,kill -s SIGNAL可以用来发送任何信号,尽管SIGSTOP(停止proc)和SIGCONT(继续PROC)

Different programs have different way of taking process id as parameter. 不同的程序采用不同的方式将进程ID作为参数。 Man page is you help here. 手册页为您提供帮助。

Now for example top will give most of the basic system stats. 现在,例如top将提供大多数基本系统统计信息。 To get the top result for a process say X , this command will give you the required result top -n1 -b -p <pid of X> 要获得某个进程的最高结果(例如X ,此命令将为您提供所需的结果top -n1 -b -p <pid of X>

Also /proc/<pid> dir will have all the necessary system stats, all you need is know where and how to read those data. 另外/proc/<pid> dir将具有所有必要的系统统计信息,您所需要做的就是知道在哪里以及如何读取这些数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM