gpt4 book ai didi

C 程序从父 id 获取子进程 id (minix)

转载 作者:太空宇宙 更新时间:2023-11-04 04:26:09 25 4
gpt4 key购买 nike

<分区>

因此,给定一个传递给系统调用的进程 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;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com