views:

1347

answers:

2

I'm working on a deadlock detection algorithm and I'm only given kernel level libraries, i.e. #include <linux/somelibrary> and nothing else. Are there kernel-level facilities that will allow me to get the pid of the current process similar to getpid() of unistd.h?

+4  A: 

This question makes little sense.

Are you writing kernel-based code? In which case you can get the pid of the current task by using the "current" macro which points to the current task's task struct (which contains a member with the pid). That would only work if your kernel code is running in a context where a "current task" makes sense (i.e. not an interrupt, tasklet etc).

If you're writing userspace code, there should be no reason you can't call getpid, which is a library call from the C library defined in unistd.h (or something it includes), which makes the system call. If there is such a reason, please explain it.

Making a system call in Linux isn't particularly difficult, but does involve architecture-specific code that you don't want to write.

MarkR
Hi, thanks for getting back to me so quickly.It is kernel-based code. Can you elaborate more on the current macro and how it's used? You are correct in saying that it's not userspace code and I can't access unistd.h.
+2  A: 

Hi,

I did some quick research and I found the answer. Thanks so much for your direction. The quick code I used was:

printf("My current process id/pid is %d\n", current->pid);

Thanks again!

props for filling us in on how you did it
Matt Joiner