![](/img/trans.png)
[英]Set the cgroup in centos7 VM and select 4 cpus in this cgroup, make the stress task in this cgroup, but only one of them works
[英]The task is removed from cgroup after the exit
这是我的测试代码。 我想将当前的PID写入tasks
文件:
package subsystems
import (
"io/ioutil"
"os"
"path"
"strconv"
"testing"
)
func TestSubsystems(t *testing.T) {
p := "/sys/fs/cgroup/memory/test1"
f := "tasks"
err := os.MkdirAll(p, 0644)
if err != nil {
t.Log(err.Error())
}
pid := os.Getpid()
if err := ioutil.WriteFile(path.Join(p, f), []byte(strconv.Itoa(pid)), 0644); err != nil {
t.Failed()
}
}
但是,当程序以代码0退出时,我看不到tasks
任何内容:
root@ubuntu:/sys/fs/cgroup/memory/test1# cat tasks
root@ubuntu:/sys/fs/cgroup/memory/test1# cat tasks
root@ubuntu:/sys/fs/cgroup/memory/test1#
如何解决这个问题呢?
您所描述的完全不是问题,这是正确的行为:
为什么这是正确的行为? 好吧,最简单的答案是,没有理由将不存在的PID保留在cgroup成员列表中。 另一方面,有很多原因不这样做。
PID重用是首先想到的这些原因之一:如果某个PID在任务死亡或正常关闭后没有从cgroup中删除,则其他任何稍后将重用此PID的任务(假设cgroup仍在活着的人)将成为该组的成员,这显然不是期望的行为,尤其是当我们谈论容器时(例如,如果我们谈论devices
cgroup,则这种行为可能是特权获得的原因)。
正如我之前在评论中提到的那样,我在文档中找不到此行为的明确描述-可能是因为感觉太明显了,但是源代码是不言自明的: exit(2)
系统调用执行 cgroup_exit()
在cgroup_exit()
,此函数将任务移出cgroups 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.