Getting the process id of the current process without unistd.h

I am working on a lock detection algorithm, and they only give me kernel level libraries, i.e. #include <linux/somelibrary> and nothing else. Are there kernel-level tools that let me get the pid of the current process, similar to getpid() unistd.h ?

+4
source share
2 answers

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!

+7
source

This question does not make sense.

Do you write kernel-based code? In this case, you can get the pid of the current task using the "current" macro, which points to the current task struct task (which contains the element with pid). This will only work if your kernel code is run in a context where the "current task" makes sense (i.e., not interruption, tasket, etc.).

If you are writing user space code, there should be no reason why you cannot call getpid, which is a library call from the C library defined in unistd.h (or something that it includes) that makes a system call. If there is such a reason, please explain it.

Making a system call on Linux is not particularly difficult, but includes architecture-specific code that you do not want to write.

+4
source

All Articles