我正在为FreeBSD编写内核代码进行分配,而且我遇到了进程及其父进程的问题。
我的驱动程序代码如下所示:
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;
我输出父pid的系统调用(293)如下所示:
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;
}
我从驱动程序获得的输出如下:
// parent
Current process pid: 4682
parentid : 4682
parentid : 10895
parentid : 1
// child
Current process pid: 18710
parentid : 18710
parentid : 1
Syscall 292刚刚创建了信号量并将其添加到进程信号量队列中,但这对我来说不是问题所以我把它留了出来。
看起来子进程的父树比分叉的进程要小?这对我来说没有意义。任何澄清将不胜感激。