# how to get the process data structure through pid?



## billconan (Jan 18, 2009)

hello guys,


i'm required to modify the process scheduling part of the freebsd kernel as part of our homework.

the homework needs us add a new variable to the process data structure, and the priority of the process will be having something to do with the variable.

to adjust the variable while the process is running, we also required to write a system call to modify the variable on the fly.

the problem is how to get the process data structure via the pid in the syscall routine?

i'm not familiar with bsd nor linux. i noticed that there is a function called find_task_by_pid that can do this, but i did find identical under freebsd.

my second question is, to finish this homework, we need to modify the runq_choose function defined under the kern_switch.c

problem is, according to the homework requirement, we want only the "real time" and "time sharing" processes to use the new scheduling strategy. but how can i know the type of the running queue in that function? i've noticed a variable called pri, is that a hint of the type of the running queue? 

thank you.


----------



## mjguzik (Jan 24, 2009)

billconan said:
			
		

> the problem is how to get the process data structure via the pid in the syscall routine?



You can use pfind - it returns locked process or NULL. Remember to iterate through all threads. (See sched_nice().)



			
				billconan said:
			
		

> my second question is, to finish this homework, we need to modify the runq_choose function defined under the kern_switch.c
> 
> problem is, according to the homework requirement, we want only the "real time" and "time sharing" processes to use the new scheduling strategy. but how can i know the type of the running queue in that function? i've noticed a variable called pri, is that a hint of the type of the running queue?



After taking a quick look I suggest the following (I'm not sure about correctness):
See pri_to_rtp() (sys/kern/kern_resource.c). I'm not sure if variable 'pri' is useful in this case. You can always check td_pri_class of the first thread.

Good luck.


----------



## kota (Mar 16, 2012)

*how to use pfind-*

Does pfind work on user space; if it works how to use it? If not how to use pfind in kernel space so that *I* can get process(proc) data stucture?


----------



## Maelstorm (Jul 17, 2012)

All of the functions that I have seen in the kernel source code that take calls from userland programs have the following general format in the function definition:


```
int function_name(struct thread *td, *pargs)
```

Take a look at the thread structure data type in /usr/src/sys/sys/proc.h  In FreeBSD (and possibly Linux as well), when execution enters the system call handler function, the function has access to the information about the thread making the call, which in turn leads to the process structure.  However, if you want to locate the PID of another thread, then you will need to look at the pidhash table.  As for pfind, it is a kernel function and must be used from kernel space.  Its definition is also located in proc.h.  There's a manual page on it too: man 9 pfind.


----------



## Maelstorm (Jul 17, 2012)

Maelstorm said:
			
		

> All of the functions that I have seen in the kernel source code that take calls from userland programs have the following general format in the function definition:
> 
> 
> ```
> ...



EDIT: Added void keyword to *pargs.


----------

