Search code examples
cstructoperating-systemminix

Printing child processes given a pid (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.

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); 
}

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.


Solution

  • 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 \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;
    }