[英]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.