任务杀手如何运作?

发布于 2024-12-05 21:45:50 字数 227 浏览 0 评论 0原文

任务杀手应用程序的实用性存在争议,但我想知道:它们实际上是如何工作的?如何杀死特定进程?

是否有用于此目的的 API?如果有,它实际上做什么

编辑

值得补充:我看到任务杀手应用程序杀死了未root设备上的进程。所以,我想知道如何杀死 Android 中不属于您的进程?

The usefullness of task killer apps is debated, but I'm wondering: how do they actually work? How is it possible to kill particular process?

Is there an API for this, and if so what does it actually do?

EDIT

Worth adding: I saw task killer apps kill processes on not rooted devices. So, I wonder how is it possible to kill process, which you don't own in Android?

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

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

发布评论

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

评论(1

同尘 2024-12-12 21:45:50

简而言之,自动任务杀手的工作原理是轮询操作系统以获取当前正在运行的进程及其消耗的内存列表。然后,任务杀手通过智能算法或用户输入向系统发出调用,告诉系统终止进程。有两个 api 可以做到这一点。

它们是

  • Process.killProcess(int pid)
  • ActivityManager.killBackgroundProcesses(String packageName)

首先通过调用 Process.killProcess(int pid) 来实现,其中pid 是特定进程的唯一标识符。 Android 终止进程的方式与 Linux 相同;但是,用户只能终止他们拥有的进程。在 Android 中,每个应用程序都使用唯一的 UID (UserID) 运行。使用此 API 的应用程序只能终止自己的进程,因此以下 中的解释Process.killProcess(int pid) 的文档:

终止具有给定 PID 的进程。请注意,虽然此 API 允许
如果我们请求根据 PID 杀死任何进程,内核将
仍然对您实际能够使用的 PID 施加标准限制
杀死。通常这意味着仅运行调用者的进程
包/应用程序以及该应用程序创建的任何其他进程;
共享共同 UID 的包也能够杀死彼此的包
流程。

当调用此方法时,操作系统会生成信号并将其发送到进程。每当进程从操作系统接收到信号时,它必须处理该信号或立即死亡。诸如 SIG_KILL 之类的信号无法被处理并导致接收进程立即死亡。如果您想终止您无权终止的进程,即它不是您的进程,那么您必须切换用户或升级您的权限(在 Android 上,这需要设备上的 root 权限)。

第二个 API 的工作原理是告诉内置的 ActivityManager 您想要终止与特定包关联的进程。 此 API 不需要您的 UID 与进程的 UID 相匹配,因为它要求用户接受 KILL_BACKGROUND_PROCESSES 权限。此权限向操作系统发出信号,表明用户已批准应用程序作为任务杀手。当任务杀手想要终止某个应用程序时,它会告诉操作系统终止该进程,从而使应用程序能够解决只能终止其拥有的进程的问题。

Android 文档中,它说这个 API 实际上使用第一个 <代码>Process.killProcess API

让系统立即终止所有关联的后台进程
与给定的包。这与内核杀死那些相同
回收内存的进程;系统将负责重新启动
将来根据需要进行这些流程。

如果您想了解更多信息,我建议您阅读 Posix SignalsLinux 终止命令

In a nutshell, Automatic Task Killers work by polling the OS for a list of currently running processes and the memory they are consuming. Then either with an intelligent algorithm or with user input the Task Killers issue a call to the system telling the system to kill the process. There are two apis you can do this.

They are

  • Process.killProcess(int pid)
  • ActivityManager.killBackgroundProcesses(String packageName)

This first works by invoking Process.killProcess(int pid) where pid is the unique identifier for a specific process. Android kills processes in the same way that linux does; however, a user may only kill processes that they own. In Android each app is run with a unique UID (UserID). Apps using this API an App can only kill their own processes, hence the following explanation in the docs for Process.killProcess(int pid):

Kill the process with the given PID. Note that, though this API allows
us to request to kill any process based on its PID, the kernel will
still impose standard restrictions on which PIDs you are actually able
to kill. Typically this means only the process running the caller's
packages/application and any additional processes created by that app;
packages sharing a common UID will also be able to kill each other's
processes.

When this method is called the signal is generated by the OS and sent to the process. Whenever a process receives a signal from the OS it must either handle that signal or immediately die. Signals such as SIG_KILL cannot be handled and result in the immediate death of the recipient process. If you want to kill processes that you don't have privileges to kill, i.e. its not your process, then you must switch users or escalate your privileges (on android this requires root privileges on the device).

The second API works by telling the built in ActivityManager that you wan to kill processes associated with a specific Package. This API gets around the need for your UID to match the UID of the process because it requires the user to accept the KILL_BACKGROUND_PROCESSES permission. This permission signals to the OS that an app has been approved by the user as a task killer. When a task killer wants to kill an app, it tells the OS to kill the process allowing an app to get around the problem of only being able to kill processes that it owns.

In the Android Docs it says that this API actually uses the first Process.killProcess API

Have the system immediately kill all background processes associated
with the given package. This is the same as the kernel killing those
processes to reclaim memory; the system will take care of restarting
these processes in the future as needed.

If you want to know more I suggest you read about the Posix Signals and The Linux kill command

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