打印给定 pid 的子进程(MINIX) [英] Printing child processes given a pid (MINIX)

查看:16
本文介绍了打印给定 pid 的子进程(MINIX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在做一个项目,作为其中的一部分,我需要在 MINIX 中实现系统调用/库函数.

I'm working on a project at the moment and as part of it I need to implement system calls/library functions in MINIX.

作为其中的一部分,我需要能够使用其 pid 打印给定进程的子进程列表.我想我已经找到了我需要的部分内容,但我坚持让它与给定的 pid 一起工作.

As part of this I need to be able to print a list of child processes of a given process, using its pid. I think I've found part of what I need, but I'm stuck with making it work with a given pid.

struct task_struct *task; 
struct list_head *list;

list_for_each(list, &current->children) { 
    task = list_entry(list, struct task_struct, children); 
}

这看起来和我需要的很接近吗?我知道要传递一个 pid 供我使用,我需要使用:

Does this seem like something close to what I'd need? I know that for passing a pid for me to use I'd need to use:

struct task_struct find_task_by_pid(pid_t pid);

但是将它与上述结合起来并不是我以前做过的事情.

But combining this with the above isn't something I've done before.

推荐答案

我想通了,对我来说似乎效率不高,但确实有效.

I figured it out, doesn't seem too efficient to me but it works.

#include <stdio.h>
#include "pm.h"
#include "mproc.h"

int do_printchildpids(){
int i = m_in.m1_i1; //pid received 
int c = 0; //Counter


printf("Searching for children of process: %d 
", i);

while (c < NR_PROCS)
  {
    int n = mproc[c].mp_pid; //First process in the list of availableprocess
    int pinx = mproc[c].mp_parent; //Index of parent of the current process
    int ppid = mproc[pinx].mp_pid; //pid of parent process


    if(i == ppid) //If parents pid matches the given value
    {
    printf("%d 
", n); //Print the childs id
    c++;
    }
    else
    {
    c++;
    }
  }
return -1;
}

这篇关于打印给定 pid 的子进程(MINIX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆