每个进程几只手
(每道题开头都有同一段:那张线程表 PSL,和把它切成字典列表的 rows()——每个字典有 pid / tid / nlwp / stat / cpu / comm 六项。)
按 PID 分组数线程,和 NLWP 对一对:
PSL = '''PID TID NLWP STAT %CPU COMM
1 1 1 Ss 0.0 bash
12 12 1 S 0.0 收数据.sh
17 17 4 Ssl 49.8 算账
17 18 4 Rsl 49.6 算账-核对
17 19 4 Ssl 0.0 算账-写盘
17 20 4 Ssl 0.1 算账-心跳
21 21 2 Ssl 0.0 通知
21 22 2 Ssl 0.0 通知-等待'''
def rows(text):
out = []
for line in text.split("\n")[1:]:
pid, tid, nlwp, stat, cpu, comm = line.split()
out.append({"pid": int(pid), "tid": int(tid), "nlwp": int(nlwp), "stat": stat, "cpu": float(cpu), "comm": comm})
return out
r = rows(PSL)
count = {}
for x in r:
count[x["pid"]] = count.get(x["pid"], 0) + 1
ok = all(count[x["pid"]] == x["nlwp"] for x in r)
print(",".join(str(k) + ":" + str(v) for k, v in sorted(count.items())) + "/" + str(ok))
全部评论