??????λ????????PID??process ID???
????????????
????#include <unistd.h>
????pid_t getpid(void);
????????????????????????????????????
????find /usr/include -name unistd.h
????/usr/include/asm/unistd.h
????/usr/include/bits/unistd.h
????/usr/include/linux/unistd.h
????/usr/include/sys/unistd.h
????/usr/include/unistd.h
????cat /usr/include/unistd.h | grep getpid
????/* Get the process ID of the calling process.  */
????extern __pid_t getpid (void) __THROW;
??????λ???????TID??thread ID???
?????????man?????????????
????(1) The gettid() system call first appeared on Linux in kernel 2.4.11.
????(2) gettid() returns the thread ID of the current process. This is equal to the process ID (as returned by getpid(2))?? unless the process is part of a thread group (created by specifying the CLONE_THREAD flag to the clone(2) system call). All processes in the same thread group have the same PID?? but each one has a unique TID.
????(3) gettid() is Linux specific and should not be used in programs that are intended to be portable. ???????????????????????????
???????????man???????????????????????????????“error?? undefined reference to `gettid'”???????????????????????????????
????http://www.kernel.org/doc/man-pages/online/pages/man2/gettid.2.html
????(1) Glibc does not provide a wrapper for this system call; call it using syscall(2).?????Glibc???????????????????????????????????????????????????????????????
????(2) The thread ID returned by this call is not the same thing as a POSIX thread ID (i.e.?? the opaque value returned by pthread_self(3)).
????????/usr/include/sys/syscall.h???????/usr/include/asm/unistd.h??????????????????system call number??
????#define __NR_gettid     224
???????????????????TID??nasty???????
????#include <sys/syscall.h>
????printf("The ID of this thread is: %ld "?? (long int)syscall(224));
??????????elegant???????
#include <sys/syscall.h>
#define gettidv1() syscall(__NR_gettid)
#define gettidv2() syscall(SYS_gettid)
printf("The ID of this thread is: %ld "?? (long int)gettidv1());// ?μ???
printf("The ID of this thread is: %ld "?? (long int)gettidv2());// traditional form
????PS: ??/usr/include/sys/syscall.h?п??????????__NR_<name>??SYS_<name>??????????????????????__NR_<name>??
// /usr/include/bits/syscall.h
#define SYS_gettid __NR_gettid
#ifndef _LIBC
/* The Linux kernel header file defines macros `__NR_<name>'?? but some
programs expect the traditional form `SYS_<name>'.  So in building libc
we scan the kernel's list and produce <bits/syscall.h> with macros for
all the `SYS_' names.  */
# include <bits/syscall.h>
#endif
???????TID?????????????
??????????pid
????(1) ps ux | grep prog_name
????(2) pgrep prog_name
?????????tid
????(1) ps -efL | grep prog_name
????(2) ls /proc/pid/task