duanlu9557 2019-09-05 13:44
浏览 1129
已采纳

退出后,该任务将从cgroup中删除

Here is my test code. I want to write current PID to the tasks file:

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()
  }
}

But when the program exits with code 0, I cannot see anything in 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# 

How to solve this problem?

  • 写回答

1条回答 默认 最新

  • drazvzi741287 2019-09-05 17:39
    关注

    What you've described is not a problem at all — it is a correct behavior:

    • you create an empty cgroup;
    • you add a task to it;
    • the task runs in the cgroup for a while;
    • the task exits;
    • the task gets removed from the cgroup due to the previous item;
    • the cgroup is empty again;
    • you observe the list of cgroup members and see nothing.

    Why this is a correct behavior? Well, the most trivial answer is that there are no reasons for keeping non-existent PID in the list of cgroup members. On the other hand, there are numerous reasons for not doing this.

    PIDs reuse is one of these reasons which comes to mind first: if some PID does not get removed from the cgroup after the death or the graceful shutdown of the task, any other task which will later reuse this PID (assuming that the cgroup is still alive) will be a member of this group, which, obviously, is not a desired behavior, especially when we speak about containers (e.g. such a behavior may be a reason of privileges gain if we speak about the devices cgroup).

    As I previously mentioned in the comments, I can't find an explicit description of this behavior in the documentation — probably that's because it feels to be too obvious, but the source code is self-explanatory: the exit(2) system call executes cgroup_exit() under the hood, and this function moves the task out of its cgroups.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的