【问题标题】:Printing child processes given a pid (MINIX)打印给定 pid 的子进程(MINIX)
【发布时间】:2016-12-06 21:53:55
【问题描述】:

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

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

struct task_struct *task; 
struct list_head *list;

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

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

struct task_struct find_task_by_pid(pid_t pid);

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

【问题讨论】:

    标签: c struct operating-system minix


    【解决方案1】:

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

    #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 \n", 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", n); //Print the childs id
        c++;
        }
        else
        {
        c++;
        }
      }
    return -1;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 2023-04-04
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2015-12-23
      相关资源
      最近更新 更多