嵌入式linux使用rpc实现多任务过程调用

2019-07-12 15:41发布

在嵌入式linux应用产品开发过程中,由于应用程序使用了多进程开发模式,在软件平台为应用程序提供API接口时,会涉及到接口函数多任务调度支持的问题。
linux的每个进程有自己独立的地址空间,如果API接口函数在多个应用进程中执行,则不能使用全局变量进行数据存储,并且会涉及到很多临界资源保护和读写访问冲突等方面的问题。为了简化API接口函数的开发,并为应用程序提供简单的API接口函数,在API接口函数实现中,添加RPC机制实现多任务的本地过程调用,支持多进程模式的API接口函数支持。
RPC(Remote Procedure Call Protocol)的本意是一种远程过程调用机制,通过网络调用远程设备的过程函数。在Linux中,没有提供与RPC对应的本地过程调用机制,所以我们直接用RPC来进行本地的过程调用,实现对多任务应用开发的支持。
RPC的开发可以通过rpcgen工具实现,rpcgen可以自动生成RPC开发需要的大多数程序代码。rpcgen需要一个符合规格的说明文件作为输出,在给文件中可以定义API函数接口,数据结构等内容。rpcgen的说明文件示例如下:
program rpc_test{ version VERSION{ int rpc_test_get_status(void) = 1; int rpc_test_check(void) = 2; int rpc_test_set_val(int,int) = 3; } = 1; } = 1;
说明文件编写完成后,可以使用rpcgen生成对应的程序代码,命令如下:
rm rpc_test_server.c rm rpc_test_client.c rpcgen -N -Sc -o rpc_test_client.c rpc_test.x rpcgen -N -Ss -o rpc_test_server.c rpc_test.x rpcgen -N rpc_test.x arm-linux-gnueabihf-gcc -Wall -o rpc_server rpc_test_server.c rpc_test_svc.c rpc_test_xdr.c arm-linux-gnueabihf-gcc -Wall -o rpc_client rpc_test_clnt.c rpc_test_client.c rpc_test_xdr.c gcc -Wall -o rpc_server_x86 rpc_test_server.c rpc_test_svc.c rpc_test_xdr.c gcc -Wall -o rpc_client_x86 rpc_test_clnt.c rpc_test_client.c rpc_test_xdr.c
RPC服务运行需要portmap服务支持,由于嵌入式系统中用来没有实现portmap机制,在执行rpc_server时,报错如下: Cannot register service: RPC: Unable to receive; errno = Connection refused unable to register (rpc_test, VERSION, udp). 下载portmap服务代码,编译生成portmap服务程序:
CC=arm-linux-gnueabihf-gcc ./configure --host=arm-linux 下载portmap程序到开发板并执行,rpc_server可以正常启动。 下载rpc_client程序到开发板,执行时报错如下: 127.0.0.1: RPC: Unknown protocol该错误是由于在/etc/下缺少配置文件protocols,创建文件,内容如下: # Internet (IP) protocols # # Updated from http://www.iana.org/assignments/protocol-numbers and other # sources. # New protocols will be added on request if they have been officially # assigned by IANA and are not historical. # If you need a huge list of used numbers please install the nmap package. ip 0 IP # internet protocol, pseudo protocol number #hopopt 0 HOPOPT # IPv6 Hop-by-Hop Option [RFC1883] icmp 1 ICMP # internet control message protocol igmp 2 IGMP # Internet Group Management ggp 3 GGP # gateway-gateway protocol ipencap 4 IP-ENCAP # IP encapsulated in IP (officially ``IP'') st 5 ST # ST datagram mode tcp 6 TCP # transmission control protocol egp 8 EGP # exterior gateway protocol igp 9 IGP # any private interior gateway (Cisco) pup 12 PUP # PARC universal packet protocol udp 17 UDP # user datagram protocol hmp 20 HMP # host monitoring protocol xns-idp 22 XNS-IDP # Xerox NS IDP rdp 27 RDP # "reliable datagram" protocol iso-tp4 29 ISO-TP4 # ISO Transport Protocol class 4 [RFC905] dccp 33 DCCP # Datagram Congestion Control Prot. [RFC4340] xtp 36 XTP # Xpress Transfer Protocol ddp 37 DDP # Datagram Delivery Protocol idpr-cmtp 38 IDPR-CMTP # IDPR Control Message Transport ipv6 41 IPv6 # Internet Protocol, version 6 ipv6-route 43 IPv6-Route # Routing Header for IPv6 ipv6-frag 44 IPv6-Frag # Fragment Header for IPv6 idrp 45 IDRP # Inter-Domain Routing Protocol rsvp 46 RSVP # Reservation Protocol gre 47 GRE # General Routing Encapsulation esp 50 IPSEC-ESP # Encap Security Payload [RFC2406] ah 51 IPSEC-AH # Authentication Header [RFC2402] skip 57 SKIP # SKIP ipv6-icmp 58 IPv6-ICMP # ICMP for IPv6 ipv6-nonxt 59 IPv6-NoNxt # No Next Header for IPv6 ipv6-opts 60 IPv6-Opts # Destination Options for IPv6 rspf 73 RSPF CPHB # Radio Shortest Path First (officially CPHB) vmtp 81 VMTP # Versatile Message Transport eigrp 88 EIGRP # Enhanced Interior Routing Protocol (Cisco) ospf 89 OSPFIGP # Open Shortest Path First IGP ax.25 93 AX.25 # AX.25 frames ipip 94 IPIP # IP-within-IP Encapsulation Protocol etherip 97 ETHERIP # Ethernet-within-IP Encapsulation [RFC3378] encap 98 ENCAP # Yet Another IP encapsulation [RFC1241] # 99 # any private encryption scheme pim 103 PIM # Protocol Independent Multicast ipcomp 108 IPCOMP # IP Payload Compression Protocol vrrp 112 VRRP # Virtual Router Redundancy Protocol [RFC5798] l2tp 115 L2TP # Layer Two Tunneling Protocol [RFC2661] isis 124 ISIS # IS-IS over IPv4 sctp 132 SCTP # Stream Control Transmission Protocol fc 133 FC # Fibre Channel mobility-header 135 Mobility-Header # Mobility Support for IPv6 [RFC3775] udplite 136 UDPLite # UDP-Lite [RFC3828] mpls-in-ip 137 MPLS-in-IP # MPLS-in-IP [RFC4023] manet 138 # MANET Protocols [RFC5498] hip 139 HIP # Host Identity Protocol shim6 140 Shim6 # Shim6 Protocol [RFC5533] wesp 141 WESP # Wrapped Encapsulating Security Payload rohc 142 ROHC # Robust Header Compression