有没有办法在后台发送一些已知 pid 的进程?

发布于 2024-11-15 07:54:34 字数 94 浏览 0 评论 0原文

我是 Linux 和系统编程的新手。 我想编写 ac 程序,查找 cpu% 使用率超过特定给定值的进程并将它们发送到后台。

任何人都可以帮助我! 我真的很感激

I am new in Linux and system programming .
I Want to write a c program which finds processes whose cpu% usage are more than a specific given value and sends them to background.

anybody can help me !
I really appreciate it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

胡大本事 2024-11-22 07:54:34

我相当确定您要问的是您想要检测某个进程是否正在使用 X 数量的 CPU,如果是,请将其从 CPU 中移除一段时间。已经有一个软件可以做到这一点:它称为内核。我不知道有什么方法可以以编程方式使另一个进程脱离CPU,除非其他程序支持外部接口来减少其负载。

最有可能的是,您真正想要做的是配置正在运行的进程的nice 和其他调度程序参数,以便当另一个程序需要工作时,内核更愿意将其从CPU 上取下。

但您真正想要解决的根本问题是什么?也许如果您告诉我们我们可以提供替代解决方案。

I'm fairly sure that what you're asking is that you want to detect if a process is using X amount of CPU and if so, take it off the CPU for a while. There's a piece of software already that does this: It's called the kernel. I'm not aware of any way to programatically take another process off CPU unless that other program supports an external interface to reduce its load.

Most likely what you really want to do is configure the nice and other scheduler parameters of the running process so the kernel is more like to to take it off CPU when another program needs to do work.

But what underlying problem are you really trying to solve here? Maybe if you tell us that we can offer an alternate solution.

恰似旧人归 2024-11-22 07:54:34

请查看进程管理实用程序的源代码,例如:

  • htop
  • top(标准unix命令)
  • ps(标准 UNIX 命令)

Please look at source code of process managament utilities like:

  • htop
  • top (standard unix command)
  • ps (standard unix command)
暖风昔人 2024-11-22 07:54:34

恕我直言,你不能。

后台管理保证了shell。因此,& 由 /bin/bash 命令解释。当按下 CTRL-Z 时,内核会停止当前的 fg 作业,并且再次通过您的 shell 您可以将其发送到后台。

您正在寻找如何远程控制在 fg 中运行某些程序的 shell。我不知道任何“远程控制”方式。

Ofc,这里有替代解决方案,例如:

  • 使用 screen 命令,您可以将特定屏幕调回终端,并且可以手动将进程发送到bg中。
  • 或者您可以使用一些屏幕共享实用程序来接管特定终端和 CTRL-Z、bg
  • ,或者您可以修补 bash 并添加远程控制功能。 ;)

或者,这是我不知道的事情。 ;) - 嗯,也许在 /etc/profile 中trap一些用户信号处理代码?

您可以在这里阅读一些相关内容:http://en.wikipedia.org/wiki/Process_group

老实说,经过半个小时的思考,我不明白为什么你想要远程(从另一个终端 - 通过它的 PID)将一些进程从 fg 发送到 bg 中。给我一点意义都没有。

您能告诉我,您想要实现什么吗?

IMHO, You can't.

Background management ensures the shell. So, the & is interpreted for example by /bin/bash command. When pressed CTRL-Z, the kernel stopping your current fg-job, and again by your shell you can send it into background.

Youre looking for the way how to remote control the shell what running some program in fg. I don't know any 'remote-controling' way.

Ofc, here are alternative solutions, for example:

  • use the screen command, and you can recall the specific screen into your terminal, and can manually send process into bg.
  • or you can use some screen-sharing utility, to overtake a specific terminal and CTRL-Z, bg
  • or, you can patch bash and adding remote control functionality. ;)

or, here is something what i don't know. ;) - hm, maybe trap some user-signal handling code in the /etc/profile?

You can read a bit about here: http://en.wikipedia.org/wiki/Process_group

Honestly, after a half hour of thinking I don't get any idea why you want remotely (from the another terminal - by its PID) send some processes from the fg into the bg. Give me no sense.

Can you please tell, what you want achieve?

千里故人稀 2024-11-22 07:54:34

您可能想降低进程优先级,但我不确定这是个好主意。

我们通常将进程发送到后台以释放 shell 的提示符。

You probably want to reduce process priority, but I not sure it's good idea.

We send process to background generally to free shell's prompt.

薄暮涼年 2024-11-22 07:54:34

“+”表示该程序“位于前台进程组”。然而,我认为这种状态根本不会影响进程的调度。

但是,您可以使用tcsetpgrp更改它。

从手册页来看:“函数 tcsetpgrp() 使进程组 ID 为 pgrp 的进程组成为与 fd 关联的终端上的前台进程组,该终端必须是调用进程的控制终端,并且仍然与其会话关联。此外,pgrp 必须是与调用进程属于同一会话的(非空)进程组。”

根据我的阅读,您只需调用此函数并使 shell(或其他程序)成为前台进程即可。

The "+" means that the program "is in the foreground process group". I don't believe, however, that this state at all affects the process's scheduling.

However, you can change it with tcsetpgrp.

From the man page: "The function tcsetpgrp() makes the process group with process group ID pgrp the foreground process group on the terminal associated to fd, which must be the controlling terminal of the calling process, and still be associated with its session. Moreover, pgrp must be a (non-empty) process group belonging to the same session as the calling process."

By my reading, you just call this function and make the shell (or some other program) be the foreground process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文