简体   繁体   中英

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

I'm working on a deadlock detection algorithm and I'm only given kernel level libraries, ie #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 ?

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!

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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM