系统调用应用层 demo

Posted Li-Yongjun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了系统调用应用层 demo相关的知识,希望对你有一定的参考价值。

x86 平台

syscall.c

#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>

int main(int argc, char *argv[])

	int pid;

	pid = syscall(39);
	printf("pid = %d\\n", pid);

	return 0;

$ gcc syscall.c -o syscall.out 
$ ./syscall.out 
pid = 16386

arm 平台

syscall.c

#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>

int main(int argc, char *argv[])

	int pid;

	pid = syscall(20);
	printf("pid = %d\\n", pid);

	return 0;

Makelfile

CC=/home/liyongjun/project/board/buildroot/RPi3/host/bin/arm-buildroot-linux-gnueabihf-gcc

TARGET=syscall

all:
	$CC $TARGET.c -o $TARGET.out

clean:
	rm *.out

# tftp -gr syscall.out 192.168.31.224 && chmod +x syscall.out
# ./syscall.out 
pid = 205

区别

可以看到,同样是 getpid 系统调用,在 x86 平台上系统调用号是 39,在 arm 平台上是 20。
系统调用号可以在内核源码中找到
x86: arch/x86/entry/syscalls/syscall_64.tbl

#
# 64-bit system call numbers and entry vectors
#
# The format is:
# <number> <abi> <name> <entry point>
#
# The __x64_sys_*() stubs are created on-the-fly for sys_*() system calls
#
# The abi is "common", "64" or "x32" for this file.
#
0	common	read			sys_read
1	common	write			sys_write
2	common	open			sys_open
3	common	close			sys_close
4	common	stat			sys_newstat
5	common	fstat			sys_newfstat
6	common	lstat			sys_newlstat
7	common	poll			sys_poll
8	common	lseek			sys_lseek
9	common	mmap			sys_mmap
10	common	mprotect		sys_mprotect
11	common	munmap			sys_munmap
12	common	brk			sys_brk
13	64	rt_sigaction		sys_rt_sigaction
14	common	rt_sigprocmask		sys_rt_sigprocmask
15	64	rt_sigreturn		sys_rt_sigreturn
16	64	ioctl			sys_ioctl
17	common	pread64			sys_pread64
18	common	pwrite64		sys_pwrite64
19	64	readv			sys_readv
20	64	writev			sys_writev
21	common	access			sys_access
22	common	pipe			sys_pipe
23	common	select			sys_select
24	common	sched_yield		sys_sched_yield
25	common	mremap			sys_mremap
26	common	msync			sys_msync
27	common	mincore			sys_mincore
28	common	madvise			sys_madvise
29	common	shmget			sys_shmget
30	common	shmat			sys_shmat
31	common	shmctl			sys_shmctl
32	common	dup			sys_dup
33	common	dup2			sys_dup2
34	common	pause			sys_pause
35	common	nanosleep		sys_nanosleep
36	common	getitimer		sys_getitimer
37	common	alarm			sys_alarm
38	common	setitimer		sys_setitimer
39	common	getpid			sys_getpid // getpid
40	common	sendfile		sys_sendfile64
41	common	socket			sys_socket
42	common	connect			sys_connect

arm: arch/arm/tools/syscall.tbl

#
# Linux system call numbers and entry vectors
#
# The format is:
# <num>	<abi>	<name>			[<entry point>			[<oabi compat entry point>]]
#
# Where abi is:
#  common - for system calls shared between oabi and eabi (may have compat)
#  oabi   - for oabi-only system calls (may have compat)
#  eabi   - for eabi-only system calls
#
# For each syscall number, "common" is mutually exclusive with oabi and eabi
#
0	common	restart_syscall		sys_restart_syscall
1	common	exit			sys_exit
2	common	fork			sys_fork
3	common	read			sys_read
4	common	write			sys_write
5	common	open			sys_open
6	common	close			sys_close
# 7 was sys_waitpid
8	common	creat			sys_creat
9	common	link			sys_link
10	common	unlink			sys_unlink
11	common	execve			sys_execve
12	common	chdir			sys_chdir
13	oabi	time			sys_time32
14	common	mknod			sys_mknod
15	common	chmod			sys_chmod
16	common	lchown			sys_lchown16
# 17 was sys_break
# 18 was sys_stat
19	common	lseek			sys_lseek
20	common	getpid			sys_getpid // getpid
21	common	mount			sys_mount
22	oabi	umount			sys_oldumount
23	common	setuid			sys_setuid16
24	common	getuid			sys_getuid16
25	oabi	stime			sys_stime32
26	common	ptrace			sys_ptrace
27	oabi	alarm			sys_alarm

以上是关于系统调用应用层 demo的主要内容,如果未能解决你的问题,请参考以下文章

webService初探(简单小demo)

RPC远程调用概念 &amp;&amp; demo实例

startActivityForResult的用法和demo

http程序接口调用(最入门级,文末附Demo)

Java中RMI远程调用demo

wangEditor 结合Netcore 实现图片上传 (IIS) Demo