linux下syscall函数,SYS_gettid,SYS_tgkill

原文:http://blog.chinaunix.net/uid-28458801-id-4630215.html

NAME       
       syscall - 间接系统调用

SYNOPSIS       
       #define _GNU_SOURCE        
       #include
       #include                  /* For SYS_xxx definitions */

       int syscall(int number, ...);


DESCRIPTION    
       syscall() 执行一个系统调用,根据指定的参数number和所有系统调用的汇编语言接口来确定调用哪个系统调用。
       系统调用所使用的符号常量可以在头文件里面找到。



Linux中,每个进程有一个pid,类型pid_t,由getpid()取得。Linux下的POSIX线程也有一个id,类型 pthread_t,由pthread_self()取得,该id由线程库维护,其id空间是各个进程独立的(即 不同进程中的线程可能有相同的id)。Linux中的POSIX线程库实现的线程其实也是一个进程(LWP),只是该进程与主进程(启动线程的进程)共享一些资源而已,比如代码段,数据段等。
有时候我们可能需要知道线程的真实pid。比如进程P1要向另外一个进程P2中的某个线程发送信号时,既不能使用P2的pid,更不能使用线程的pthread id,而只能使用该线程的真实pid,称为tid。
有一个函数gettid()可以得到tid,但glibc并没有实现该函数,只能通过Linux的系统调用syscall来获取。

测试实例:

点击(此处)折叠或打开

  1. #include <unistd.h>
  2. #include <sys/syscall.h>
  3. #include <sys/types.h>
  4. #include <signal.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>

  7. int
  8. main(int argc, char *argv[])
  9. {
  10.      pid_t tid;

  11.      tid = syscall(SYS_gettid);
  12.      printf("tid : %d\n",tid);

  13.      tid = syscall(SYS_tgkill, getpid(), tid, SIGHUP);
  14.      printf("tid : %d ...\n",tid);
  15. }

测试结果:


SYS_*

点击(此处)折叠或打开

  1. /* Generated at libc build time from kernel syscall list. */

  2. #ifndef _SYSCALL_H
  3. # error "Never use directly; include instead."
  4. #endif

  5. #include <bits/wordsize.h>

  6. #define SYS__sysctl __NR__sysctl
  7. #define SYS_access __NR_access
  8. #define SYS_acct __NR_acct
  9. #define SYS_add_key __NR_add_key
  10. #define SYS_adjtimex __NR_adjtimex
  11. #define SYS_afs_syscall __NR_afs_syscall
  12. #define SYS_alarm __NR_alarm
  13. #define SYS_brk __NR_brk
  14. #define SYS_capget __NR_capget
  15. #define SYS_capset __NR_capset
  16. #define SYS_chdir __NR_chdir
  17. #define SYS_chmod __NR_chmod
  18. #define SYS_chown __NR_chown
  19. #define SYS_chroot __NR_chroot
  20. #define SYS_clock_getres __NR_clock_getres
  21. #define SYS_clock_gettime __NR_clock_gettime
  22. #define SYS_clock_nanosleep __NR_clock_nanosleep
  23. #define SYS_clock_settime __NR_clock_settime
  24. #define SYS_clone __NR_clone
  25. #define SYS_close __NR_close
  26. #define SYS_creat __NR_creat
  27. #define SYS_create_module __NR_create_module
  28. #define SYS_delete_module __NR_delete_module
  29. #define SYS_dup __NR_dup
  30. #define SYS_dup2 __NR_dup2
  31. #define SYS_epoll_create __NR_epoll_create
  32. #define SYS_epoll_ctl __NR_epoll_ctl
  33. #define SYS_epoll_wait __NR_epoll_wait
  34. #define SYS_eventfd __NR_eventfd
  35. #define SYS_execve __NR_execve
  36. #define SYS_exit __NR_exit
  37. #define SYS_exit_group __NR_exit_group
  38. #define SYS_faccessat __NR_faccessat
  39. #define SYS_fadvise64 __NR_fadvise64
  40. #define SYS_fallocate __NR_fallocate
  41. #define SYS_fchdir __NR_fchdir
  42. #define SYS_fchmod __NR_fchmod
  43. #define SYS_fchmodat __NR_fchmodat
  44. #define SYS_fchown __NR_fchown
  45. #define SYS_fchownat __NR_fchownat
  46. #define SYS_fcntl __NR_fcntl
  47. #define SYS_fdatasync __NR_fdatasync
  48. #define SYS_fgetxattr __NR_fgetxattr
  49. #define SYS_flistxattr __NR_flistxattr
  50. #define SYS_flock __NR_flock
  51. #define SYS_fork __NR_fork
  52. #define SYS_fremovexattr __NR_fremovexattr
  53. #define SYS_fsetxattr __NR_fsetxattr
  54. #define SYS_fstat __NR_fstat
  55. #define SYS_fstatfs __NR_fstatfs
  56. #define SYS_fsync __NR_fsync
  57. #define SYS_ftruncate __NR_ftruncate
  58. #define SYS_futex __NR_futex
  59. #define SYS_futimesat __NR_futimesat
  60. #define SYS_get_kernel_syms __NR_get_kernel_syms
  61. #define SYS_get_mempolicy __NR_get_mempolicy
  62. #define SYS_get_robust_list __NR_get_robust_list
  63. #define SYS_get_thread_area __NR_get_thread_area
  64. #define SYS_getcwd __NR_getcwd
  65. #define SYS_getdents __NR_getdents
  66. #define SYS_getdents64 __NR_getdents64
  67. #define SYS_getegid __NR_getegid
  68. #define SYS_geteuid __NR_geteuid
  69. #define SYS_getgid __NR_getgid
  70. #define SYS_getgroups __NR_getgroups
  71. #define SYS_getitimer __NR_getitimer
  72. #define SYS_getpgid __NR_getpgid
  73. #define SYS_getpgrp __NR_getpgrp
  74. #define SYS_getpid __NR_getpid
  75. #define SYS_getpmsg __NR_getpmsg
  76. #define SYS_getppid __NR_getppid
  77. #define SYS_getpriority __NR_getpriority
  78. #define SYS_getresgid __NR_getresgid
  79. #define SYS_getresuid __NR_getresuid
  80. #define SYS_getrlimit __NR_getrlimit
  81. #define SYS_getrusage __NR_getrusage
  82. #define SYS_getsid __NR_getsid
  83. #define SYS_gettid __NR_gettid
  84. #define SYS_gettimeofday __NR_gettimeofday
  85. #define SYS_getuid __NR_getuid
  86. #define SYS_getxattr __NR_getxattr
  87. #define SYS_init_module __NR_init_module
  88. #define SYS_inotify_add_watch __NR_inotify_add_watch
  89. #define SYS_inotify_init __NR_inotify_init
  90. #define SYS_inotify_rm_watch __NR_inotify_rm_watch
  91. #define SYS_io_cancel __NR_io_cancel
  92. #define SYS_io_destroy __NR_io_destroy
  93. #define SYS_io_getevents __NR_io_getevents
  94. #define SYS_io_setup __NR_io_setup
  95. #define SYS_io_submit __NR_io_submit
  96. #define SYS_ioctl __NR_ioctl
  97. #define SYS_ioperm __NR_ioperm
  98. #define SYS_iopl __NR_iopl
  99. #define SYS_ioprio_get __NR_ioprio_get
  100. #define SYS_ioprio_set __NR_ioprio_set
  101. #define SYS_kexec_load __NR_kexec_load
  102. #define SYS_keyctl __NR_keyctl
  103. #define SYS_kill __NR_kill
  104. #define SYS_lchown __NR_lchown
  105. #define SYS_lgetxattr __NR_lgetxattr
  106. #define SYS_link __NR_link
  107. #define SYS_linkat __NR_linkat
  108. #define SYS_listxattr __NR_listxattr
  109. #define SYS_llistxattr __NR_llistxattr
  110. #define SYS_lookup_dcookie __NR_lookup_dcookie
  111. #define SYS_lremovexattr __NR_lremovexattr
  112. #define SYS_lseek __NR_lseek
  113. #define SYS_lsetxattr __NR_lsetxattr
  114. #define SYS_lstat __NR_lstat
  115. #define SYS_madvise __NR_madvise
  116. #define SYS_mbind __NR_mbind
  117. #define SYS_migrate_pages __NR_migrate_pages
  118. #define SYS_mincore __NR_mincore
  119. #define SYS_mkdir __NR_mkdir
  120. #define SYS_mkdirat __NR_mkdirat
  121. #define SYS_mknod __NR_mknod
  122. #define SYS_mknodat __NR_mknodat
  123. #define SYS_mlock __NR_mlock
  124. #define SYS_mlockall __NR_mlockall
  125. #define SYS_mmap __NR_mmap
  126. #define SYS_modify_ldt __NR_modify_ldt
  127. #define SYS_mount __NR_mount
  128. #define SYS_move_pages __NR_move_pages
  129. #define SYS_mprotect __NR_mprotect
  130. #define SYS_mq_getsetattr __NR_mq_getsetattr
  131. #define SYS_mq_notify __NR_mq_notify
  132. #define SYS_mq_open __NR_mq_open
  133. #define SYS_mq_timedreceive __NR_mq_timedreceive
  134. #define SYS_mq_timedsend __NR_mq_timedsend
  135. #define SYS_mq_unlink __NR_mq_unlink
  136. #define SYS_mremap __NR_mremap
  137. #define SYS_msync __NR_msync
  138. #define SYS_munlock __NR_munlock
  139. #define SYS_munlockall __NR_munlockall
  140. #define SYS_munmap __NR_munmap
  141. #define SYS_nanosleep __NR_nanosleep
  142. #define SYS_nfsservctl __NR_nfsservctl
  143. #define SYS_open __NR_open
  144. #define SYS_openat __NR_openat
  145. #define SYS_pause __NR_pause
  146. #define SYS_personality __NR_personality
  147. #define SYS_pipe __NR_pipe
  148. #define SYS_pivot_root __NR_pivot_root
  149. #define SYS_poll __NR_poll
  150. #define SYS_ppoll __NR_ppoll
  151. #define SYS_prctl __NR_prctl
  152. #define SYS_pread64 __NR_pread64
  153. #define SYS_pselect6 __NR_pselect6
  154. #define SYS_ptrace __NR_ptrace
  155. #define SYS_putpmsg __NR_putpmsg
  156. #define SYS_pwrite64 __NR_pwrite64
  157. #define SYS_query_module __NR_query_module
  158. #define SYS_quotactl __NR_quotactl
  159. #define SYS_read __NR_read
  160. #define SYS_readahead __NR_readahead
  161. #define SYS_readlink __NR_readlink
  162. #define SYS_readlinkat __NR_readlinkat
  163. #define SYS_readv __NR_readv
  164. #define SYS_reboot __NR_reboot
  165. #define SYS_remap_file_pages __NR_remap_file_pages
  166. #define SYS_removexattr __NR_removexattr
  167. #define SYS_rename __NR_rename
  168. #define SYS_renameat __NR_renameat
  169. #define SYS_request_key __NR_request_key
  170. #define SYS_restart_syscall __NR_restart_syscall
  171. #define SYS_rmdir __NR_rmdir
  172. #define SYS_rt_sigaction __NR_rt_sigaction
  173. #define SYS_rt_sigpending __NR_rt_sigpending
  174. #define SYS_rt_sigprocmask __NR_rt_sigprocmask
  175. #define SYS_rt_sigqueueinfo __NR_rt_sigqueueinfo
  176. #define SYS_rt_sigreturn __NR_rt_sigreturn
  177. #define SYS_rt_sigsuspend __NR_rt_sigsuspend
  178. #define SYS_rt_sigtimedwait __NR_rt_sigtimedwait
  179. #define SYS_sched_get_priority_max __NR_sched_get_priority_max
  180. #define SYS_sched_get_priority_min __NR_sched_get_priority_min
  181. #define SYS_sched_getaffinity __NR_sched_getaffinity
  182. #define SYS_sched_getparam __NR_sched_getparam
  183. #define SYS_sched_getscheduler __NR_sched_getscheduler
  184. #define SYS_sched_rr_get_interval __NR_sched_rr_get_interval
  185. #define SYS_sched_setaffinity __NR_sched_setaffinity
  186. #define SYS_sched_setparam __NR_sched_setparam
  187. #define SYS_sched_setscheduler __NR_sched_setscheduler
  188. #define SYS_sched_yield __NR_sched_yield
  189. #define SYS_select __NR_select
  190. #define SYS_sendfile __NR_sendfile
  191. #define SYS_set_mempolicy __NR_set_mempolicy
  192. #define SYS_set_robust_list __NR_set_robust_list
  193. #define SYS_set_thread_area __NR_set_thread_area
  194. #define SYS_set_tid_address __NR_set_tid_address
  195. #define SYS_setdomainname __NR_setdomainname
  196. #define SYS_setfsgid __NR_setfsgid
  197. #define SYS_setfsuid __NR_setfsuid
  198. #define SYS_setgid __NR_setgid
  199. #define SYS_setgroups __NR_setgroups
  200. #define SYS_sethostname __NR_sethostname
  201. #define SYS_setitimer __NR_setitimer
  202. #define SYS_setpgid __NR_setpgid
  203. #define SYS_setpriority __NR_setpriority
  204. #define SYS_setregid __NR_setregid
  205. #define SYS_setresgid __NR_setresgid
  206. #define SYS_setresuid __NR_setresuid
  207. #define SYS_setreuid __NR_setreuid
  208. #define SYS_setrlimit __NR_setrlimit
  209. #define SYS_setsid __NR_setsid
  210. #define SYS_settimeofday __NR_settimeofday
  211. #define SYS_setuid __NR_setuid
  212. #define SYS_setxattr __NR_setxattr
  213. #define SYS_sigaltstack __NR_sigaltstack
  214. #define SYS_splice __NR_splice
  215. #define SYS_stat __NR_stat
  216. #define SYS_statfs __NR_statfs
  217. #define SYS_swapoff __NR_swapoff
  218. #define SYS_swapon __NR_swapon
  219. #define SYS_symlink __NR_symlink
  220. #define SYS_symlinkat __NR_symlinkat
  221. #define SYS_sync __NR_sync
  222. #define SYS_sync_file_range __NR_sync_file_range
  223. #define SYS_sysfs __NR_sysfs
  224. #define SYS_sysinfo __NR_sysinfo
  225. #define SYS_syslog __NR_syslog
  226. #define SYS_tee __NR_tee
  227. #define SYS_tgkill __NR_tgkill
  228. #define SYS_time __NR_time
  229. #define SYS_timer_create __NR_timer_create
  230. #define SYS_timer_delete __NR_timer_delete
  231. #define SYS_timer_getoverrun __NR_timer_getoverrun
  232. #define SYS_timer_gettime __NR_timer_gettime
  233. #define SYS_timer_settime __NR_timer_settime
  234. #define SYS_times __NR_times
  235. #define SYS_tkill __NR_tkill
  236. #define SYS_truncate __NR_truncate
  237. #define SYS_umask __NR_umask
  238. #define SYS_umount2 __NR_umount2
  239. #define SYS_uname __NR_uname
  240. #define SYS_unlink __NR_unlink
  241. #define SYS_unlinkat __NR_unlinkat
  242. #define SYS_unshare __NR_unshare
  243. #define SYS_uselib __NR_uselib
  244. #define SYS_ustat __NR_ustat
  245. #define SYS_utime __NR_utime
  246. #define SYS_utimes __NR_utimes
  247. #define SYS_vfork __NR_vfork
  248. #define SYS_vhangup __NR_vhangup
  249. #define SYS_vmsplice __NR_vmsplice
  250. #define SYS_vserver __NR_vserver
  251. #define SYS_wait4 __NR_wait4
  252. #define SYS_waitid __NR_waitid
  253. #define SYS_write __NR_write
  254. #define SYS_writev __NR_writev
  255. #if __WORDSIZE == 64
  256. #define SYS_accept __NR_accept
  257. #define SYS_arch_prctl __NR_arch_prctl
  258. #define SYS_bind __NR_bind
  259. #define SYS_connect __NR_connect
  260. #define SYS_epoll_ctl_old __NR_epoll_ctl_old
  261. #define SYS_epoll_pwait __NR_epoll_pwait
  262. #define SYS_epoll_wait_old __NR_epoll_wait_old
  263. #define SYS_getpeername __NR_getpeername
  264. #define SYS_getsockname __NR_getsockname
  265. #define SYS_getsockopt __NR_getsockopt
  266. #define SYS_listen __NR_listen
  267. #define SYS_msgctl __NR_msgctl
  268. #define SYS_msgget __NR_msgget
  269. #define SYS_msgrcv __NR_msgrcv
  270. #define SYS_msgsnd __NR_msgsnd
  271. #define SYS_newfstatat __NR_newfstatat
  272. #define SYS_recvfrom __NR_recvfrom
  273. #define SYS_recvmsg __NR_recvmsg
  274. #define SYS_security __NR_security
  275. #define SYS_semctl __NR_semctl
  276. #define SYS_semget __NR_semget
  277. #define SYS_semop __NR_semop
  278. #define SYS_semtimedop __NR_semtimedop
  279. #define SYS_sendmsg __NR_sendmsg
  280. #define SYS_sendto __NR_sendto
  281. #define SYS_setsockopt __NR_setsockopt
  282. #define SYS_shmat __NR_shmat
  283. #define SYS_shmctl __NR_shmctl
  284. #define SYS_shmdt __NR_shmdt
  285. #define SYS_shmget __NR_shmget
  286. #define SYS_shutdown __NR_shutdown
  287. #define SYS_signalfd __NR_signalfd
  288. #define SYS_socket __NR_socket
  289. #define SYS_socketpair __NR_socketpair
  290. #define SYS_timerfd_create __NR_timerfd_create
  291. #define SYS_tuxcall __NR_tuxcall
  292. #define SYS_utimensat __NR_utimensat
  293. #else
  294. #define SYS__llseek __NR__llseek
  295. #define SYS__newselect __NR__newselect
  296. #define SYS_bdflush __NR_bdflush
  297. #define SYS_break __NR_break
  298. #define SYS_chown32 __NR_chown32
  299. #define SYS_fadvise64_64 __NR_fadvise64_64
  300. #define SYS_fchown32 __NR_fchown32
  301. #define SYS_fcntl64 __NR_fcntl64
  302. #define SYS_fstat64 __NR_fstat64
  303. #define SYS_fstatat64 __NR_fstatat64
  304. #define SYS_fstatfs64 __NR_fstatfs64
  305. #define SYS_ftime __NR_ftime
  306. #define SYS_ftruncate64 __NR_ftruncate64
  307. #define SYS_getcpu __NR_getcpu
  308. #define SYS_getegid32 __NR_getegid32
  309. #define SYS_geteuid32 __NR_geteuid32
  310. #define SYS_getgid32 __NR_getgid32
  311. #define SYS_getgroups32 __NR_getgroups32
  312. #define SYS_getresgid32 __NR_getresgid32
  313. #define SYS_getresuid32 __NR_getresuid32
  314. #define SYS_getuid32 __NR_getuid32
  315. #define SYS_gtty __NR_gtty
  316. #define SYS_idle __NR_idle
  317. #define SYS_ipc __NR_ipc
  318. #define SYS_lchown32 __NR_lchown32
  319. #define SYS_lock __NR_lock
  320. #define SYS_lstat64 __NR_lstat64
  321. #define SYS_madvise1 __NR_madvise1
  322. #define SYS_mmap2 __NR_mmap2
  323. #define SYS_mpx __NR_mpx
  324. #define SYS_nice __NR_nice
  325. #define SYS_oldfstat __NR_oldfstat
  326. #define SYS_oldlstat __NR_oldlstat
  327. #define SYS_oldolduname __NR_oldolduname
  328. #define SYS_oldstat __NR_oldstat
  329. #define SYS_olduname __NR_olduname
  330. #define SYS_prof __NR_prof
  331. #define SYS_profil __NR_profil
  332. #define SYS_readdir __NR_readdir
  333. #define SYS_sendfile64 __NR_sendfile64
  334. #define SYS_setfsgid32 __NR_setfsgid32
  335. #define SYS_setfsuid32 __NR_setfsuid32
  336. #define SYS_setgid32 __NR_setgid32
  337. #define SYS_setgroups32 __NR_setgroups32
  338. #define SYS_setregid32 __NR_setregid32
  339. #define SYS_setresgid32 __NR_setresgid32
  340. #define SYS_setresuid32 __NR_setresuid32
  341. #define SYS_setreuid32 __NR_setreuid32
  342. #define SYS_setuid32 __NR_setuid32
  343. #define SYS_sgetmask __NR_sgetmask
  344. #define SYS_sigaction __NR_sigaction
  345. #define SYS_signal __NR_signal
  346. #define SYS_sigpending __NR_sigpending
  347. #define SYS_sigprocmask __NR_sigprocmask
  348. #define SYS_sigreturn __NR_sigreturn
  349. #define SYS_sigsuspend __NR_sigsuspend
  350. #define SYS_socketcall __NR_socketcall
  351. #define SYS_ssetmask __NR_ssetmask
  352. #define SYS_stat64 __NR_stat64
  353. #define SYS_statfs64 __NR_statfs64
  354. #define SYS_stime __NR_stime
  355. #define SYS_stty __NR_stty
  356. #define SYS_truncate64 __NR_truncate64
  357. #define SYS_ugetrlimit __NR_ugetrlimit
  358. #define SYS_ulimit __NR_ulimit
  359. #define SYS_umount __NR_umount
  360. #define SYS_vm86 __NR_vm86
  361. #define SYS_vm86old __NR_vm86old
  362. #define SYS_waitpid __NR_waitpid
  363. #endif

你可能感兴趣的:(C++)