阻止子进程在无法控制的作业中被终止,当父进程启动时。

huangapple go评论82阅读模式
英文:

Stopping children being terminated when parent process was started in a Job I can't control

问题

我有一个我无法控制的进程(A),它启动了我自己开发的进程(B),而进程(B)又会启动若干其他进程(C)。

看起来,A使用作业对象启动B,因为当A终止时,B也会终止。我还通过Process Explorer确认了这一点。我遇到的问题是B的子进程也被终止了,这对作业对象来说是正常行为。

有没有办法让我从B启动的子进程不继承作业属性或具有新的属性集?

我尝试过创建一个新的作业并将子进程分配给它,但似乎这只是意味着子进程属于两个作业,并且仍然会被终止。

有没有人遇到过这种情况?

英文:

I have a process (A) that I don't control that starts my own developed process (B) which in turn will start several other processes (C).

It looks like A starts B using a Job object as when A is terminated B is also terminated. I've also confirmed this with Process Explorer. The issue I am having is that the child processes of B are also being terminated, which is normal behavior for Job objects.

Is there a way for me to launch child processes from B that don't inherit the Job attributes or have a new set of attributes?

I've tried creating a new Job and assigning the child processes to that, but it seems this is just meaning the child processes belong to two jobs and are still terminated.

Has anyone come across this before?

答案1

得分: 1

如果进程B通过CreateProcess()启动进程C,那么进程B可以指定CREATE_BREAKAWAY_FROM_JOB标志:

进程创建标志

常量/值 描述
CREATE_BREAKAWAY_FROM_JOB<br>0x01000000 与作业关联的进程的子进程不与该作业关联。<br>如果调用进程未与作业关联,此常量不会产生任何效果。如果调用进程与作业关联,该作业必须设置JOB_OBJECT_LIMIT_BREAKAWAY_OK限制。

但是,如上所述,这仅在作业启用了JOB_OBJECT_LIMIT_BREAKAWAY_OK限制以允许子进程"脱离"作业时才起作用:

作业对象 | 管理作业中的进程

一旦将进程与作业关联,默认情况下,它使用CreateProcess创建的任何子进程也与该作业关联。 (使用Win32_Process.Create创建的子进程不与作业关联。)可以通过为作业设置扩展限制JOB_OBJECT_LIMIT_BREAKAWAY_OK或JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK来更改此默认行为。

  • 如果作业具有扩展限制JOB_OBJECT_LIMIT_BREAKAWAY_OK并且父进程是使用CREATE_BREAKAWAY_FROM_JOB标志创建的,则父进程的子进程不与该作业关联。

  • 如果作业具有扩展限制JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK,则与作业关联的任何父进程的子进程都不与该作业关联。 不需要使用CREATE_BREAKAWAY_FROM_JOB标志创建父进程。

如果进程A未启用JOB_OBJECT_LIMIT_BREAKAWAY_OK限制,则不希望进行"脱离"操作。进程B 可能能够更新它,但只有在它知道它正在运行的作业的名称以便获取该作业的HANDLE时才能这样做:

IsProcessInJob函数

除非应用程序拥有作业对象的名称,否则无法获取其正在运行的作业对象的句柄。但是,应用程序可以调用QueryInformationJobObject函数并将NULL作为参数,以获取有关作业对象的信息。

英文:

If Process B is starting Process C via CreateProcess(), then Process B can specify the CREATE_BREAKAWAY_FROM_JOB flag:

Process Creation Flags

> | Constant/value | Description |
> | -------------- | ----------- |
> | CREATE_BREAKAWAY_FROM_JOB<br>0x01000000 | The child processes of a process associated with a job are not associated with the job.<br>If the calling process is not associated with a job, this constant has no effect. If the calling process is associated with a job, the job must set the JOB_OBJECT_LIMIT_BREAKAWAY_OK limit. |

But, as stated, this only works if the Job has the JOB_OBJECT_LIMIT_BREAKAWAY_OK limit enabled to allow child processes to "break away" from the job:

Job Objects | Managing Processes in Jobs

> After a process is associated with a job, by default any child processes it creates using CreateProcess are also associated with the job. (Child processes created using Win32_Process.Create are not associated with the job.) This default behavior can be changed by setting the extended limit JOB_OBJECT_LIMIT_BREAKAWAY_OK or JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK for the job.
>
> - If the job has the extended limit JOB_OBJECT_LIMIT_BREAKAWAY_OK and the parent process was created with the CREATE_BREAKAWAY_FROM_JOB flag, then child processes of the parent process are not associated with the job.
>
> - If the job has the extended limit JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK, then child processes of any parent process associated with the job are not associated with the job. It is not necessary for parent processes to be created with the CREATE_BREAKAWAY_FROM_JOB flag.

If Process A does not enable the JOB_OBJECT_LIMIT_BREAKAWAY_OK limit, then it doesn't want break aways. Process B might be able to update that, but only if it knows the name of the Job that it is running in so it can obtain a HANDLE to that Job:

IsProcessInJob function

> An application cannot obtain a handle to the job object in which it is running unless it has the name of the job object. However, an application can call the QueryInformationJobObject function with NULL to obtain information about the job object.

huangapple
  • 本文由 发表于 2023年8月10日 22:24:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定