【发布时间】:2016-12-16 20:54:34
【问题描述】:
所以给定一个传递给系统调用的进程 ID,我需要返回所有子进程 ID。这必须用 C 语言编写。我使用 mproc 来获取子 pid 的父进程并列出某个索引中的所有进程,但无法从该索引跳转到该索引。
我的代码:
int do_getchildpids() {
// get pid from parameter
int ppid = m_in.m1_i1;
// Get the child pid of this process
pid_t cpid;
if (cpid == fork()) {
printf("Child pid for ppid %d is %d\n", ppid, getpid());
}
// ** Other attempt at this problem below **
// if mp_parent == ppid then print pid
int idx = 0;
int cpid = mproc[idx].mp_pid;
while (mproc[idx].mp_pid) {
idx++;
}
printf("Searching for children of %d...\n", ppid);
if (pid == 0) {
// for loop that gets the ppid, checks against given ppid
// prints out pid if true
if (cpid) {
// loop through proc table checking if ppid is equal to parameter passed
if (ppid == mproc[mproc[i].mp_parent].mp_pid)
printf("Child pid is %d.\n", getpid());
}
printf("Child pid is: %d.\n", getpid());
} else {
printf("Error, child pid was not set or is -1\n");
}
return 0;
}
【问题讨论】:
-
不要注释掉需要我们帮助的代码,语法高亮会让人难以阅读。
-
@Barmar 道歉 - 已编辑。