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