Unix parent process source pointer is not what I expect

I am writing kernel code for FreeBSD for assignment, and I have a problem with processes and their parents.

My driver code is as follows:

    int pid ;
    const char *sem_name  = "Semaphore1\0";

    pid = fork();

    if (pid == -1) {
        printf("ERROR FAILED TO FORK.");
        return 1;
    } else if (pid == 0) {
        // child
        sleep(10);
        syscall( 293, sem_name );
    } else {
       // parent
       syscall( 292, sem_name, 6 );
       syscall( 293, sem_name );

    }

    return 0;

And my syscall (293), which outputs the parent pids, looks like this:

  struct proc        *parent_ptr;    // holds pointer to parent

  err = copyinstr( SCARG(uap, name), &kname, MAX_STR_LENGTH, &knamesize );
  if (err == EFAULT)
    return( err );

  // p is the pointer to the proc that called the syscall
  uprintf("Current process pid: %d\n", p->p_pid);

  parent_ptr = p;
  n_sem = malloc( (u_long)sizeof( struct n_semaphore ), M_FREE, M_NOWAIT );

  while (parent_ptr->p_pptr != NULL) {
     uprintf("parentid : %d\n", parent_ptr->p_pid);
     LIST_FOREACH( n_sem, &parent_ptr->p_semaphores, next ) {
         uprintf("-sem name: %s count %d\n", n_sem->name, n_sem->count);
     }
     parent_ptr = parent_ptr->p_pptr;
  }

The result that I get from the driver looks like this:

// parent 
Current process pid: 4682
parentid : 4682
parentid : 10895
parentid : 1
// child
Current process pid: 18710
parentid : 18710
parentid : 1   

Syscall 292 just created a semaphore and adds it to the process semaphore queue, but this is not a problem for me, so I left it.

Does the child process seem to have a smaller parent tree than the process that expands it? It makes no sense to me. Any clarification would be greatly appreciated.

+4
source share

Source: https://habr.com/ru/post/1618056/


All Articles