C程序获取子进程id';s仅来自父id(minix)
因此,给定传递给系统调用的进程id,我需要返回所有子进程id。这必须用C写。我用mproc获取子pid的父进程,并列出某个索引中的所有进程,但无法从该索引跳到该索引 我的代码:C程序获取子进程id';s仅来自父id(minix),c,minix,C,Minix,因此,给定传递给系统调用的进程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()) { prin
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;
}
这是一个非常简陋的解决方案,但是如果您正在运行linux,并且希望获得父进程的子进程,那么您可以使用linux
pgrep
命令,通过使用fork()
和execv()
或通过一个简单的system()
调用。然后通过管道将输出传输到一个文件并读取该文件的内容
system("pgrep -P [parent_pid] 1>[outputfile]")
fp = fopen([outputfile], "r")
while (fscanf(fp, "%d", &child_pid) != EOF){
<add child_pid to a list you've made>
}
fclose(fp)
系统(“pgrep-P[parent\u pid]1>[outputfile]”)
fp=fopen([outputfile],“r”)
while(fscanf(fp、%d、&child_-pid)!=EOF){
这是一个非常简陋的解决方案,但是如果您正在运行linux,并且希望获得父进程的子进程,您可以使用linuxpgrep
命令,通过使用fork()
和execv()
或通过一个简单的system()
调用。然后将输出传输到一个文件并读取该文件的内容
system("pgrep -P [parent_pid] 1>[outputfile]")
fp = fopen([outputfile], "r")
while (fscanf(fp, "%d", &child_pid) != EOF){
<add child_pid to a list you've made>
}
fclose(fp)
系统(“pgrep-P[parent\u pid]1>[outputfile]”)
fp=fopen([outputfile],“r”)
while(fscanf(fp、%d、&child_-pid)!=EOF){
不要注释掉您希望我们帮助的代码,语法荧光笔会使其难以阅读。@Barmar道歉-已编辑。不要注释掉您希望我们帮助的代码,语法荧光笔会使其难以阅读。@Barmar道歉-已编辑。